Java Android Volley 访问 http 响应头字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20702178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:40:35  来源:igfitidea点击:

Android Volley access http response header fields

javaandroidhttpandroid-volley

提问by Sergio Serra

How can i access HTTP header fields like ETag from a response using Volley ? With HttpUrlCoonectioni just do conn.getHeaderField("ETag")and that's it.

我如何使用 Volley 从响应中访问像 ETag 这样的 HTTP 标头字段?随着HttpUrlCoonection我只是做conn.getHeaderField("ETag"),就是这样。

Thanks

谢谢

采纳答案by diegocarloslima

You can subclass Request(or any of its subclasses) and override the parseNetworkResponsemethod:

您可以子类化Request(或其任何子类)并覆盖该parseNetworkResponse方法:

@Override
protected Response<Bitmap> parseNetworkResponse(NetworkResponse response) {
    Map<String, String> responseHeaders = response.headers;
}

回答by Blaz

You can extend Requestclass. Then when you implement parseNetworkResponse(NetworkResponse response)method you can access header values in response.headers. So you can access ETag header like response.headers.get("ETag"). What I did was to then add this header value in response object like response.setETag(etag)and than I just return it in Response.success(response, null). Response object will then be delivered to deliverResponse(E response)where you can send it forward to some listener.

你可以扩展Request类。然后,当您实现parseNetworkResponse(NetworkResponse response)方法时,您可以访问response.headers. 因此,您可以访问 ETag 标头,例如response.headers.get("ETag"). 我所做的是在响应对象中添加这个标头值,response.setETag(etag)而不是我只是在Response.success(response, null). 然后响应对象将被传递到deliverResponse(E response)您可以将其转发给某个侦听器的位置。