2

我主要活动:安卓:ProgressDialog外部异步类(某块异步线程)

public class ChooseWriteSentenceActivity extends ActionBarActivity{ 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     String userName = "Zdzisiu"; 
     String password = "Ziemniak"; 
     MainServie service = new MainService(this); 
     boolean isExsist = service.findUser(String userName, String password); 
     //more code... 
    } 
} 

在我的应用程序服务使用存储库和jsonconsumers但简单的代码,我跳过他们。

public class MyService{ 
    private Context context; 
    public MyService(Context context){ 
     this.context = context 
    } 
    public boolean findUser(String userName, String password){ 
     String resultS = null; 
     try{ 
      resultS = new QueryExecutorFindUser(context).execute(userName,password).get(); 
     } 
     catch(Exception ex){ 
      ex.printStackTrace(); 
     } 
     boolean realRes = jsonConsumer(resultS).getFindUser(); 
     return realRes; 
    } 
} 

public class QueryExecutorFindUser extends AsyncTask<String,Void,String> { 


protected final String connectionUrl = "http://myWebService:44302/Service.svc/"; 

    protected ProgressDialog progressDialog; 
    protected Context curContext; 

    public QueryExecutor(Context context){ 
     curContext = context; 
    } 

    @Override 
    protected void onPreExecute() 
    { 
     super.onPreExecute(); 
     progressDialog = ProgressDialog.show(curContext,"Loading...", 
       "Loading application View, please wait...", false, false); 
    } 

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

    protected String doInBackground(String... args){ 
     String result = null; 
     String url = connectionUrl + args[0] + "/" + args[1]; 
     HttpResponse response = null; 
     HttpClient httpclient = this.getNewHttpClient(); 
     HttpGet get = new HttpGet(url); 
     get.setHeader("Accept", "application/json"); 
     get.setHeader("Content-type", "application/json"); 
     try{ 
      response = httpclient.execute(get); 
      StatusLine statusLine = response.getStatusLine(); 
      if(statusLine.getStatusCode() == HttpStatus.SC_OK){ 
       if(response != null){ 
        ByteArrayOutputStream out = new ByteArrayOutputStream(); 
        response.getEntity().writeTo(out); 
        out.close(); 
        result = out.toString(); 
       } 
      } else{ 
       throw new IOException(statusLine.getReasonPhrase()); 
      } 
     } catch(Exception ex){ 
      ex.getMessage(); 
     } finally{ 
      if(response != null){ 
       try{ 
        response.getEntity().getContent().close(); 
       } catch(Exception ex){ 
       } 
      } 
     } 

     return result; 
    } 
} 

而且进度对话框显示,但只有后onCreatre在ChooseWriteSentenceActivity包括QueryExecutor doInBacground(...)的所有代码完成(所以它在同一时间几乎消失)。它看起来像......等待与QueryExecutorFindUser.doInBackground()线程,它是像同步运行(?),我认为,因为当我调试代码onPreExecute()正确运行(并开始之前doInBackground(...))和progressDialog如果我从QueryExecutorFindUser中删除扩展AsyncTask,并在主要活动中使用此扩展创建私有类(并从包含服务的onCreated()运行所有代码。).isShowing()== true(但屏幕上不显示:()在这个私人的类.doInBackground(...))findUser()它工作okey。

我更喜欢progressDialog在一个地方没有在所有主要活动(实践中的cource的使用QueryExecutor所有查询不仅findUser)我不知道我做错了什么,我花了整整一天的时间没有结果:(

+0

尝试在onCreate而不是onPre上启动进度对话框 – 2014-12-05 14:52:16

+0

谢谢,但它没有帮助 – user3232354 2014-12-05 16:37:37

回答

1

对话框绑定到Activity并最终必须由一个托管。所以直到你的应用程序的活动被创建,对话框将不会显示。

+0

Okey我明白,所以只有当Activity直接运行异步线程时才有可能。在我的例子中,新线程从新对象运行 - 此对象的实例被阻止创建活动,对吧?这就是为什么我的代码不起作用,这一个工程okey:http://stackoverflow.com/questions/3347247/external-asynctask-class-with-progressdialog-update-and-returning-back? – user3232354 2014-12-05 17:32:41

+0

什么是'jsonConsumer'和'doInBackground()'中的'out'?你有很多东西在这里链接在一起,如果jsonConsumer以某种方式阻塞,你正在阻止UI线程继续,所以你的'Activity'没有进入运行状态,所以不会显示对话框。 – 2014-12-05 20:59:53

+0

是的,你有我的传球手阻止活动,谢谢:) – user3232354 2014-12-07 17:26:53