小编Bot*_*ito的帖子

流改造意外结束

我有一个 Android 应用程序,它向 Flask 中的 REST API 发出 http 请求。我使用 Retrofit2 和 okhttp3 向带有 Raspbian Lite 的 Raspberry Pi 上托管的服务器发出请求。我的问题是,有时我会收到 IOException -> java.net.ProtocolException: 意外的流结束 但它有时会发生,其他时候它工作得很好。http客户端构建如下:

OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(new Interceptor() {
        @NotNull
        @Override
        public Response intercept(@NotNull Chain chain) throws IOException {
            Request original = chain.request();

            Request request = original.newBuilder()
                    .header("User-Agent","myUserAgent")
                    .header("Connection","close")
                    .addHeader("Accept-Encoding", "identity")
                    .method(original.method(),original.body())
                    .build();

            return chain.proceed(request);
        }
    });
Retrofit retrofit=null;
    try {
         retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .client(httpClient.build())
                .build();

    }catch (Exception e){
         //Log.d

    }
ApiService API_SERVICE=null; …
Run Code Online (Sandbox Code Playgroud)

android ioexception okhttp retrofit2

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

android ×1

ioexception ×1

okhttp ×1

retrofit2 ×1