【问题标题】:Retrofit set tag on request that gets returned with response根据请求改造设置标签,并随响应返回
【发布时间】:2015-06-16 15:39:49
【问题描述】:

基本上,我的问题很简单。我正在使用改造作为与我无法控制的服务器进行通信的框架。我想在我的请求上设置某种标签,该标签会自动在响应中返回。知道如何实现这一点吗?

【问题讨论】:

  • @ethan123 您要发送什么类型的值。您可以设置自定义标头,但这将要求服务器将标头设置回响应中。当前没有任何通用数据的请求/响应标头。

标签: android retrofit


【解决方案1】:

我发现了一种复杂且不酷的方法来做到这一点。

0。在您的请求和响应类型中添加标签字段

1.自定义okhttp3.RequestBody添加标签字段:

2。自定义okhttp3.ResponseBody添加标签字段:

3.自定义Converter.Factory 设置和获取标签:
例如,我对GsonResponseBodyConverterGsonRequestBodyConverter 做了一些更改:

TagGsonRequestBodyConverter.java:

@Override public RequestBody convert(T value) throws IOException {
    Buffer buffer = new Buffer();
    Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8);
    JsonWriter jsonWriter = gson.newJsonWriter(writer);
    adapter.write(jsonWriter, value);
    jsonWriter.close();

    TagRequestBody requestBody = TagRequestBody.create(MEDIA_TYPE, buffer.readByteString());
    requestBody = value.tag;//just for example ,you will need to check type here
    return requestBody;
  }

TagGsonResponseBodyConverter.java:

    @Override public T convert(ResponseBody source) throws IOException {
try {
  //the ugly part,for that retrofit will wrap the responseBody with ExceptionCatchingRequestBody.(ExceptionCatchingRequestBody extends ResponseBody) 
  ResponseBody value = source;
  if (value.getClass().getSimpleName().equals("ExceptionCatchingRequestBody")){
    ResponseBody temp = null;
    try {
      Field f = source.getClass().getDeclaredField("delegate");
      f.setAccessible(true);
      temp = (T) f.get(value);
    } catch (Exception e) {
      e.printStackTrace();
    }
    value = temp != null?temp:value;
  }
  T t = adapter.fromJson(source.charStream());
  if (value instanceof TagResponseBody) {
    t.tag = ((TagResponseBody)value).tag;
  }
  return t;
} finally {
  value.close();
}

}

4.在创建改造的okhttpClient时添加拦截器,将标签从TagRequestBody传递给TagResponseBody:

.addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Response response = chain.proceed(chain.request());
                        if (request.body() instanceof TagRequestBody) {
                            TagResponseBody responseBody = new TagResponseBody(response.body());
                            responseBody.arg = ((TagRequestBody) request.body()).arg;
                            response = response.newBuilder()
                                    .body(responseBody)
                                    .build();
                        }
                        return response;
                    }
                })

所以标签被持有:
RequestDataWithTag --CustGsonRequestBodyConverter--> RequestBodyWithTag --Interceptor--> ResponseBodyWithTag --CustGsonResponseBodyConverter--> ResponseDataWithTag

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-16
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    相关资源
    最近更新 更多