2015-09-18 90 views
1

我有这个应用程序,我使用OkHttp RequestBody来执行与身体的网络请求,但返回的内容长度是明显错误的。由于问题的一个例子,有这样的情况:OkHttp RequestBody返回错误的内容长度

public static RequestBody createNewTokenBody(final String redirectUri, final String 
     code, 
              final String clientId, final String 
                clientSecret) { 

    final byte[] requestBody = "bunchofcharsthatshouldgointhebody".getBytes(Charset.forName("UTF-8")); 

    final RequestBody ret = RequestBody.create(MediaType.parse(MEDIA_TYPE), requestBody, 0, 
      requestBody.length); 

    try { 
     Log.d("debug", "Request body length to return: " + ret.contentLength()); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return ret; 
} 

因此,这将打印“请求车身长度返回:33”。 然而,当该主体被附接到该请求到由下面的拦截器捕获:

client.networkInterceptors().add(new Interceptor() { 
     @Override 
     public Response intercept(final Chain chain) throws IOException { 
      final Request request = chain.request(); 

      final RequestBody body = request.body(); 
      if (body != null) { 
       Log.d("debug", "REQUEST BODY LENGTH: " + body.contentLength()); 
      } 

      return chain.proceed(request); 
     } 
    }); 

“REQUEST身长度:2” 被记录代替,而不管指定我内容。毋庸置疑,由于身体未被完全阅读,这完全无法满足要求。有人知道这种行为背后的原因吗?

回答

0

如果将来需要引用它,则会将其报告为Retrofit-2 beta1中的错误,并且已在当前快照中修复。

https://github.com/square/retrofit/issues/1097

+0

我钢是-1 ...我使用编译'com.squareup.okhttp:okhttp:2.4.0' –

1

我无法在独立测试用例中重现此操作。如果可以的话,请用最小的可执行测试用例来报告针对OkHttp的错误,以证明问题。

+0

https://github.com/square/okhttp/issues/1864感谢。 –