Q:

Volley request to django rest handle response

I am using django with the rest framework as backend for an Android application and. I use a POST request to register users, which works fine, but there is something I'd like to handle and I don't know how to do.
When the username is already taken, django sends back a http code 400 and a json object with some info (such as username already taken). I can see that in postman, but on Android all I get is com.android.volley.ClientError . Is there a way to get the message that django is giving back?
Here is the function on my views on Django:
def registere(request):
    ans = RegSerializer(data=request.data)
    if ans.is_valid():
        ans.save()
        return Response(ans.data, status=status.HTTP_201_CREATED)
    else:
        return Response(ans._errors, status=status.HTTP_400_BAD_REQUEST)

And here is how I handle errors on Android:
new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(getApplicationContext(), "Error:  " + error.toString(), Toast.LENGTH_SHORT).show();
                    }
                }

A:

Try this to parse error from django :
new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        try {
                       if (error.networkResponse != null && error.networkResponse.data != null) {
                       String data = new String(error.networkResponse.data, "UTF-8");
                       int statusCode = error.networkResponse.statusCode; 
                       Log.e(LOG_TAG, "CustomRequest: Parsed Error Response = " + data);
                                }
                            } catch (Exception e) {
                               e.printStackTrace();
                         }
                    }
                }

Hope this helps !

