2017-06-05 6 views
0

私のdjango APiへのポストリクエストを送信しようとしていますが、それは正常に動作しても何のエラーも出ませんが、まだdjangoはどのデータも受け取りません。 私の見解はこのようになります。どのようにアンドロイドからdjangoサーバーへの投稿をHTTPにするのですか?

def add(request): 
    if request.method == "POST": 
     form = bookForm(request.POST) 
     if form.is_valid(): 
      data = form.save(commit=False) 
      data.save() 
      return redirect('/books') 
    else: 
     form = bookForm 
return render(request, 'book_edit.html', {'form': form}) 

私のHttpポスト方法はです。

try{ 
       Log.w("this","Started"); 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost("http://vidu171.pythonanywhere.com/books/add"); 

       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
       nameValuePairs.add(new BasicNameValuePair("title", params[0])); 
       nameValuePairs.add(new BasicNameValuePair("author","vidu")); 

       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       HttpResponse response = httpclient.execute(httppost); 
       Log.w("htt response",response.toString()); 
}catch(Exception e){ 
       Log.e("log_tag", "Error in http connection "+e.toString()); 

      } 

私は、フォームへのURLが http://vidu171.pythonanywhere.com/books/addで許可を与えています。私はAndroidアプリのような他のソースからのDjangoでAPI呼び出しを行うため、その残りのAPIを使用することをお勧めし、ジャンゴの場合には、Djangoの残りFramework.Youを使用してみていることを示唆している事前

答えて

2

におかげでこのTutorial

で開始することができます
+0

私はそのようなAPiを作ったが、私はどのようにANdroidからAPIに投稿するのか分からない。 –

+0

Rest Frameworkを使用してAPIを再実装してみてください。 – AR7

+0

次にアンドロイドから投稿する方法は? –

関連する問題