0

I have an Async Task getting me some data from the web. Async Task works fine and I want a Progress Dialog Spinner to be displayed while the data is being procured from the web.The Progress Dialog Spinner never shows up. Here is my code:

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();
}
}

Thanks!

4

4 に答える 4

1

setActivity(activity)開始するための別のメソッド (非 UI スレッド スコープ) を記述する代わりに、非 UI スレッドで表示しようとしているためProgressDialog、コードを のonPreExecute()(UI スレッド) に配置します。AsyncTask

これを試して、

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 に答える
1

onPreExecute() 関数からプログレス バーを呼び出す

于 2012-04-27T06:58:38.303 に答える
0

アクティビティ内の別のメソッドからAsyncTaskを呼び出してみてください。私の推測では、今のところ、アクティビティのonCreateメソッドで呼び出しています。アクティビティはまだ構築中であるため、これにより例外が発生する可能性があります。この問題が発生したときに一度試したことは、アクティビティのonPostCreateメソッドから非同期タスクを開始することです。

于 2013-01-27T13:04:18.513 に答える
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 に答える