我在AsyncTask
中编写了此代码,但我不明白Exception
。谁能告诉我哪里错了?我不知道自己要做什么。以下是我AsyncTask
中的3种方法。
protected void onPreExecute() {
HttpParams params = httpClient.getParams();
if(script.contains("appscan.php"))
{
Params.printLog(TAG,"onPreExecute appscan.php script:"+script);
HttpConnectionParams.setConnectionTimeout(params, 10000);
HttpConnectionParams.setSoTimeout(params, 10000);
}
else
{
Params.printLog(TAG,"onPreExecute script:"+script);
HttpConnectionParams.setConnectionTimeout(params, 120000);
HttpConnectionParams.setSoTimeout(params, 120000);
}
}
protected String doInBackground(Void... urls) {
String st_response="";
try {
final HttpResponse response;
//synchronized (httpClient) {
if(request==null)
cancel(true);
String m_url;
if(isPost)
{
m_url=baseUrl+script;
HttpPost httppost = new HttpPost(m_url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
httppost.setEntity(new UrlEncodedFormEntity((List<NameValuePair>)request));
if(getClient()==null)
{
return "";
}
response = getClient().execute(httppost);
}
else
{
String m_request=request==null?"":Uri.encode(request.toString());
if(DEBUG)
m_url=baseUrl1+script+m_request;
else
m_url=baseUrl+script+m_request;
Params.printLog(TAG,"run :"+m_url);
HttpUriRequest request1=new HttpGet(m_url);
if(getClient()==null || request1==null)
{
return "";
}
response = getClient().execute(request1);
}
StringBuilder builder = new StringBuilder();
InputStream is=response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
for (String line = null; (line = reader.readLine()) != null;) {
builder.append(line).append("\n");
}
is.close();
st_response=builder.toString();
//}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnknownHostException e)
{
st_response="{\"httperr\":1}";
e.printStackTrace();
}
catch (IOException e) {
st_response="{\"httperr\":1}";
e.printStackTrace();
}
return st_response;
}
protected void onPostExecute(String content) {
if(o==null)
((ResponseListener)listener).onResponseReceived(content);
else
((ResponseWithObjectListener)listener).onResponseReceived(content,o);
}
当我尝试加载大量数据时,我收到此错误:
03-15 13:50:23.160: E/AndroidRuntime(4980): FATAL EXCEPTION: AsyncTask #4
03-15 13:50:23.160: E/AndroidRuntime(4980): java.lang.RuntimeException: An error occured while executing doInBackground()
03-15 13:50:23.160: E/AndroidRuntime(4980): at android.os.AsyncTask$3.done(AsyncTask.java:200)
03-15 13:50:23.160: E/AndroidRuntime(4980): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
03-15 13:50:23.160: E/AndroidRuntime(4980): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
03-15 13:50:23.160: E/AndroidRuntime(4980): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
03-15 13:50:23.160: E/AndroidRuntime(4980): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
03-15 13:50:23.160: E/AndroidRuntime(4980): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
03-15 13:50:23.160: E/AndroidRuntime(4980): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
03-15 13:50:23.160: E/AndroidRuntime(4980): at java.lang.Thread.run(Thread.java:1019)
03-15 13:50:23.160: E/AndroidRuntime(4980): Caused by: java.lang.OutOfMemoryError
03-15 13:50:23.160: E/AndroidRuntime(4980): at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95)
03-15 13:50:23.160: E/AndroidRuntime(4980): at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:153)
03-15 13:50:23.160: E/AndroidRuntime(4980): at java.lang.StringBuilder.append(StringBuilder.java:217)
03-15 13:50:23.160: E/AndroidRuntime(4980): at com.qriket.qriket.httpclient.HttpAsyncTask.doInBackground(HttpAsyncTask.java:126)
03-15 13:50:23.160: E/AndroidRuntime(4980): at com.qriket.qriket.httpclient.HttpAsyncTask.doInBackground(HttpAsyncTask.java:1)
03-15 13:50:23.160: E/AndroidRuntime(4980): at android.os.AsyncTask$2.call(AsyncTask.java:185)
03-15 13:50:23.160: E/AndroidRuntime(4980): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
由于
答案 0 :(得分:0)
看起来响应非常大,(字符串构建器无法构建整个字符串) 我建议将它存储在保存文件中,并在任务完成后以块的形式读取它。