如何在Android中一次或逐个调用多个Web服务?因为如果我添加两个属性,则会发生错误。我使用KSOAP2来调用webservice。
答案 0 :(得分:1)
您可以使用Mutlitple AsyncTask执行并行Web服务调用。所以它将立即调用多个webservice。现在,如果你想逐个进行,那么在单个asynctask中,你可以在获得前一个响应后做到这一点。
对于AsycTask,请参阅此链接
答案 1 :(得分:0)
调用Web服务的Asynctask来获取后台进程的值..
SoapTask task = new SoapTask();
task.execute();
public class SoapTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
}
@Override
protected Void doInBackground(Void... urls) {
Live_Price();
return null;
}
@Override
protected void onPostExecute(Void result) {
if(!error){
try {
Object mObject = response.getProperty(0);
float str = Float.parseFloat(mObject.toString());
float gold = (float) (str / 31.10);
Object mObjectsilver = responseSilver;
float str1 = Float.parseFloat(mObjectsilver.toString());
float silver = (float) (str1 / 31.10);
// U just the get the value from the response like above and print it..
} catch (Exception e) {
e.printStackTrace();
}
}else {
System.out.println("server side problem");
}
}
}
在该代码中,您想获得两个url,username,userpassword,只需调用simple方法并获取webservices的响应即可获得值。并且响应变量声明全局只是在任何方法中使用..
public void Live_Price(){
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
response = (SoapObject) envelope.getResponse();
Log.d("myApp","call..ss..>"+ response.toString());
} catch (Exception e) {
error = true;
e.printStackTrace();
}
try {
androidHttpTransportsilver.call(SOAP_ACTIONsilver, envelopesilver);
responseSilver = (SoapPrimitive) envelopesilver.getResponse();
Log.d("myApp","call..ss..>"+ responseSilver.toString());
} catch (Exception e) {
error = true;
e.printStackTrace();
}
}