에서 나는 주요 활동이 있습니다안드로이드 :해서 ProgressDialog가 외부 비동기 클래스 (STH 블록 비동기 스레드)
내 응용 프로그램 서비스에서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;
}
}
그리고 진행 대화 상자가 표시입니다 만 QueryExecutor에서 doInBacground (...)를 포함 ChooseWriteSentenceActivity에서 onCreatre의 모든 코드 후 완료 (그래서 동시에 실질적으로 사라집니다). 그것은 sth가 QueryExecutorFindUser.doInBackground()를 사용하여 스레드를 기다리고있는 것처럼 보이며 동 기적으로 (?) 실행됩니다. 왜냐하면 코드 onPreExecute()가 올바르게 실행되고 (doInBackground (...) 이전에 시작) progressDialog .isShowing() == true (화면에는 표시되지 않음 :().
QueryExecutorFindUser에서 AsyncTask를 확장하고 주 확장에서이 확장을 사용하여 개인 클래스를 만들고 서비스를 포함하여 onCreated()에서 모든 코드를 실행하십시오.
나는 progressDialog를 모든 주요 활동 (실제로는 findUser뿐 아니라 모든 쿼리에 대해 사용하는 연습)에서 사용하지 않는 편이 좋지만 (예 : 모든 쿼리에 대해 QueryExecutor를 사용합니다.) 나는 내가 뭘 잘못하고 있는지 모르겠다. 하루 종일 보냈다. i 결과가 없습니다 : (
onCreate 대신 onCreate 대화 상자를 시작하십시오. –
감사하지만 도움이되지 않습니다. – user3232354