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

Ido Magor
  • 544
  • 1
  • 5
  • 14

1 Answers1

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.

  • I will add it right now and tell you what is the output – Ido Magor Jul 05 '15 at 16:12
  • 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 – Ido Magor Jul 05 '15 at 16:15
  • you will have to import sys at the top of the file, just write import sys –  Jul 05 '15 at 16:17
  • 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 – Ido Magor Jul 05 '15 at 16:20
  • 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. –  Jul 05 '15 at 16:24
  • i am using class based views look at my post.. :) – Ido Magor Jul 05 '15 at 16:25
  • My bad ;) I lost the track of that. –  Jul 05 '15 at 16:27
  • Have a look at this [link]http://stackoverflow.com/questions/6838831/django-rest-framework-function-object-has-no-attribute-as-view –  Jul 05 '15 at 16:29
  • But i do use the csrf_extemp as decorations for my class based view and its still doesn't work... – Ido Magor Jul 05 '15 at 16:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/82415/discussion-between-ido-magor-and-rajesh-yogeshwar). – Ido Magor Jul 05 '15 at 16:37