我有这个应用程序,我使用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” 被记录代替,而不管指定我内容。毋庸置疑,由于身体未被完全阅读,这完全无法满足要求。有人知道这种行为背后的原因吗?
我钢是-1 ...我使用编译'com.squareup.okhttp:okhttp:2.4.0' –