我将以下代码放在doInBackground()
的另一个AsyncTask
中,其中HttpPostHandler
包含另一个AsyncTask
。然后handler.get()
继续加载。
任何人对此有任何想法?
这是关于线程的问题???
代码:
@Override
protected Void doInBackground(Void... params) {
try {
champ = Utility.getCounter("test");
this.wait(3);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
在实用程序中:
public static int getCounter(String code) {
HttpPostHandler handler = new HttpPostHandler();
try {
handler.execute(counter_URL + code);
handler.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
String html = handler.getResultJSONString();
TagNode tagNode;
...
}
以下是HttpPostHandler AsyncTask
public class HttpPostHandler extends AsyncTask<String, Void, String> {
private String url;
private String resultJSONString=null;
private JSONObject resultJSON;
public String getResultJSONString() {
return resultJSONString;
}
public void setResultJSONString(String resultJSONString) {
this.resultJSONString = resultJSONString;
}
private static String toUTF8(InputStream is){
//InputStream is = resEntity.getContent();
InputStreamReader isr = null;
StringBuffer buffer = new StringBuffer();
try {
isr = new InputStreamReader(is, "utf-8");
Reader in = new BufferedReader(isr);
int ch;
while((ch = in.read()) != -1){
buffer.append((char)ch);
}
isr.close();
is.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
return buffer.toString();
}
@Override
protected String doInBackground(String... params) {
String result = "";
DefaultHttpClient client = new DefaultHttpClient();
try {
HttpGet get = new HttpGet(params[0]);
HttpResponse response = client.execute(get);
Log.d("danny", "response = "+response);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = toUTF8(resEntity.getContent());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
client.getConnectionManager().shutdown();
}
setResultJSONString(result);
return result;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
resultJSONString = result;
}
}
答案 0 :(得分:1)
如果您在API 11(3.0)或更高版本中运行,则可以将handler.execute(counter_URL + code);
更改为handler.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, counter_URL + code);
这允许同时运行多个AsyncTasks。
答案 1 :(得分:1)
在doinbackground()中使用asynctask类的发布进度方法而不是直接调用“handler.execute”。
在onprogressupdate()中调用“handler.execute”并在doInBackground中调用发布进度。
答案 2 :(得分:1)
以下是发布进度的示例:
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
如果对您有帮助,请投票或标记为真。