【问题标题】:Getting headers from a response in volley从凌空响应中获取标头
【发布时间】:2016-07-25 15:58:14
【问题描述】:

我正在使用 Volley,我想向服务器发出请求,该服务器在“可见层”中返回一个 JSON(我可以在 Web 浏览器中看到它)。我的问题是服务器还返回了我需要在我的应用程序中获取的标头信息,但我无法从请求中获取标头。

找了好久都没找到有用的东西(只在请求头中添加数据,没有从头的响应中获取数据)

有人知道如何实现吗?

【问题讨论】:

    标签: android header android-volley


    【解决方案1】:

    要获取您需要在请求中覆盖 parseNetworkResponse() 的标头。

    例如JsonObjectRequest:

    public class MetaRequest extends JsonObjectRequest {
    
        public MetaRequest(int method, String url, JSONObject jsonRequest, Response.Listener
                <JSONObject> listener, Response.ErrorListener errorListener) {
            super(method, url, jsonRequest, listener, errorListener);
        }
    
        public MetaRequest(String url, JSONObject jsonRequest, Response.Listener<JSONObject>
                listener, Response.ErrorListener errorListener) {
            super(url, jsonRequest, listener, errorListener);
        }
    
        @Override
        protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
            try {
                String jsonString = new String(response.data,
                        HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
                JSONObject jsonResponse = new JSONObject(jsonString);
                jsonResponse.put("headers", new JSONObject(response.headers));
                return Response.success(jsonResponse,
                        HttpHeaderParser.parseCacheHeaders(response));
            } catch (UnsupportedEncodingException e) {
                return Response.error(new ParseError(e));
            } catch (JSONException je) {
                return Response.error(new ParseError(je));
            }
        }
    }
    

    【讨论】:

    • 谢谢!!有用!!我在 parseNetworkResponse 的标头中做我的工作,而不是将标头添加到返回的 JSONObject 中。
    【解决方案2】:

    这是一个使用 JSONArray 数据和标头的示例。

    首先创建自己的自定义请求类型实现:

    public class JsonRequest extends JsonObjectRequest {
    
        public JsonRequest(int method, String url, JSONObject jsonRequest, Response.Listener
                <JSONObject> listener, Response.ErrorListener errorListener) {
            super(method, url, jsonRequest, listener, errorListener);
        }
    
        @Override
        protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
            try {
                String jsonString = new String(response.data,
                        HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
    
                JSONObject jsonResponse = new JSONObject();
                jsonResponse.put("data", new JSONArray(jsonString));
                jsonResponse.put("headers", new JSONObject(response.headers));
    
                return Response.success(jsonResponse,
                        HttpHeaderParser.parseCacheHeaders(response));
            } catch (UnsupportedEncodingException e) {
                return Response.error(new ParseError(e));
            } catch (JSONException je) {
                return Response.error(new ParseError(je));
            }
        }
    }
    

    并在您的请求代码中:

    JsonRequest request = new JsonRequest
            (Request.Method.POST, URL_API, payload, new Response.Listener<JSONObject>() {
    
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONArray data = response.getJSONArray("data");
                        JSONObject headers = response.getJSONObject("headers");
                    } catch (JSONException e) {
                        Log.e(LOG_TAG, Log.getStackTraceString(e));
                    }
                }
            }, new Response.ErrorListener() {
    
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e(LOG_TAG, Log.getStackTraceString(error));
                }
            });
    

    在 Volley 文档Implementing a Custom Request 中查看有关实现您自己的自定义请求的更多信息。

    【讨论】:

    • 什么是有效载荷?
    • Payload 是您的请求参数,它可以是 JSON。一个例子:JSONObject payload = new JSONObject(); payload.put("email", email); payload.put("password", password);
    猜你喜欢
    • 1970-01-01
    • 2020-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 2017-07-28
    • 2019-03-04
    相关资源
    最近更新 更多