我已经解决了与此相关的所有问题,但尚未找到适合我的解决方案。
我在用着retrofit 2.8.1
and OkHttp 4.5.0
.
我的服务界面如下所示
public interface MlApiService
{
@POST
@Multipart
Call<List<PreprocessedDocument>> postDocument( @Url String apiUrl, @Part MultipartBody.Part document,
@Part ( "document_id") RequestBody documentId );
}
我像下面这样构建客户端requestTimeoutInSeconds
设置为 90 秒。
public void init()
{
GsonBuilder gson = new GsonBuilder();
gson.registerTypeAdapter( new TypeToken<List<PreprocessedDocument>>() {}.getType(), new CustomResponseDeserializer() );
HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor();
logInterceptor.setLevel( HttpLoggingInterceptor.Level.HEADERS );
OkHttpClient client = new OkHttpClient.Builder().retryOnConnectionFailure( true ).addInterceptor( logInterceptor )
.readTimeout( requestTimeoutInSeconds, TimeUnit.SECONDS ).build();
//Dummy Base URL must be provided. otherwise client won't get initialized
Retrofit retrofit = new Retrofit.Builder().baseUrl( "http://thisIsJustDummyUrlForTheSakeOfAddingBaseUrl.com/" )
.client( client ).addConverterFactory( GsonConverterFactory.create( gson.setLenient().create() ) ).build();
mlApiService = retrofit.create( MlApiService.class );
}
请求到达服务器,当服务器响应时,我收到以下错误:
Caused by: java.io.IOException: unexpected end of stream on Connection{34.XXX.XXX.9:8085, proxy=DIRECT hostAddress=/34.XXX.XXX.9:8085 cipherSuite=none protocol=http/1.1}
at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:203)
at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
Caused by: java.io.EOFException: \n not found: limit=0 content=…
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:227)
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:211)
at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:187)
到目前为止我尝试过的几件事
- 重试连接失败(true)
- .addHeader("连接","关闭")
- .header("接受编码", "身份")
该 API 在邮递员中工作正常,但当我从代码中尝试时却失败了。所以我尝试了邮递员发送的相同标头。还是没有运气。
一些观察:
- 有时它会起作用。并不总是失败。(同一文件始终适用于邮递员)
- 它始终适用于其他文件(从来都不是问题)。
- 请求到达服务器并处理该请求,没有任何错误并做出响应。服务器完成处理并响应客户端后立即收到错误。
EDIT 1:我访问的服务器由gunicorn/20.0.4 支持并使用Flask。我无权访问服务器代码。我怀疑收到的发送响应中有一些垃圾字符导致错误。我不知道如何在被 Retrofit/okhttp 读取之前记录原始响应。
EDIT 2:
我以详细方式运行 Curl 命令,这就是我得到的结果。
- 服务器回复为空
- 与主机 xx.xxx.xxx.9 的连接 #0 保持不变
卷曲:(52)服务器的空回复