Search code examples
androidandroid-volleyjsonobjectrequest

Getting Volley.ServerError while making a JSONObjectRequest to an IP with a PORT


I have seen many blogs and links but couldn't find a satisfactory answer. I am making a simple POST request (JSONObjectRequest) using volley in my application but getting com.android.volley.ServerError in volley error. The URL I am using is "http://192.XXX.X.XX:8080/zin-pushnotification_stage/pushnotification/service/register". Is it possible that volley does not support urls having port numbers in it? Need a simple and precise solution.

Code:

Map<String, String> body = new HashMap<>();
body.put("deviceId", "STECH-1502878253");
body.put("token", "XYZ");
body.put("deviceType", "IPHONE");
body.put("companyKey", "STECH");
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST,"http://192.XXX.X.XX:8080/zin-pushnotification_stage/pushnotification/service/register", new JSONObject(body), new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.e("Data response", response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("VolleyError","Error response", error);
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> header = new HashMap<>();
                header.put("Content-Type", ApplicationConstants.Content_Type);
                header.put("Authorization", ApplicationConstants.CUSTOM_AUTH_TOKEN);
                return header;
            }
        };

        requestQueue.add(request);

Stack Trace:

E/Volley: [35860] BasicNetwork.performRequest: Unexpected response code 400 for http://192.XXX.X.XX:8080/zin-pushnotification_stage/pushnotification/service/register
W/System.err: com.android.volley.ServerError
W/System.err:     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:163)
W/System.err:     at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)

Solution

  • JsonObjectRequest extends JsonRequest. If you take a look at the source code of JsonRequest you will spot:

     /** Default charset for JSON request. */
    protected static final String PROTOCOL_CHARSET = "utf-8";
    
    /** Content type for request. */
    private static final String PROTOCOL_CONTENT_TYPE =
    String.format("application/json; charset=%s", PROTOCOL_CHARSET);
    
    @Override
        public String getBodyContentType() {
            return PROTOCOL_CONTENT_TYPE;
    }
    

    So JsonObjectRequest by default sets the content-type header to application/json; charset=utf-8. What you did was to also send another header for content-type which caused the server response with status 400. Removing the extra header by deleting the line:

    header.put("Content-Type", ApplicationConstants.Content_Type);
    

    made the request to include only the default header which was the correct one.