Android HTTPS con.getResponseCode() 在大请求的 readUtf8LineStrict 上抛出 EndOfFile 异常
Android HTTPS con.getResponseCode() throws EndOfFile exception on readUtf8LineStrict in big requests
问题
我有一个 post 请求仅在要恢复的数据很长时才导致错误(json 格式的数据约为 60Mb)。
相同的请求,但信息较少,不会导致任何错误,而且运行完美。
总是在发出请求后恰好 2 分钟出现的错误消息如下:
java.io.EOFException
at com.android.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:98)
at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:202)
at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:119)
at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:798)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:405)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:349)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:517)
at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:25)
at com.myproject.mypackage.DataManagerService$ServiceHandler.sincronitzar_baixada_dades(DataManagerService.java:1406)
at com.myproject.mypackage.DataManagerService$ServiceHandler.handleMessage(DataManagerService.java:403)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.os.HandlerThread.run(HandlerThread.java:61)
在服务器端,我们发现在我的设备出现错误后服务正在准备数据仍然存在。
几天前,我们增加了服务器上的 RAM,以避免同一个 casuistry 中的 OOM 错误。
2016 年 3 月 6 日更新了更多代码
还附上发出请求并将响应保存在设备文件中的代码:
请求代码:
HttpsURLConnection con = (HttpsURLConnection) Https_url.openConnection();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1){
con.setRequestProperty("Connection", "close");
}
con.setReadTimeout(DEFAULT_RESPONSE_TIMEOUT);
con.setConnectTimeout(DEFAULT_RESPONSE_TIMEOUT);
con.setRequestMethod("POST");
con.setRequestProperty("Accept-Encoding","identity");
con.setDoInput(true);
con.setDoOutput(true);
con.connect();
int status = con.getResponseCode();
if (status >= 400) {
inputStreamPujada = con.getErrorStream();
}
else{
inputStreamPujada = con.getInputStream();
}
if (inputStreamPujada != null){
try {
OutputStream out = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len;
while((len=in.read(buf))>0){ <--// THIS LINE IS THROWING EOFExceptiono error!
out.write(buf,0,len);
}
out.close();
in.close();
}
catch (Exception e) { e.printStackTrace(); }
}
我们尝试了什么?
- 我们尝试格式化 json 没有隔断线的输出,并且还使用漂亮的 json 格式。
- 看来错误是因为DataStream没有找到“\n”并抛出EndOfFile异常
- 还尝试提高服务器端的内存 RAM,但没有效果。
- 还尝试编辑请求 属性 "Accept-Encoding" as this question
问题
此 EOFException 的原因是什么?为什么要处理低数据并将其放入大数据中?是否有一些 extrange 超时或库限制,最大时间在 2 分钟内?
提前致谢!
经过大量研究,我们修改了服务器端,以便在 分页系统中为 JSON 文件的每次刷新 发送 5000 条记录的数据量。
服务器似乎被寻址数据阻塞,导致请求以 EOFException 结束,如问题中所示。
我希望有一天这个问题对其他人有用。
谢谢!
问题
我有一个 post 请求仅在要恢复的数据很长时才导致错误(json 格式的数据约为 60Mb)。
相同的请求,但信息较少,不会导致任何错误,而且运行完美。
总是在发出请求后恰好 2 分钟出现的错误消息如下:
java.io.EOFException
at com.android.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:98)
at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:202)
at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:119)
at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:798)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:405)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:349)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:517)
at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:25)
at com.myproject.mypackage.DataManagerService$ServiceHandler.sincronitzar_baixada_dades(DataManagerService.java:1406)
at com.myproject.mypackage.DataManagerService$ServiceHandler.handleMessage(DataManagerService.java:403)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.os.HandlerThread.run(HandlerThread.java:61)
在服务器端,我们发现在我的设备出现错误后服务正在准备数据仍然存在。
几天前,我们增加了服务器上的 RAM,以避免同一个 casuistry 中的 OOM 错误。
2016 年 3 月 6 日更新了更多代码
还附上发出请求并将响应保存在设备文件中的代码:
请求代码:
HttpsURLConnection con = (HttpsURLConnection) Https_url.openConnection();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1){
con.setRequestProperty("Connection", "close");
}
con.setReadTimeout(DEFAULT_RESPONSE_TIMEOUT);
con.setConnectTimeout(DEFAULT_RESPONSE_TIMEOUT);
con.setRequestMethod("POST");
con.setRequestProperty("Accept-Encoding","identity");
con.setDoInput(true);
con.setDoOutput(true);
con.connect();
int status = con.getResponseCode();
if (status >= 400) {
inputStreamPujada = con.getErrorStream();
}
else{
inputStreamPujada = con.getInputStream();
}
if (inputStreamPujada != null){
try {
OutputStream out = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len;
while((len=in.read(buf))>0){ <--// THIS LINE IS THROWING EOFExceptiono error!
out.write(buf,0,len);
}
out.close();
in.close();
}
catch (Exception e) { e.printStackTrace(); }
}
我们尝试了什么?
- 我们尝试格式化 json 没有隔断线的输出,并且还使用漂亮的 json 格式。
- 看来错误是因为DataStream没有找到“\n”并抛出EndOfFile异常
- 还尝试提高服务器端的内存 RAM,但没有效果。
- 还尝试编辑请求 属性 "Accept-Encoding" as this question
问题
此 EOFException 的原因是什么?为什么要处理低数据并将其放入大数据中?是否有一些 extrange 超时或库限制,最大时间在 2 分钟内?
提前致谢!
经过大量研究,我们修改了服务器端,以便在 分页系统中为 JSON 文件的每次刷新 发送 5000 条记录的数据量。
服务器似乎被寻址数据阻塞,导致请求以 EOFException 结束,如问题中所示。
我希望有一天这个问题对其他人有用。 谢谢!