使用rxandroid

时间:2016-12-23 13:03:28

标签: android retrofit rx-java rx-android

我需要向提供正文的服务器发出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();

My Retrofit服务方法是:

@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 正在运行,但所有删除请求从log:

返回错误
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)

1 个答案:

答案 0 :(得分:0)

这是一个问题。你可以在GitHub上看到它: https://github.com/square/okhttp/issues/1517