我在我的 中写了这段代码AsyncTask
,但我不明白这一点Exception
。有人能告诉我哪里错了吗?我不知道我必须做什么。这是我的 3 种方法AsyncTask
。
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)
谢谢