OkHttp RequestBody 返回错误的内容长度
OkHttp RequestBody returning wrong content length
我有这个应用程序,我在其中使用 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;
}
所以这将打印 "Request body length to return : 33"。 但是,当这个body附加到的请求被下面的拦截器捕获时:
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 BODY LENGTH: 2" 被记录,而不考虑我指定的内容。不用说,由于正文未完全阅读,这完全搞砸了请求。有人知道这种行为背后的原因吗?
我无法在独立测试用例中重现这一点。如果可以,请报告一个针对 OkHttp 的错误,并使用一个演示该问题的最小可执行测试用例。
如果需要它供将来参考,这已报告为 Retrofit-2 beta1 中的错误,并且已在当前快照中修复。
我有这个应用程序,我在其中使用 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;
}
所以这将打印 "Request body length to return : 33"。 但是,当这个body附加到的请求被下面的拦截器捕获时:
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 BODY LENGTH: 2" 被记录,而不考虑我指定的内容。不用说,由于正文未完全阅读,这完全搞砸了请求。有人知道这种行为背后的原因吗?
我无法在独立测试用例中重现这一点。如果可以,请报告一个针对 OkHttp 的错误,并使用一个演示该问题的最小可执行测试用例。
如果需要它供将来参考,这已报告为 Retrofit-2 beta1 中的错误,并且已在当前快照中修复。