0

我有一个异步任务让我从网络上获取一些数据。异步任务工作正常,我希望在从网络获取数据时显示进度对话框微调器。进度对话框微调器永远不会出现。这是我的代码:

public class JsonHttpParsingActivity extends ListActivity{
private String jsonResult;
private ArrayList nameArray;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    HttpConnection task = new HttpConnection(this);
    AsyncTask<String,Void,String> taskResult = task.execute("Some URL...");
    try {
        jsonResult = taskResult.get();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
            .
            .
            More Code.....
}
}

public class HttpConnection extends AsyncTask<String, Void, String> {
private ProgressDialog progressDialog;
private Activity m_activity;

protected HttpConnection(Activity activity) {
    setActivity(activity);
  }

  public void setActivity(Activity activity) {
    m_activity = activity;

    progressDialog = new ProgressDialog(m_activity);
    progressDialog.setMessage("Wait ...");
    progressDialog.setCancelable(false);
    progressDialog.setMax(100);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progressDialog.show();
  }

@Override
protected String doInBackground(String... params) {
    BufferedReader in = null;
    String inputLine= "", finalMessage = "";
    HttpURLConnection urlConnection = null;

    try {
        String urladdress = params[0];
        URL url = new URL(urladdress);
        urlConnection = (HttpURLConnection) url.openConnection();
        in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

        while((inputLine = in.readLine()) != null){
            finalMessage = finalMessage + inputLine;
        }
        in.close();
        Log.v("finalmessage", ""+finalMessage);
    }
    catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    finally {
        urlConnection.disconnect();
    }       
    return finalMessage;
}
protected void onProgressUpdate(Integer... values) {
     progressDialog.setProgress((int) ((values[0] / (float) values[1]) * 100));
     };

@Override 
protected void onPostExecute(String result){
      progressDialog.hide();
}
}

谢谢!

4

4 回答 4

1

从 onPreExecute() 函数调用进度条

于 2012-04-27T06:58:38.303 回答
1

而不是编写一个单独的方法setActivity(activity)(非 UI 线程范围)来启动ProgressDialog将代码放入onPreExecute()(UI 线程)中AsyncTask,因为您试图在非 UI 线程中显示它。

尝试这个,

protected HttpConnection(Activity activity) {
   m_activity = activity;
  }

Override 
protected void onPreExecute(String result){
    progressDialog = new ProgressDialog(m_activity);
    progressDialog.setMessage("Wait ...");
    progressDialog.setCancelable(false);
    progressDialog.setMax(100);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progressDialog.show();

}
于 2012-04-27T06:55:06.187 回答
0

以下代码工作正常并经过测试:

    public class HttpConnection extends AsyncTask<String, Void, String> {
    private ProgressDialog progressDialog;
    private Activity m_activity;

    protected HttpConnection(Activity activity) {

      }

      @Override
    protected void onPreExecute() {
    // TODO Auto-generated method stub
    progressDialog = new ProgressDialog(m_activity);
        progressDialog.setMessage("Wait ...");
        progressDialog.setCancelable(false);
        progressDialog.setMax(100);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.show();

    super.onPreExecute();
}

    @Override
    protected String doInBackground(String... params) {
        BufferedReader in = null;
        String inputLine= "", finalMessage = "";
        HttpURLConnection urlConnection = null;

        try {
            String urladdress = params[0];
            URL url = new URL(urladdress);
            urlConnection = (HttpURLConnection) url.openConnection();
            in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

            while((inputLine = in.readLine()) != null){
                finalMessage = finalMessage + inputLine;
            }
            in.close();
            Log.v("finalmessage", ""+finalMessage);
        }
        catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            urlConnection.disconnect();
        }       
        return finalMessage;
    }


    @Override 
    protected void onPostExecute(String result){
          progressDialog.hide();
    }
    }
于 2012-04-27T07:08:37.763 回答
0

尝试从活动中的另一个方法调用 AsyncTask。我的猜测是,现在,您在活动的 onCreate 方法中调用它。由于活动仍在建设中,这可能会给您带来例外。当我遇到这个问题时,我曾经尝试过的一件事是从活动的 onPostCreate 方法启动异步任务。

于 2013-01-27T13:04:18.513 回答