Android Show加载对话框进入AsyncTask

时间:2014-12-09 15:32:10

标签: java android xml

我希望在使用AsyncTask将Json数据加载到JsonObject之前显示加载对话框。

使用AsyncTask并从另一个类(片段)调用它的问题..

所以我在片段活动中创建了public static ProgressDialog dialog ;(使用它作为全局变量)

然后调用异步任务:

    JSONParser jsonParser = new JSONParser();

    jsonParser.execute(url,this);

    jObj = jsonParser.get();

这是JSONParser(Asynch任务)

   public class JSONParser extends AsyncTask<Object, Integer, JSONObject>  {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
private MovieListBoxOffice act; // The fragment Activity 
// constructor
public JSONParser() {
     super();
}
ProgressDialog dialog;

@Override
protected void onPreExecute() {
    super.onPreExecute();
    dialog = act.dialog;
    dialog.setMessage("Loading...");

     dialog.setCancelable(true);
     dialog.show();
}


@Override
protected JSONObject doInBackground(Object... params) {


       // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();

        HttpGet httpGet = new HttpGet((String)params[0]);

        HttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
            System.out.println(line);
        }
        is.close();
        json = sb.toString();

    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
        System.out.println("error on parse data in jsonparser.java");
    }


    return jObj;
    }


protected void onPostExecute(String result) {

    dialog.dismiss();
}

结果是加载对话框在加载数据之前不会显示(在异步任务完成之后)!!!

我怀疑jsonParser.get();是否会导致问题,如何在不将我的代码从片段类更改为postexecute的情况下管理该问题,

2 个答案:

答案 0 :(得分:0)

我会将ProgressDialog与Async Class本身联系起来,并使用getActivity()使用Async类的构造函数从此片段传递上下文。

.....//Fragment Code//......

new MyAsyncTask(getActivity(),....).execute(...);


....//Async Task Code//......

class MyAsyncTask extends AsyncTask<P1,P2,P3>
{
   Context context;
   ProgressDialog pDialog;
    .......
    public MyAsyncTask(Context ctx,...)
     {
        this.context = ctx;

        .....
      }

    onPreExecute()
     {
         pDialog = new ProgressDialog(context); 
       ...configure your ProgressDialog here
      }


}

答案 1 :(得分:0)

这是解决方案,

private ProgressDialog progressDialog;

@Override
protected void onPreExecute(){ 
        super.onPreExecute();
        // Create & Start Progress Dialog
        progressDialog = new ProgressDialog(yourContext);
        progressDialog.setMessage("Loading...");
        progressDialog.show();    
}

protected Boolean doInBackground(String... args) {
 // do something on non ui thread.
}

@Override
protected void onPostExecute(String result){
   super.onPostExecute(result);
    // Terminate Progress Dialog
   progressDialog.dismiss();
}