抽象地从响应中获取标题 [英] Getting headers from a response in volley

查看:109
本文介绍了抽象地从响应中获取标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working with Volley, I want to make request to a server which returns me a JSON in the "vissible layer" (I can see it in the web browser). My problem is that the server also returns my in the headers information that I need to get in my App, but I am not able to get the headers from the request.

我搜索了很长时间,但我还没有发现任何有用的东西(只有将数据添加到请求标题,但没有从头文件的响应中获取数据)

I have searched a long time but I havent found anything usefull (Onlye adding data to the request Header, but not getting data from the header´s response)

任何人都知道如何执行那个?

Anyone knows how to implement that?

推荐答案

要获取标题,您需要覆盖 parseNetworkResponse() in您的请求。

To get the headers you need to override parseNetworkResponse() in your request.

例如 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));
        }
    }
}

这篇关于抽象地从响应中获取标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆