asynctask中的postexecute没有执行

时间:2013-09-04 09:17:23

标签: android android-asynctask

@Override
protected String doInBackground(String... params) {
    // TODO Auto-generated method stub
    url3="http://www.digitalmarketingbox.com/webtool/boxdata/"+MainActivity.abc+"/updates.zip";

    try {
        HttpURLConnection.setFollowRedirects(false);
        // note : you may also need
        //        HttpURLConnection.setInstanceFollowRedirects(false)
        HttpURLConnection con =(HttpURLConnection) new URL(params[0]).openConnection();
        con.setRequestMethod("HEAD");
        x=con.getResponseCode() == HttpURLConnection.HTTP_OK;
      }
      catch (Exception e) {
         e.printStackTrace();
         x=false;
      }


    try {
        HttpURLConnection.setFollowRedirects(false);
        // note : you may also need
        //        HttpURLConnection.setInstanceFollowRedirects(false)
        HttpURLConnection con =(HttpURLConnection) new URL(url3).openConnection();
        con.setRequestMethod("HEAD");
        xc=con.getResponseCode() == HttpURLConnection.HTTP_OK;
      }
      catch (Exception e) {
         e.printStackTrace();
         xc=false;
      }

    //x=exists(params[0]);
    //xc=exists(url3);
    valve=String.valueOf(x);

    if(valve.equals("true"))
    {

    finalurl=url+ MainActivity.abc + "/resources.zip";
    filename="resources.zip";
    MainActivity.fl1=filename;
    Log.i("filename",filename);
    }

    else if(xc==true) 
    {
    finalurl=url+ MainActivity.abc + "/updates.zip";    
    filename="updates.zip";
    MainActivity.fl1=filename;
    Log.i("filename",filename);
    }

    else 
    {
    try
    {
        HttpClient client = new DefaultHttpClient();  
         Log.i("url for checking update",url1);
         HttpPost post = new HttpPost(url1); 
         List<NameValuePair> parameters = new ArrayList<NameValuePair>();
         parameters.add(new BasicNameValuePair("reqtype", "resendcontent"));
         parameters.add(new BasicNameValuePair("menuboxid",MainActivity.abc));
         parameters.add(new BasicNameValuePair("updatetoken", "dmb20101512"));
         UrlEncodedFormEntity ent;
         ent = new UrlEncodedFormEntity(parameters,HTTP.UTF_8);
         post.setEntity(ent);
         HttpResponse responsePOST = client.execute(post);  
         HttpEntity resEntity = responsePOST.getEntity(); 
         finalurl=url+ MainActivity.abc + "/updates.zip";   
        filename="updates.zip";
        MainActivity.fl1=filename;
        Log.i("filename",filename);
        Log.i("url",finalurl);
      }
     catch (final IOException e) {      
            e.printStackTrace();
        }
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finalurl=url+ MainActivity.abc + "/updates.zip";    
    filename="updates.zip";
    MainActivity.fl1=filename;
    }

    try{
        URL url = new URL(finalurl);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);
        urlConnection.connect();            
        //File SDCardRoot = Environment.getExternalStorageDirectory();
        filedestination=Environment.getExternalStorageDirectory() +"/" + filename;
        //File file = new File(SDCardRoot,filename);
        File file = new File(filedestination);
        FileOutputStream fileOutput = new FileOutputStream(file);
        InputStream inputStream = urlConnection.getInputStream();
        totalSize = urlConnection.getContentLength();
        Log.i("size",String.valueOf(totalSize));
        byte[] buffer = new byte[1024];
        int bufferLength = 0;
        while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
            fileOutput.write(buffer, 0, bufferLength);
            downloadedSize += bufferLength;

            }
        //close the output stream when complete //
        fileOutput.close();
        }
        catch (final IOException e) {       
            e.printStackTrace();
        }
        Log.i("download info","Download complete");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String source = Environment.getExternalStorageDirectory() + "/" + filename;
        Log.i("Download destination",source);
        String destination =Environment.getExternalStorageDirectory()+ "/";  
        Log.i("Extract destination",destination);
        try {  
          ZipFile zipFile = new ZipFile(source);    
          zipFile.extractAll(destination);  
        } catch (ZipException e) {  
          e.printStackTrace(); 
        }

        try
        {
         HttpClient client1 = new DefaultHttpClient(); 
            Log.i("url is",url1);   
            HttpPost post1 = new HttpPost(url1); 
            List<NameValuePair> parameters1 = new ArrayList<NameValuePair>();
            parameters1.add(new BasicNameValuePair("reqtype", "completeupdate"));
            parameters1.add(new BasicNameValuePair("menuboxid",MainActivity.abc));
            parameters1.add(new BasicNameValuePair("updatetoken", "dmb20101512"));
            UrlEncodedFormEntity ent1 = new UrlEncodedFormEntity(parameters1,HTTP.UTF_8);
            post1.setEntity(ent1);   
            }

         catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
return foo;
}

@Override
protected void onPostExecute(String result) {
    // TODO Auto-generated method stu
super.onPostExecute(result);
//result=bn;
/*if(!result.isEmpty())
{
Calendar c = Calendar.getInstance(); 
int seconds = c.get(Calendar.SECOND);
Log.i("AsyncTask finish time",String.valueOf(seconds));
}*/

}

我在我的活动中使用这个asynctask类来做一些任务并通过getstatus方法检查它已经完成或者没有完成,但它从未完成 上面是我的asynctask类的代码,它的postexecute没有用,有人可以告诉我为什么吗?

1 个答案:

答案 0 :(得分:0)

我也遇到过同样的问题。它发生在您的主UI线程进入休眠状态时。尝试在UI线程中使用计时器使其保持清醒状态。