【问题标题】:Empty Response Received on Android POST RequestAndroid POST 请求收到空响应
【发布时间】:2015-06-09 07:40:44
【问题描述】:

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

服务器端工作正常,我在 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);

【问题讨论】:

  • 06-09 13:11:50.239 26850-26992/com.tadhack2015.tuktuk E/Volley:[1671] BasicNetwork.performRequest:apps.byethost4.com/tuktuk/rest/v1/login 的意外响应代码 400
  • 我在 Logcat 中收到了这个错误
  • 您是否尝试过使用 AdvancedRestClient,如果您也发现同样的错误?
  • 我在邮递员身上检查得很好,效果很好

标签: android rest android-volley


【解决方案1】:

尝试像这样使用 Map 和 Object(我也将它用于 String):

   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 通常是错误的请求(如果我的记忆正确的话),所以我会在发送 JSON 对象 之前 打印它,看看它是否正是服务器所期望的,你可以使用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());

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    这个问题的正确答案如下所示。在此版本的请求中,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);
    

    【讨论】:

      【解决方案3】:

      用它来解决你的问题

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

      【讨论】:

      • 在 Android 5.0 上已弃用
      • 对不起怎么办
      • ContentValues contentValues = new ContentValues(); contentValues.put("电子邮件","nifras.personal@gmail.com");并在参数中传递内容值
      • 嗨,数组列表在新版本的 android 中已过时。这就是它被弃用的原因。
      • 现在他们以任何方式使用 HashMaps,感谢您的努力。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-12
      • 2012-04-14
      • 1970-01-01
      • 1970-01-01
      • 2021-07-25
      • 2021-01-21
      • 2020-11-17
      相关资源
      最近更新 更多