0
我想發送請求到我的Django APi但它沒有給出任何錯誤,它工作正常,但仍然Django沒有收到任何數據。 我的觀點是這樣的。如何將Http發佈到android的django服務器?
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 post方法是。
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。在此先感謝
我做了這樣的APi但是我不知道如何從ANdroid發佈到API。 –
嘗試使用Rest框架重新實現您的API。 – AR7
那麼我將如何從Android發佈? –