Empty Response Received on Android POST Request

2.3k views Asked by At

I'm using Volley to perform rest requests on android. When I make a login attempt it fails to give the response and instead returns an empty response.

The server side works fine, I have received the empty response on Android client side. This is the code I wrote:

HashMap<String, String> params = new HashMap<String, String>();
    params.put("email", "[email protected]");
    params.put("password", "123456");

    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
            urlJsonObj, new JSONObject(params), new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            Log.d(TAG, response.toString());


            Toast.makeText(getApplicationContext(),
                    response.toString(),
                    Toast.LENGTH_LONG).show();
            hidepDialog();
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_SHORT).show();
            // hide the progress dialog
            hidepDialog();
        }
    });

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(jsonObjReq);
3

There are 3 answers

0
nifCody On BEST ANSWER

The correct Answer for this question is shown below. In this version of the request, the Post parameters are overriden in the existing getParams() method of Volley. The mistake I did was to not override that method.

String url = "http://httpbin.org/post";

StringRequest postRequest = new StringRequest(Request.Method.POST, url,
    new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonResponse = new JSONObject(response).getJSONObject("form");
                String site = jsonResponse.getString("site"),
                        network = jsonResponse.getString("network");
                System.out.println("Site: "+site+"\nNetwork: "+network);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    },
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    }
) {
@Override
protected Map<String, String> getParams()
{
    Map<String, String>  params = new HashMap<>();
    // the POST parameters:
    params.put("site", "code");
    params.put("network", "tutsplus");
    return params;
}
};
Volley.newRequestQueue(this).add(postRequest);
5
Anand Savjani On

Use this for solve your problem

ArrayList<NameValuePair> arrResult  = new ArrayList<NameValuePair>();
arrSchoolResult.add(new BasicNameValuePair("email", ""[email protected]""));
0
TommySM On

Try Using Map and Object (I use that for String too) like this:

   Map<String, Object> jsonParams = new HashMap<>();
    jsonParams.put("param1", getParam1());
    jsonParams.put("param2", getParam2());

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, new JSONObject(jsonParams),
            new Response.Listener<JSONObject>()
            {
                @Override
                public void onResponse(JSONObject response)
                {
             // ...... you know the rest

btw, 400 is usually bad request (if my memory serves me correctly), so I would print the JSON object before sending it, and see if it's exactly what server is expecting, you can use Postman to check it too, might be easier (REST client version of Postman in chrome is more comfortable to use).

a quick and dirty way to print it:

   Map<String, Object> jsonParams = new HashMap<>();
    jsonParams.put("param1", getParam1());
    jsonParams.put("param2", getParam2());

    Log.d("My JSON Object",(new JSONObject(jsonParams)).toString());

Hope This Helps.