0

I have a android app which tries to register a user to django server, sending a POST request with json.

the response code I get is 500 INTERNAL SERVER ERROR

Android code:

httpClient = new DefaultHttpClient();
registerUserRequest = new HttpPost("http://10.100.102.9:8000/users/register");

public void registerUser(){


    Map<String, String> userMap = new HashMap<String, String>();
    userMap.put("username", "UserName");
    userMap.put("password", "12345");
    userMap.put("first_name", "first");
    userMap.put("last_name", "last");

    JSONObject obj=new JSONObject(params);

    StringEntity se = new StringEntity(obj.toString());
    registerUserRequest.setEntity(se);
    registerUserRequest.setHeader("Content-type", "application/json");
    new executeRequest().execute(registerUserRequest);
}

 private class executeRequest extends AsyncTask<HttpRequest, Void, Integer>
 {

    @Override
    protected Integer doInBackground(HttpRequest... params) {
        // TODO Auto-generated method stub
        HttpRequest request = params[0];
        int responseCode = -1;
        ResponseHandler responseHandler = new BasicResponseHandler();
        HttpResponse response;
        try {
            response = httpClient.execute(registerUserRequest,
                    responseHandler);

            Log.d("Response of REGISTER_USER request", response.toString());
            responseCode = response.getStatusLine().getStatusCode();
        } catch (ClientProtocolException e) {
            // This exception raised
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return responseCode;
    }

}

The exception raised is ClientProtocolException in the AsyncTask.

Django Code:

class UserViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer

    @list_route(methods=['post'])
    def register(self, request):
        serializer = UserSerializer(data=request.DATA)
        if serializer.is_valid():
            user = User.objects.create_user(
                username = serializer.init_data['username'],
                password = serializer.init_data['password'],
                first_name = serializer.init_data['first_name'],
                last_name = serializer.init_data['last_name'],
            )

            return Response(serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

The URL is fine I tested it - "http://10.100.102.9:8000/users/register"

ERROR: the response code I get is 500 INTERNAL SERVER ERROR

05-24 13:05:14.317: W/System.err(15625): org.apache.http.client.HttpResponseException: INTERNAL SERVER ERROR
05-24 13:05:14.317: W/System.err(15625):    at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:71)
05-24 13:05:14.327: W/System.err(15625):    at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:59)
05-24 13:05:14.337: W/System.err(15625):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657)
05-24 13:05:14.337: W/System.err(15625):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)

Any ideas why is this happening?

thanks!

9
  • Please post the error too. Commented May 24, 2015 at 10:01
  • you mean in client-side? edited my question Commented May 24, 2015 at 10:06
  • I mean the error that Django in your server returns to your client. Commented May 24, 2015 at 10:07
  • i tried to look for how to debug in PyCharm Django projects, couldn't really find any help. the message in the Console is [24/May/2015 17:11:10]"POST /users/register HTTP/1.1" 500 66235 I realize its not much... do you happen to know how to debug in django? Commented May 24, 2015 at 14:24
  • Since you cannot access the server report but you are using PyCharm, it has a very good debugger. Just create a breakpoint early in your view and step to the problem. Commented May 24, 2015 at 15:19

1 Answer 1

3

500 INTERNAL SERVER ERROR means error in server side code not mobile side. check the exception happened in server side.

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

Comments

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.