我有一个AsyncTask来从我服务器上的数据库中获取JSON数据。它运行正常,但有时失败,出现以下错误。这似乎发生在我保持应用程序空闲但打开一段时间然后提出请求时。
doInBackground方法
<?php
include ('db.php');
$sql = "SELECT id FROM users WHERE email = ?"."AND password = ?";
$stmt = $conn->prepare($sql);
$stmt->blind_param('ss',$_POST['email'],$_POST['password']);
$stmt->execute();
$stmt->blind_result($id);
if($stmt->fetch())
{
echo 'loggin in';
} else {
echo 'try again';
}
错误中的这一行
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();
额外信息 - 这不是应用程序中唯一的Asynctask。其他活动和片段还有更多。例如 - 如果活动2的AsynckTask抛出此错误,那么如果我回到活动1,那么它的AsyncTask也不起作用。
我应该在这里或服务器端做些什么吗?感谢这里的任何帮助。