I'm trying to get a large Json String from a server to an android application.
I tried many ways but as soon as the data gets too large I start facing a lot of problems.
Now I managed to get data and I get a problem when I'm trying to get the String from the HttpEntity. But after approximately 30 seconds of waiting I get the warning below. And my data is lost.
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
Here's the function I'm using to get the data from the input stream.
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";
}
The warning is about a connection error, but When I call this function, I already got the response's content using:
InputStream is = httpEntity.getContent();
Log.d("DEBUG", "CONTENT TO IS");
Any idea what causes this? And is there a better way to get a huge Json String from a server to an android phone and parse it?
I fixed the issue now thanks to Gupta's link.
instead of using:
httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream is = httpEntity.getContent();
result = NetworkConnectionHandler.convertStreamToFile(is, context);
I now use:
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
result = NetworkConnectionHandler.convertStreamToFile(in, context);
Hope this helps.