检查AsyncTask状态似乎无法正常工作(日志cat上没有显示日志)

时间:2013-10-03 10:35:00

标签: android android-asynctask

我试图看看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永远不会出现在log cat上。 我做错了什么? 我试图遵循这个主题Android, AsyncTask, check status?,但就我而言,它不起作用。

由于

4 个答案:

答案 0 :(得分:4)

问题:

您正在创建像

这样的对象
DownloadWebPageTask webPage = new DownloadWebPageTask();

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

new DownloadWebPageTask().execute(initConfiguration);

解决方案: 它应该像

webPage.execute(initConfiguration);

答案 1 :(得分:1)

@Override
protected void onResume(){
    super.onResume();
    new DownloadWebPageTask().execute(initConfiguration);

这样做

@Override
protected void onResume(){
    super.onResume();
    webPage.execute(initConfiguration);

答案 2 :(得分:1)

您没有实施webPage.execute(),添加

答案 3 :(得分:0)

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