Android POST 请求收到空响应

Empty Response Received on Android POST Request

我正在使用 Volley 在 android 上执行休息请求。当我尝试登录时,它没有给出响应,而是 returns 一个空响应。

服务器端工作正常,我在 Android 客户端收到空响应。 这是我写的代码:

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

用它来解决你的问题

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

像这样尝试使用映射和对象(我也将其用于字符串):

   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

顺便说一句,400 通常是错误的请求(如果我没记错的话),所以我会在 发送对象 之前打印它,看看它是否是正是服务器所期望的,您也可以使用 Postman 来检查它,可能会更容易(chrome 中的 Postman 的 REST 客户端版本使用起来更舒适)。

一种快速而肮脏的打印方式:

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

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

希望对您有所帮助。

此问题的正确答案如下所示。在此版本的请求中,Post 参数在 Volley 现有的 getParams() 方法中被覆盖。我犯的错误是没有覆盖那个方法。

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