0

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

10 Comments

I will add it right now and tell you what is the output
I added that code to my settings.py and when i ran the server i got an error that says - the sys object you use in the 'handlers' -> 'stream' is not defined Full error output - > NameError: name 'sys' is not defined
you will have to import sys at the top of the file, just write import sys
I got this error: AttributeError: 'function' object has no attribute 'as_view' [05/Jul/2015 19:19:02]"POST /user/ HTTP/1 .1" 500 107417 I looked at my urls and they look liks this: urlpatterns = [ url(r'^user/$', views.User.as_view()), ] urlpatterns = format_suffix_patterns(urlpatterns) Also theres this error AttributeError: 'RegexURLResolver' object has no attribute '_urlconf_module' During handling of the above exception, a
First of all, User is a model(Table) in Django authentication system, so its worth changing it. Second, my guess is you are using function based views, if yes change them to class based views, define get and post methods as per your needs. Try again.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.