我编写了这个小应用程序,它从 Web 服务中获取一些数据并将它们放入自定义 ListView:
肥皂电话
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;
}
异步任务
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...
我在谷歌发现这是由于 string = line.get(); 这需要很长时间才能运行...
那么当 get() 工作时如何放置 ProgressDialog 呢?