我有一个AsyncTask可以从服务器上的数据库中获取JSON数据。它可以正常工作,但有时会出现以下错误而失败。当我保持应用程序空闲但打开一段时间并发出请求时,似乎发生了这种情况。
doInBackground方法
try {
URL url = new URL(url_get_initial_posts);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
String data_string_language = URLEncoder.encode("selected_language","UTF-8")+"="+URLEncoder.encode(arg_language,"UTF-8")
+"&"+URLEncoder.encode("app_time","UTF-8")+"="+URLEncoder.encode(String.valueOf(arg_app_time),"UTF-8");
bufferedWriter.write(data_string_language);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
while ((JSON_INITIAL_POST = bufferedReader.readLine()) != null) {
stringBuilder.append(JSON_INITIAL_POST + "\n");
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return stringBuilder.toString().trim();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
此行中的错误
W/System.err: at com.indiparent.android.indiparent.PostTab$BackgroundJSONPosts.doInBackground(PostTab.java:343)
链接到
InputStream inputStream = httpURLConnection.getInputStream();
错误
W/System.err: java.net.SocketException: Connection reset
W/System.err: at java.net.SocketInputStream.read(SocketInputStream.java:209)
W/System.err: at java.net.SocketInputStream.read(SocketInputStream.java:139)
W/System.err: at com.android.okhttp.okio.Okio$2.read(Okio.java:136)
W/System.err: at com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:211)
W/System.err: at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:306)
W/System.err: at com.android.okhttp.okio.RealBufferedSource.indexOf(RealBufferedSource.java:300)
W/System.err: at com.android.okhttp.okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196)
W/System.err: at com.android.okhttp.internal.http.Http1xStream.readResponse(Http1xStream.java:186)
W/System.err: at com.android.okhttp.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:127)
W/System.err: at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:737)
W/System.err: at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:609)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:471)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:244)
W/System.err: at com.indiparent.android.indiparent.PostTab$BackgroundJSONPosts.doInBackground(PostTab.java:343)
W/System.err: at com.indiparent.android.indiparent.PostTab$BackgroundJSONPosts.doInBackground(PostTab.java:308)
W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:333)
W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:266)
W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
W/System.err: at java.lang.Thread.run(Thread.java:764)
额外信息-这不是应用程序中唯一的Asynctask。其他活动和片段还有更多。例如-如果活动2的AsynckTask抛出此错误,则如果我回到活动1,则其AsyncTask也将不起作用。
我应该在这里还是在服务器端执行某些操作?在这里感谢任何帮助。
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.connect();
OutputStream outputStream = conn.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
String post_data = URLEncoder.encode("ln", "UTF-8") + "=" + URLEncoder.encode(Constants.ln, "UTF-8")
+ "&"+URLEncoder.encode("size", "UTF-8") + "=" + URLEncoder.encode(""+ Constants.size, "UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream in = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String row = "";
Log.d("post",post_data);
while ((row = br.readLine()) != null) {
sb.append(row + "\n");}
br.close();
in.close();
Log.d("post",sb.toString());
conn.disconnect();
if(sb.toString().length()<20)Constants.iter=false;
if (!sb.toString().equals(""))GetJson(sb.toString());
Thread.currentThread().interrupt();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch enter code here(IOException e) {
e.printStackTrace();
}
}
}) ;
if(con==0) get_data.start();
}