永远不会调用AsyncTask onPostExecute

时间:2014-08-04 15:00:23

标签: java android android-asynctask

我尝试添加覆盖注释,但没有使用它。

public class Details extends Activity {
String name = "";
String balance = "";

public class MyTask extends AsyncTask<Void, Void, String> {
    ProgressDialog progressDialog;

    protected void onPreExecute() {

        progressDialog = new ProgressDialog(Details.this);
        progressDialog.setMessage("Loading...");
        progressDialog.setCancelable(false);
        progressDialog.show();

    }

    @Override
    protected String doInBackground(Void... arg0) {

NetWork netWork = new NetWork();
SharedPreferences settings = getSharedPreferences("details", 0);
String user = settings.getString("user", "");
String pass = settings.getString("pass", "");

String s="";
try {
    s = netWork.getGradesHtml(
            "https://app.laundro-smart.com/Forms/frmMain.aspx", user, pass,
            "GET",0,3,0,"");
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}


int index = Jsoup
        .parse(s,
                "https://app.laundro-smart.com/Forms/frmMacineStateInRegion.aspx")
        .select("span#ctl00_ContentPlaceHolder1_lblPrePaidBalance")
        .toString().lastIndexOf("<");

 balance =Jsoup.parse(s,
                        "https://app.laundro-smart.com/Forms/frmMacineStateInRegion.aspx")
                        .select("span#ctl00_ContentPlaceHolder1_lblPrePaidBalance")
                        .toString().substring(55, index);
index = Jsoup
        .parse(s,
                "https://app.laundro-smart.com/Forms/frmMacineStateInRegion.aspx")
        .select("span#ctl00_lnkUsername").toString()
        .lastIndexOf("<");
name= Jsoup.parse(s,
                        "https://app.laundro-smart.com/Forms/frmMacineStateInRegion.aspx")
                        .select("span#ctl00_lnkUsername")
                        .toString().substring(58, index);
                        return name;    
    }
    @Override
    protected void onPostExecute(String aa) {

        TextView a = (TextView) findViewById(R.id.textView2);
        a.setText(aa);
        a =(TextView)findViewById(R.id.textView5);
        a.setText(balance);
        progressDialog.cancel();

    }

}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_details);
    (new MyTask()).execute();           


}

1 个答案:

答案 0 :(得分:0)

我有一个您编辑的示例代码并尝试使用此代码。此代码用于从Internet下载文件,一旦下载完成,它将进入其他活动

公共类asyncDownloader扩展了AsyncTask&lt; string,String,String&gt; {

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub

        String path=Environment
                .getExternalStorageDirectory().getPath() + "/SimonSong.mp3";
        Log.e("simon", "Path is "+path);
        Intent intent = new Intent();
        intent.setAction(Constants.BROAD);
        intent.putExtra("test", path);
        sendBroadcast(intent);

        super.onPostExecute(result);
    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        // Toast.makeText(this, "async class is called",
        // Toast.LENGTH_LONG).show();
        Log.e("simon", "async class is called");
        int count;
        try {
            URL url = new URL(params[0]);
            URLConnection conection = url.openConnection();
            conection.connect();

            int lenghtOfFile = conection.getContentLength();
            Log.e("simon", "download is started");
            InputStream input = new BufferedInputStream(url.openStream(),
                    10 * 1024);

            OutputStream output = new FileOutputStream(Environment
                    .getExternalStorageDirectory().getPath() + "/SimonSong.mp3");
            byte data[] = new byte[1024];
            long total = 0;
            Prog pg = new Prog();
            while ((count = input.read(data)) != -1) {
                total += count;

                int temp = (int) ((total * 100) / lenghtOfFile);                
                //Log.e("simon", "Downloading completed " + temp + " %");
                output.write(data, 0, count);
                pg.setUpdate(temp);
            }

            output.flush();
            output.close();
            input.close();




            //Log.e("simon", "download is finished");

        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        }

        return null;
    }

    @Override
    protected void onProgressUpdate(String... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
        //Log.e("simon","Value  :  "+values[0]);
    }


}