我正在尝试从服务器中获取大型 Json 字符串到 android 应用程序。
我尝试了很多方法但是一旦数据变得太大,我就开始遇到很多问题。
现在我设法获取数据,当我尝试从 HttpEntity 获取字符串时,我遇到了问题。但在等待大约30秒后,我会收到以下警告。我的数据丢失了。
02-25 10:51:08.261: W/System.err(13528): java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
02-25 10:51:08.281: W/System.err(13528): at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:545)
02-25 10:51:08.281: W/System.err(13528): at libcore.io.IoBridge.recvfrom(IoBridge.java:509)
02-25 10:51:08.281: W/System.err(13528): at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
02-25 10:51:08.281: W/System.err(13528): at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
02-25 10:51:08.281: W/System.err(13528): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
02-25 10:51:08.281: W/System.err(13528): at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
02-25 10:51:08.286: W/System.err(13528): at org.apache.http.impl.io.AbstractSessionInputBuffer.read(AbstractSessionInputBuffer.java:134)
02-25 10:51:08.286: W/System.err(13528): at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:161)
02-25 10:51:08.286: W/System.err(13528): at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:159)
02-25 10:51:08.286: W/System.err(13528): at java.io.InputStreamReader.read(InputStreamReader.java:244)
02-25 10:51:08.286: W/System.err(13528): at java.io.BufferedReader.fillBuf(BufferedReader.java:130)
02-25 10:51:08.286: W/System.err(13528): at java.io.BufferedReader.readLine(BufferedReader.java:390)
02-25 10:51:08.286: W/System.err(13528): at com.mss.annborgadmin.utils.NetworkConnectionHandler.convertStreamToFile(NetworkConnectionHandler.java:55)
02-25 10:51:08.291: W/System.err(13528): at com.mss.annborgadmin.tasks.HistoryTask.doInBackground(HistoryTask.java:120)
02-25 10:51:08.291: W/System.err(13528): at com.mss.annborgadmin.tasks.HistoryTask.doInBackground(HistoryTask.java:1)
02-25 10:51:08.291: W/System.err(13528): at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-25 10:51:08.291: W/System.err(13528): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-25 10:51:08.291: W/System.err(13528): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-25 10:51:08.291: W/System.err(13528): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-25 10:51:08.296: W/System.err(13528): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-25 10:51:08.296: W/System.err(13528): at java.lang.Thread.run(Thread.java:841)
02-25 10:51:08.296: W/System.err(13528): Caused by: libcore.io.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer)
02-25 10:51:08.301: W/System.err(13528): at libcore.io.Posix.recvfromBytes(Native Method)
02-25 10:51:08.301: W/System.err(13528): at libcore.io.Posix.recvfrom(Posix.java:140)
02-25 10:51:08.301: W/System.err(13528): at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
02-25 10:51:08.306: W/System.err(13528): at libcore.io.IoBridge.recvfrom(IoBridge.java:506)
02-25 10:51:08.306: W/System.err(13528): ... 19 more
这是我用来从输入流中获取数据的函数。
public static String convertStreamToFile(InputStream is, Context context)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try
{
while ((line = reader.readLine()) != null)
{
sb.append((line + "\n"));
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
String result = sb.toString();
File historyFile = FileHandler.openFile(context, "temp.txt");
FileHandler.writeData(result, historyFile);
return "done";
}
警告是关于连接错误,但是当我调用此函数时,我已经使用以下内容获得了响应的内容:
InputStream is = httpEntity.getContent();
Log.d("DEBUG", "CONTENT TO IS");
知道是什么原因引起的吗?是否有更好的方法从服务器到Android手机获取巨大的Json String并解析它?
而不是使用:
httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream is = httpEntity.getContent();
result = NetworkConnectionHandler.convertStreamToFile(is, context);
我现在用:
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
result = NetworkConnectionHandler.convertStreamToFile(in, context);
希望这有帮助。
答案 0 :(得分:0)
这是服务器的错 - 它必须在每次请求后关闭连接。可能是android保留了一个连接池并使用旧连接。