Ido Magor
Ido Magor

Reputation: 544

Android access django rest framework makes HTTP 500

I'm an experienced developer with android and currently i am developing android application that has Django as its server side on heroku cloud.

I'm pretty new to django and django rest framework so i dont really get how to use it except for the guide on their website.

What i was trying to do recently was using Volley/AsyncHttpClient/Apache Http to contact with my django server.

With each of these libraries i got Http 500 error on django in his console output.

What i tried to do on each of them is adding data to the body or parameters. On Volley - i overrided the getParams and added them to hash on AsyncHttpClient - i made RequestParams on HttpClient(apache) - i used a list of NameValuePair and added them as entity of UrlEncodedForm to the http post request.

i also tried on volley and asynchttpclient to add data to the body of the request and it didn't worked also.

I even thought of changing my server side because of all the trouble Django is causing me , so please if anyone have the answer please give it :)

This is my Server Side(Django):

class User(APIView):
    queryset = AppUser.objects.all()

    def get(self,request,format=None):
        users = AppUser.objects.all()
        serialized_users = UserSerializer(users, many=True)
        return HttpResponse(serialized_users.data)

    def post(self,request):
        user_serializer = UserSerializer(data=request.DATA)
        if user_serializer.is_valid():
            user_serializer.save()
            return HttpResponse(status.HTTP_201_CREATED)
        return HttpResponse(status=status.HTTP_406_NOT_ACCEPTABLE)

**There's no point to show the urls/models/serializer because it all works on the google chrome with the GET method.

Android(apache http client):

    try {
                    DefaultHttpClient client = new DefaultHttpClient();
                    HttpPost post = new HttpPost(url);
                    List<NameValuePair> paramsList = new ArrayList<NameValuePair>();
                    paramsList.add(new BasicNameValuePair("user",userJson));

                    post.setEntity(new UrlEncodedFormEntity(paramsList));

                    HttpResponse response = client.execute(post);
                    HttpEntity entity = response.getEntity();
                } catch (Exception e) {

                }

Android (AsyncHttpClient):

    try {
                        RequestParams params = new RequestParams();
                        params.put("user",userJson);
                        mClient.post(msg.getText().toString(),params,new AsyncHttpResponseHandler() {
                            @Override
                            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                                if(statusCode == 201) Toast.makeText(MainActivity.this,"Success" , Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
   }
                    });
                } catch (Exception e) {

                }

I'm really clueless what to do next because i think i covered all my options contacting my server...

Thanks

Upvotes: 0

Views: 561

Answers (1)

user3631116
user3631116

Reputation:

If I am not wrong, the message in console just says Http 500 Server error without the cause right?

To debug it more, add following to your settings(base.py)

LOGGING = {
 'version': 1,
 'disable_existing_loggers': False,
 'handlers': {
   'console': {
     'level': 'ERROR',
     'class': 'logging.StreamHandler',
     'stream': sys.stderr
   },
  },
 'loggers': {
   'django.request': {
     'handlers': ['console'],
     'propogate': True,
     'level': 'ERROR',
   }
 }
}

This few lines in your settings will print the cause of 500 error in the console, you might get clue to what you are doing wrong.

Upvotes: 1

Related Questions