2014-02-02 4 views
0

Я хочу отклонить мой progressdialog, который начался в конструкторе в postexecute. Но postexecute не может быть выполнен. Я попытался напечатать сообщение в doInBackground(), но даже это не было напечатано .Однако он отлично работает, когда я не показываю диалог. Пожалуйста, помогите, спасибо.AsyncTask не работает во время progressDialog spinning

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

Как вы начинаете задание «MyJsonParser»? –

+0

новый MyJsonParser (MainActivity.this) .execute(); – Hikmat

+0

Итак, если 'ProgressDialog.show' находится в конструкторе doInBackground не называется? –

ответ

0

Как насчет показать диалог прогресса в onPreExecute() вместо конструктора, onPreExecute() работает в UI потоке.

@Override 
protected void onPreExecute() 
{  
    System.out.println("onPreExecuteeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"); 
    pDialog = ProgressDialog.show(activity, "Loading", "Please wait..."); 
    super.onPreExecute(); 
} 
+0

конструктор, называемый там новым новым MyJsonParser (MainActivity.this), так или иначе я тоже пробовал PreExecute, он не работает – Hikmat

Смежные вопросы