Java 如何从Android向Django发送数据
我找不到POST数据的问题,我在Heroku和android应用程序上部署了django应用程序,需要在用户完成游戏时向其发送数据。当我测试不使用POST方法保存数据(硬编码值)时,它起作用了。有人能告诉我代码中的问题在哪里吗 url.pyJava 如何从Android向Django发送数据,java,android,python,django,post,Java,Android,Python,Django,Post,我找不到POST数据的问题,我在Heroku和android应用程序上部署了django应用程序,需要在用户完成游戏时向其发送数据。当我测试不使用POST方法保存数据(硬编码值)时,它起作用了。有人能告诉我代码中的问题在哪里吗 url.py url(r'^scorepost/', 'scorepost'), view.py @csrf_exempt def scorepost(request): if request.method == 'POST':
url(r'^scorepost/', 'scorepost'),
view.py
@csrf_exempt
def scorepost(request):
if request.method == 'POST':
email = string(request.POST['email'])
difficulty = string(request.POST['difficulty'])
time = string(request.POST['time'])
moves = string(request.POST['moves'])
score = string(request.POST['score'])
user = CustomUser.objects.get(email=email)
Highscore.objects.create(user_id = user.id, difficulty = difficulty, time = time, moves = moves, score = score)
return HttpResponse("Score saved")
return HttpResponse("Score not saved")
安卓
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://testdjangoandroid.herokuapp.com/scorepost/");
try {
List<NameValuePair> pairs = new ArrayList<NameValuePair>(5);
pairs.add(new BasicNameValuePair("email", user_email));
pairs.add(new BasicNameValuePair("difficulty", Integer.toString(difficulty)));
pairs.add(new BasicNameValuePair("time", Long.toString(totalTime)));
pairs.add(new BasicNameValuePair("moves", Integer.toString(totalMoves)));
pairs.add(new BasicNameValuePair("score", Long.toString(totalScore)));
httppost.setEntity(new UrlEncodedFormEntity(pairs));
httpclient.execute(httppost);
HttpClient-HttpClient=newdefaulthttpclient();
HttpPost HttpPost=新的HttpPost(“http://testdjangoandroid.herokuapp.com/scorepost/");
试一试{
列表对=新的ArrayList(5);
添加(新的BasicNameValuePair(“电子邮件”,用户电子邮件));
添加(新的BasicNameValuePair(“难度”,Integer.toString(难度));
添加(新的BasicNameValuePair(“时间”,Long.toString(totalTime)));
添加(新的BasicNameValuePair(“移动”,Integer.toString(totalMoves)));
添加(新的BasicNameValuePair(“分数”,Long.toString(totalScore)));
setEntity(新的UrlEncodedFormEntity(对));
httpclient.execute(httppost);
您使用的是什么django版本?如果>=1.5:您是否正确设置了允许的\u域设置?电子邮件未使用,并且未定义用户。您是否收到任何类型的错误?错误的症状是什么?@alecxe我在设置中找不到允许的\u域,您能告诉我如何设置吗在哪里做呢?@aleksandar oops,该设置称为ALLOWED_HOSTS,请参阅。