【问题标题】:volley response was not getting any data凌空响应没有得到任何数据
【发布时间】:2017-08-30 11:22:49
【问题描述】:

当我使用以下代码在 volley 中调用 post 服务但我使用得到响应时

E/VOLLEY:com.android.volley.NetworkResponse@3d5dc44

 try {
            RequestQueue requestQueue = Volley.newRequestQueue(this);
            String URL = "url";

         final   String requestBody = "mutation M {" + "logIn" + "(" +
                    "countryCode:" + "\"" + "1" + "\"" +
                    "deviceType:" + "\"" + "Android" + "\"" + 
                    "osVerison:" + "\"" + "5.1.1" + "\"" +
                    "appVerison:" + "\"" + "2.9" + "\"" +
                    "currentCountry:" + "\"" + "IND" + "\"" +
                    "language:" + "\"" + "ENG" + "\"" +
                    "deviceModal:" + "\"" + "ASUS" + "" +

                    "\")" + "{_id,countryCode,phoneNumber,emailAddress,profilePic}";


            StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.e("VOLLEY", response);
                }

            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("VOLLEY", error.toString());
                }
            }) {


                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String, String>  params = new HashMap<String, String>();
                    params.put("Cache-Control","no-cache");
                    params.put("Content-type", "application/graphql");
                    return params;
                }
                @Override
                public byte[] getBody() throws AuthFailureError {
                    try {
                        return requestBody == null ? null : requestBody.getBytes("utf-8");
                    } catch (UnsupportedEncodingException uee) {
                        VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
                        return null;
                    }
                }

                @Override
                protected Response<String> parseNetworkResponse(NetworkResponse response) {
                    String responseString = "";
                    if (response != null) {
                        responseString = response.toString();
                        // can get more details such as response.headers
                    }
                    return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
                }
            };

            requestQueue.add(stringRequest);
        } catch (Exception e) {
            e.printStackTrace();
        }

我是 volley 的新手,请帮助我进行改造,但我想在 volley 中使用。

【问题讨论】:

标签: android android-volley


【解决方案1】:

我已经修改了这段代码并为我工作我从 parseNetworkResponse 的超级方法中找到了这个

@Override
    protected Response < String > parseNetworkResponse(NetworkResponse response) {

     String parsed;
     try {
      parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
     } catch (UnsupportedEncodingException e) {
      parsed = new String(response.data);
     }
     return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));

    }

【讨论】:

    猜你喜欢
    • 2016-05-10
    • 2018-06-18
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 2023-01-19
    • 2021-09-09
    相关资源
    最近更新 更多