我正在开发一个带有一些端点的应用程序。在摘要补丁请求中,当已填充正文时不回调,并且出现超时错误。此事件仅针对补丁请求和其他方法(如 GET、POST、PUT 和 DELETE (正常工作而发生。另一件事是这些端点在邮递员中都可以正常工作。
我的服务之一
public interface ApiService {
@Headers({"Accept: application/json;version=0.1.1",
"Content-Type: application/json; charset=utf-8"})
@PATCH("api/club/product/{id}/")
Call<ResponseBody> requestEditProducts(@Header ("Authorization") String token,
@Body RequestBody body,
@Path("id") int id);
}
客户端类
public class RetrofitClient {
private static Retrofit retrofit = null;
public static Retrofit getInstance() {
if (retrofit == null) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(logging);
httpClient.connectTimeout(120, TimeUnit.SECONDS);
httpClient.callTimeout(120, TimeUnit.SECONDS);
httpClient.readTimeout(120, TimeUnit.SECONDS);
httpClient.writeTimeout(120, TimeUnit.SECONDS);
httpClient.retryOnConnectionFailure(true);
retrofit = new Retrofit.Builder()
.baseUrl(Consts.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
}
return retrofit;
}
}
一个调用示例
JSONObject object = new JSONObject();
try {
object.put("name", "new name");
} catch (JSONException e) {
e.printStackTrace();
}
RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), object.toString());
Call<ResponseBody> call = RetrofitClient.getInstance().create(ApiService.class)
.requestEditProducts(token, requestBody, id);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Log.d("PATCH_REQUEST", "code: " + response.code() +
" body: " + response.body());
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
t.printStackTrace();
}
});
一次调用结果
D/OkHttp: --> PATCH http://beta.nsme.com/api/club/product/9/
D/OkHttp: Content-Type: application/json; charset=utf-8
D/OkHttp: Content-Length: 41
D/OkHttp: Accept: application/json;version=0.1.1
D/OkHttp: Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxx
D/OkHttp: {"name":"new name"}
D/OkHttp: --> END PATCH (41-byte body)
D/OkHttp: <-- HTTP FAILED: java.net.SocketException: Socket closed
W/System.err: java.io.InterruptedIOException: timeout
W/System.err: at okhttp3.internal.connection.Transmitter.timeoutExit(Transmitter.java:109)
W/System.err: at okhttp3.internal.connection.Transmitter.maybeReleaseConnection(Transmitter.java:302)
W/System.err: at okhttp3.internal.connection.Transmitter.noMoreExchanges(Transmitter.java:267)
W/System.err: at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:237)
W/System.err: at okhttp3.RealCall$AsyncCall.execute(RealCall.java:172)
W/System.err: at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err: at java.lang.Thread.run(Thread.java:818)
W/System.err: Caused by: java.net.SocketException: Socket closed
W/System.err: at libcore.io.Posix.recvfromBytes(Native Method)
W/System.err: at libcore.io.Posix.recvfrom(Posix.java:185)
W/System.err: at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:250)
W/System.err: at libcore.io.IoBridge.recvfrom(IoBridge.java:553)
W/System.err: at java.net.PlainSocketImpl.read(PlainSocketImpl.java:485)
W/System.err: at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:37)
W/System.err: at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)
W/System.err: at okio.Okio$2.read(Okio.java:140)
W/System.err: at okio.AsyncTimeout$2.read(AsyncTimeout.java:237)
W/System.err: at okio.RealBufferedSource.indexOf(RealBufferedSource.java:358)
W/System.err: at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:230)
W/System.err: at okhttp3.internal.http1.Http1ExchangeCodec.readHeaderLine(Http1ExchangeCodec.java:242)
W/System.err: at okhttp3.internal.http1.Http1ExchangeCodec.readResponseHeaders(Http1ExchangeCodec.java:213)
W/System.err: at okhttp3.internal.connection.Exchange.readResponseHeaders(Exchange.java:115)
W/System.err: at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:94)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
W/System.err: at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:43)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
W/System.err: at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:94)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
W/System.err: at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
W/System.err: at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:88)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
W/System.err: at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:212)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
W/System.err: at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:229)
W/System.err: ... 5 more
似乎问题是您重新创建改造。您必须将此创作包含在您的单例中:
public class RetrofitClient {
private static ApiService api;
public static ApiService getInstance() {
if (api == null) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(logging);
httpClient.connectTimeout(120, TimeUnit.SECONDS);
httpClient.callTimeout(120, TimeUnit.SECONDS);
httpClient.readTimeout(120, TimeUnit.SECONDS);
httpClient.writeTimeout(120, TimeUnit.SECONDS);
httpClient.retryOnConnectionFailure(true);
retrofit = new Retrofit.Builder()
.baseUrl(Consts.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
api = retrofit.create(ApiService.class)
}
return api;
}
}
然后拨打这样的电话:
Call<ResponseBody> call = RetrofitClient.getInstance().create(ApiService.class).requestEditProducts(token, requestBody, id);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Log.d("PATCH_REQUEST", "code: " + response.code() +
" body: " + response.body());
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
t.printStackTrace();
}
});
最后,经过大量测试,我们发现由于ISP代理问题而发生错误。它会导致身体的补丁方法出现问题。