【发布时间】:2017-05-09 04:38:50
【问题描述】:
我需要向我的服务器发出 HTTP DELETE 请求并提供正文。
我通过以下方式构建改造对象:
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.retryOnConnectionFailure(true)
.connectTimeout(2, TimeUnit.MINUTES)
.readTimeout(5, TimeUnit.MINUTES)
.writeTimeout(5, TimeUnit.MINUTES)
.build();
Gson gson = new GsonBuilder()
.registerTypeAdapter(Date.class, new GsonUTCDateAdapter())
.registerTypeAdapter(LocalDate.class, new GsonLocalDateAdapter())
.create();
return new Retrofit.Builder()
.baseUrl(AppConfig.API_BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
我的改造服务方式是:
@HTTP(method = "DELETE", path = "selfie/{publicId}/action/1", hasBody = true)
Observable<SimpleResponseModel> unlike(
@Path("publicId") String publicId,
@Body AuthorisedRequestModel model
);
我通过以下方式处理改造请求/响应:
NetworkHelper
.getRetrofit()
.create(SelfieService.class)
.unlike(selfieModel.getPublicId(), new AuthorisedRequestModel())
.subscribeOn(Schedulers.io())
.subscribe(new CustomSubscriber<SimpleResponseModel>() {
@Override
public void onNext(SimpleResponseModel simpleResponseModel) {
ErrorHelper.handleServerError(simpleResponseModel);
}
});
我应该提到,所有其他请求 GET、POST 和 PUT 都在工作,但所有 DELETE请求从日志返回给我以下错误:
HTTP FAILED: java.io.IOException: unexpected end of stream on okhttp3.Address@c6ec992c
所以请求不会到达服务器。
当我在没有 rxandroid 的情况下使用 Retrofit 并在 AsyncTask 中进行查询时,一切正常。
原因:
Caused by: java.io.EOFException: \n not found: size=0 content=…
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:215)
at okhttp3.internal.http.Http1xStream.readResponse(Http1xStream.java:186)
【问题讨论】:
标签: android retrofit rx-java rx-android