文章标题很明确,就是获取Http网络请求响应的头信息。
可能我通过其他方式也可以获取,但是为了保持代码的格式一致性,最终还是找到了解决方法。直接上代码:
Retrofit Service定义如下:public interface CountryApiService { @GET("?") Observable> geoIp();}复制代码
CountryCodeModel数据模型是response body对应的数据类型,这不是重点。
重点是:ResponseResponse导入的包地址是:retrofit2.Response调用Retrofit定义的接口:
this.countryApiService.geoIp() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { e.printStackTrace(); LogUtil.e("qxs", "获取code失败"); } @Override public void onNext(Response countryCodeModelResponse) { LogUtil.e("qxs", "获取code成功," + countryCodeModelResponse.code()); LogUtil.e("qxs", "获取code成功," + countryCodeModelResponse.toString()); LogUtil.e("qxs", "获取code成功," + countryCodeModelResponse.headers().toString()); LogUtil.e("qxs", "获取code成功," + countryCodeModelResponse.body().toString()); } });复制代码
其实也很简单,如果不想获取Http状态码、头信息等数据,可直接去掉Response泛型。
不需要 获取Http状态码、头信息等数据,返回值泛型为:
Observable复制代码
需要 获取Http状态码、头信息等数据,返回值泛型为:
Observable>复制代码
其它的代码,我觉得都不需要再次粘贴了。