我主要活动:安卓: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)我不知道我做错了什么,我花了整整一天的时间没有结果:(
尝试在onCreate而不是onPre上启动进度对话框 – 2014-12-05 14:52:16
谢谢,但它没有帮助 – user3232354 2014-12-05 16:37:37