1
我想通過多部分上傳圖像進行翻新。我經常得到以下例外。Retrofit多部分圖像上傳問題retrofit.RetrofitError java.io.InterruptedIOException
Caused by: retrofit.RetrofitError: timeout
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:395)
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
at $Proxy1.uploadImage(Native Method)
at gaadi.evaluator.com.retrofit.RetrofitRequest.makeImageUploadRequest(RetrofitRequest.java:24)
at gaadi.evaluator.com.service.RetrofitCarImageUploadService.onHandleIntent(RetrofitCarImageUploadService.java:59)
... 4 more
Caused by: java.io.InterruptedIOException: timeout
at okio.AsyncTimeout.exit(AsyncTimeout.java:258)
at okio.AsyncTimeout$1.write(AsyncTimeout.java:158)
at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:176)
at okio.RealBufferedSink.write(RealBufferedSink.java:46)
at com.squareup.okhttp.internal.http.HttpConnection$FixedLengthSink.write(HttpConnection.java:302)
at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:176)
at okio.RealBufferedSink$1.write(RealBufferedSink.java:198)
at java.io.OutputStream.write(OutputStream.java:82)
at retrofit.mime.TypedByteArray.writeTo(TypedByteArray.java:66)
at retrofit.client.OkClient$1.writeTo(OkClient.java:88)
at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:898)
at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:760)
at com.squareup.okhttp.Call.getResponse(Call.java:274)
at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:230)
at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:201)
at com.squareup.okhttp.Call.execute(Call.java:81)
at retrofit.client.OkClient.execute(OkClient.java:53)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:326)
... 8 more
Caused by: java.net.SocketException: Socket closed
at libcore.io.Posix.sendtoBytes(Native Method)
at libcore.io.Posix.sendto(Posix.java:151)
at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:177)
at libcore.io.IoBridge.sendto(IoBridge.java:517)
at java.net.PlainSocketImpl.write(PlainSocketImpl.java:511)
at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:46)
at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:269)
at okio.Okio$1.write(Okio.java:78)
at okio.AsyncTimeout$1.write(AsyncTimeout.java:155)
版改裝庫的使用:改進:1.9.0 okhttp的版本中使用:okhttp:2.4.0
我使用REST改造適配器如下:
final OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setConnectTimeout(30, TimeUnit.SECONDS);
okHttpClient.setWriteTimeout(30, TimeUnit.SECONDS);
okHttpClient.setReadTimeout(30, TimeUnit.SECONDS);
return new RestAdapter.Builder()
.setEndpoint(urlBase)
.setLogLevel(RestAdapter.LogLevel.BASIC)
.setClient(new OkClient(okHttpClient))
.build();
用於製作請求的接口方法如下:
@Multipart
@POST("/{path}")
public ImageUploadResponse uploadImage(@Path("path") String path, @Part(Constants.EVALUATION_DATA) String map, @Part("certImg") TypedFile file);
Cal l製作這個接口的方法如下:
TypedFile file = new TypedFile("multipart/form-data", new File(imagePath));
String url = Utils.getWebServiceURL(UILApplication.getInstance());
return requestInterface.uploadDocumentImage(path, jsonParams, file);
另外我試着設置超時值從30到60。它沒有幫助!
任何幫助將不勝感激!
PS:此代碼在WiFi連接上正常工作,但在3G移動數據網絡上失敗。
如果你做30秒到60秒?您的請求內容是否太大? – kaitian521
@kaitian我也試過。但它沒有工作! – lakshay
你想給你詳細的網址和查詢,所以我可以在我的機器上測試 – kaitian521