今天从书上和****上找了几个关于android调用webservice的样例,这些样例从代码来看。没不论什么错误,可是就是执行不成功。分析了android调用web接口的写法,发现这些样例在调用的时候没单独开一个线程来执行的问题。以下把****上下载的样例“ Android查询电话号码”的主要修改代码贴出来,希望对学习android的刚開始学习的人有帮助。
FutureTask<String> futureTask = new FutureTask<String>(
new Callable<String>()
{
@Override
public String call() throws Exception
{
try
{
// 调用WebService
transport.call(soapAction, envelope);
if (envelope.getResponse() != null)
{
// 获取返回的数据
SoapObject object = (SoapObject) envelope.bodyIn;
// 获取返回的结果
String result = object.getProperty(0)
.toString();
return result;
}
} catch (Exception e)
{
e.printStackTrace();
}
return null;
}
});
new Thread(futureTask).start(); // 将WebService返回的结果显示在TextView中
try
{
String result=futureTask.get();
resultView.setText(result);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}