Empty Response Received on Android POST Request

☆樱花仙子☆ 提交于 2020-01-04 17:13:37

问题


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", "nifras.personal@gmail.com");
    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);

回答1:


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.




回答2:


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);



回答3:


Use this for solve your problem

ArrayList<NameValuePair> arrResult  = new ArrayList<NameValuePair>();
arrSchoolResult.add(new BasicNameValuePair("email", ""nifras.personal@gmail.com""));


来源:https://stackoverflow.com/questions/30725843/empty-response-received-on-android-post-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!