我写了这个小应用程序,从Web服务中获取一些数据并将它们放在自定义ListView中:
SOAP CALL
public String getVoy(String voy) {
String SOAP_ACTION = "XXXXXXXXXX";
String METHOD_NAME = "XXXXXXXXXX";
String NAMESPACE = "urn:DefaultNamespace";
String URL = "http://xxxxxxxxxxxx/xxxxx.nsf/xxxxxxx?wsdl";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("VOY", voy);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
final SoapObject result = (SoapObject) envelope.bodyIn;
rt = (result != null) ? result.getProperty(0).toString() : "Nessuna risposta 1";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // Invio il webservice
return rt;
}
的AsyncTask
private class LineCall extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
final String text = WebService.getInstance().getVoy(params[0]);
return text;
}
}
调用AsyncTask并获取数据
LineCall line = new LineCall();
line.execute(voy);
try {
**string** = line.get();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
然后我使用字符串变量,拆分我的数据并将它们放入自定义列表...
效果很好,但问题是非常慢所以我试图创建一个ProgressDialog,但是有一些问题......
如果我把它放在上一个活动中:
Button partenze = (Button) findViewById(R.id.partenze);
partenze.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
StartDeapartures call = new StartDeapartures();
call.execute();
}
});
}
private class StartDeapartures extends AsyncTask<Void, Void, Void> {
private ProgressDialog dialog;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog = new ProgressDialog(Home.this);
dialog.setTitle("Loading..");
dialog.setCancelable(false);
dialog.show();
}
@Override
protected Void doInBackground(Void... params) {
Intent intent = new Intent(getApplicationContext(), Partenze.class);
startActivity(intent);
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
dialog.dismiss();
}
}
进度图像不移动,似乎冻结了,如果我把它放在LineCall asyncTask中,则不会显示ProgressDialog ......
我在google中发现这是du-string = line.get();这需要很长时间才能运行......
那么当get()工作时我怎么能放一个ProgressDialog?
答案 0 :(得分:1)
如果您致电AsyncTask.get()
,它将阻止主线程,直到doInBackground
完成。因此,您不应该调用AsyncTask.get()
,而应该处理结果并在AsyncTask.onPostExecute()
方法中执行UI操作。这是AsyncTask
的设计行为。
在StartDeapartures asynctask中,为什么要在doInBackground中启动一个活动?使用AsyncTask启动活动有什么意义?