我想解雇我在postexecute中的构造函数中启动的progressdialog.But postexecute无法执行。我试图在doInBackground()方法中打印一条消息,但即使那样也没有打印。但是当我不这样做时它工作正常没有显示对话框。请帮助,谢谢。
public class MyJsonParser extends AsyncTask<Void, Integer, List<Category>>
{
private static final String categoriesUrl = "some_url1";
private static final String postsUrl = "some_url2";
private List<Category> categories = new ArrayList<Category>();
private Context context;
private ProgressDialog pDialog;
private Activity activity;
public MyJsonParser(Context context)
{
this.context = context;
this.activity = (Activity) context;
pDialog = ProgressDialog.show(context, "Loading", "Please wait...");
}
@Override
protected List<Category> doInBackground(Void... params)
{
HttpClient client = new DefaultHttpClient();
HttpGet get1 = new HttpGet(categoriesUrl);
HttpResponse response1;
try
{
System.out.println("doInBackground");
response1 = client.execute(get1);
StatusLine statusLine = response1.getStatusLine();
if (statusLine.getStatusCode() == 200)
{
try
{
HttpEntity entity = response1.getEntity();
InputStream content = entity.getContent();
Reader reader = new InputStreamReader(content);
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonObject jsObject = parser.parse(reader)
.getAsJsonObject();
JsonElement jsElement = jsObject.get("categories");
Type type = new TypeToken<List<Category>>() {}.getType();
categories = gson.fromJson(jsElement, type);
content.close();
} catch (JsonIOException ex)
{
System.out.println("JsonIOException");
} catch (JsonParseException ex)
{
System.out.println("JsonIOException");
}
}
} catch (ClientProtocolException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
for(int i=0;i<categories.size();i++)
{
List<Post> posts = new ArrayList<Post>();
HttpGet get2 = new HttpGet(postsUrl+categories.get(i).getId());
HttpResponse response2;
try
{
response2 = client.execute(get2);
StatusLine statusLine = response2.getStatusLine();
if (statusLine.getStatusCode() == 200)
{
try
{
HttpEntity entity = response2.getEntity();
InputStream content = entity.getContent();
Reader reader = new InputStreamReader(content);
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonObject jsObject = parser.parse(reader)
.getAsJsonObject();
JsonElement jsElement = jsObject.get("posts");
Type type = new TypeToken<List<Post>>(){}.getType();
posts = gson.fromJson(jsElement, type);
content.close();
} catch (JsonIOException ex)
{
System.out.println("JsonIOException");
} catch (JsonParseException ex)
{
System.out.println("JsonIOException");
}
}
} catch (ClientProtocolException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
categories.get(i).setPosts(posts);
//publishProgress((int)progressbar.getMax()/i);
}
return categories;
}
@Override
protected void onPreExecute()
{
System.out.println("onPreExecuteeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
//activity.findViewById(R.id.progressBar1);
//activity.setProgress(0);
//pDialog.setMessage("Plz wait");
// pDialog.show();
super.onPreExecute();
}
@Override
protected void onPostExecute(List<Category> result)
{
Intent intent = new Intent(context, BaseContent.class);
intent.putExtra("categories", (ArrayList<Category>) result);
context.startActivity(intent);
pDialog.dismiss();
activity.finish();
super.onPostExecute(result);
}
}
答案 0 :(得分:0)
如何在onPreExecute()而不是构造函数中显示进度对话框,onPreExecute()在UI线程中运行。
@Override
protected void onPreExecute()
{
System.out.println("onPreExecuteeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
pDialog = ProgressDialog.show(activity, "Loading", "Please wait...");
super.onPreExecute();
}