How to send data from Android to Django
Question
I can't find the problem with POST data, I have django app deployed on Heroku and android app that needs to send data to it when user finishes game. When I tested data saving without POST method (hardcoded values) it worked. Can anybody tell me where is the problem in my code?
urls.py
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")
android
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);
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow