【问题标题】:java.io.IOException when using Retrofit with rxandroid将 Retrofit 与 rxandroid 一起使用时出现 java.io.IOException
【发布时间】: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);
                    }
                });

我应该提到,所有其他请求 GETPOSTPUT 都在工作,但所有 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


    【解决方案1】:

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

    【讨论】:

    • 您能详细说明一下吗?
    • 在大多数情况下,问题出在服务器本身。您可以检查服务器是否正确响应?当问题出现时,尝试使用空响应添加一些测试用例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-12
    • 2021-02-28
    • 2017-10-19
    • 2017-12-04
    • 2022-01-04
    • 1970-01-01
    相关资源
    最近更新 更多