Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从服务器向android应用程序获取大量数据_Android_Json_Http_Servlets_Connection - Fatal编程技术网

从服务器向android应用程序获取大量数据

从服务器向android应用程序获取大量数据,android,json,http,servlets,connection,Android,Json,Http,Servlets,Connection,我试图从服务器到安卓应用程序获取一个大的Json字符串 我尝试了很多方法,但一旦数据太大,我就开始面临很多问题 现在我设法获得了数据,当我试图从HttpEntity获取字符串时遇到了一个问题。但在大约30秒的等待后,我得到了下面的警告。我的数据丢失了 02-25 10:51:08.261: W/System.err(13528): java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer) 02

我试图从服务器安卓应用程序获取一个大的Json字符串

我尝试了很多方法,但一旦数据太大,我就开始面临很多问题

现在我设法获得了数据,当我试图从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");
知道这是什么原因吗?有没有更好的方法从服务器获取一个巨大的Json字符串到android手机并对其进行解析


多亏了的,我现在解决了这个问题

而不是使用:

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);

希望这有帮助。

这是服务器的故障-每次请求后都必须关闭连接。可能是android保留了一个连接池,并使用了旧的连接池。

检查这一个,谢谢,我使用了链接中建议的带有BufferedInputStream的HttpURLConnection,现在连接没有关闭,我设法获取了数据。