0

我正在尝试查看 android 中的 Asynctask 类是如何工作的。特别是我想实时显示课程的状态,以查看它何时运行以及何时完成。为此,我创建了一个扩展主要活动的类和另一个类,即 asynctaks 类。这是我的主要课程:

public class PhotoManagement extends Activity{
private String numberOfSelectedPhotos;
private Bitmap currentImage;
private String initConfiguration = "http://www.something.com";
private String response;
private ArrayList<String> formatPhotoList = new ArrayList<String>(); //create a list that will contains the available format of the photos downloaded from the server
private ArrayList<String> pricePhotoList = new ArrayList<String>(); //create a list that will contains the available price for each format of the photos
DownloadWebPageTask webPage = new DownloadWebPageTask();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
protected void onResume(){
    super.onResume();
    webPage.execute(initConfiguration);
    if(webPage.getStatus() == AsyncTask.Status.PENDING){   
        Log.i("STATUS","PENDING");
    }
    if(webPage.getStatus() == AsyncTask.Status.RUNNING){
        Log.i("","RUNNING");
    }
    if(webPage.getStatus() == AsyncTask.Status.FINISHED){
        Log.i("","FINISHED");
    }
}
}

如您所见,我只想通过简单的日志查看状态的段落。这里有 asynctask 类。

private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
        for (String url : urls) {
            DefaultHttpClient client = new DefaultHttpClient(); //create a new http client
            HttpGet httpGet = new HttpGet(url); //create a new http request passing a valid url
            try {
                HttpResponse execute = client.execute(httpGet); //try to execute the http get request
                InputStream content = execute.getEntity().getContent(); //prepare the input stream to read the bytes of the request
                BufferedReader buffer = new BufferedReader(
                        new InputStreamReader(content));
                String s = "";
                while ((s = buffer.readLine()) != null) {
                    response += s; //until is present a line to read, the response variable store the value of the lines
                }

            } catch (Exception e) {
                Log.i("MyApp", "Download Exception : " + e.toString()); //Print the error if something goes wrong
            }
        }
        return response; //return the response
    }

    @Override
    protected void onPostExecute(String result) {
        result = doInBackground(initConfiguration); //take the result from the DownloadWebPageTask class
        result = result.replace("null", "");
        Log.i("RESULT",""+result);
        //find the price and format value from the result using XmlPullParser
        try {
           XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
           factory.setNamespaceAware(true);
            XmlPullParser xpp = factory.newPullParser();
            xpp.setInput( new StringReader ( result ) );
            int attributeNumber = xpp.getAttributeCount();
            int eventType = xpp.getEventType();
            String currentTag = null;
            while(eventType != XmlPullParser.END_DOCUMENT){
                if(eventType == XmlPullParser.START_TAG) {
                    currentTag = xpp.getName();
                    if (currentTag.equals("product")){
                        xpp.getAttributeValue(null, "name");
                        formatPhotoList.add(xpp.getAttributeValue(null, "name"));
                        Log.i("FORMAT PHOTO",""+xpp.getAttributeValue(null, "name"));
                    }
                }
                eventType = xpp.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
            Log.i("","ERROR XML PULL PARSER");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.i("","ERROR IOEXCEPTION");
        }
    }

}   
}

如您所见,我还实现了 onPostExecute 方法,该方法应在 asynctask 方法完成执行指令时调用,对吗?所以此时我不明白为什么我的日志 RUNNING 和我的日志 FINISHED 从未出现在日志猫上。我做错了什么?我试图关注这个主题Android,AsyncTask,检查状态?但就我而言,它不起作用。

谢谢

4

4 回答 4

4

问题 :

您正在创建对象

DownloadWebPageTask webPage = new DownloadWebPageTask();

但是你在不同的对象上调用 asynctask,

new DownloadWebPageTask().execute(initConfiguration);

解决方案: 应该是这样的

webPage.execute(initConfiguration);
于 2013-10-03T10:40:41.413 回答
1

你没有实现webPage.execute(),添加它

于 2013-10-03T10:41:25.770 回答
1
@Override
protected void onResume(){
    super.onResume();
    new DownloadWebPageTask().execute(initConfiguration);

在这里做这样

@Override
protected void onResume(){
    super.onResume();
    webPage.execute(initConfiguration);
于 2013-10-03T10:39:41.373 回答
0

很可能任务还没有完成,甚至还没有开始。正如您可能知道的AsyncTask那样,它的(后台)工作在不同的线程上,因此您onResume正在与它并行运行。您可以使用任务的get()方法等待它完成并获取doInBackground()方法的结果,然后查询它的状态,或者从任务的onPostExecute()方法通知您的活动,让它知道(并记录)它已经完成。我不建议您使用第一个选项,因为它实际上会阻塞 UI 线程并使您对 AsyncTask 的使用毫无意义。

于 2013-10-04T13:33:03.440 回答