public class MainActivity extends Activity {
///手机归属地Webservice的参数信息
private static final String nameSpaceAddress = "http://WebXml.com.cn/";
private static final String urlAddress
= "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
private static final String methodNameAddress = "getMobileCodeInfo";
private static final String soapActionAddress = "http://WebXml.com.cn/getMobileCodeInfo";
private TextView telAddress = null;
private EditText tel = null;
private Button btnAddress = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAddress = (Button) this.findViewById(R.id.btnSearchAddress);
telAddress = (TextView) this.findViewById(R.id.telAddress);
tel = (EditText) this.findViewById(R.id.telNo);
btnAddress.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
getTelAddress();
}
}).start();
}
});
/**
* 请求WebService并获得返回的手机号码归属地信息
*/
public void getTelAddress() {
SoapObject soapObject = new
SoapObject(nameSpaceAddress, methodNameAddress);//创建SOAP对象
//设置属性,这些属性值通过SOAP协议传送给服务器
soapObject.addProperty("mobileCode", tel.getText().toString());//要查询的电话号码
soapObject.addProperty("userId", "");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.bodyOut = soapObject;
envelope.dotNet = true;
envelope.setOutputSoapObject(soapObject);
HttpTransportSE httpTransportSE = new HttpTransportSE(urlAddress);
try {
//调用服务
httpTransportSE.call(soapActionAddress, envelope);
} catch (Exception e) {
e.printStackTrace();
}
//获取服务传回的数据,手机归属地信息
SoapObject object = (SoapObject) envelope.bodyIn;
txtAddress = object.getProperty(0).toString();
//向主线程发送消息成功,getTelAddress函数执行完毕
handlerAddress.sendEmptyMessage(0);
}
Handler handlerAddress = new Handler() {
public void handleMessage(Message msg) {
telAddress.setText(txtAddress);
Toast.makeText(MainActivity.this,
"获取号码归属地成功"+txtAddress, Toast.LENGTH_LONG).show();
}
};
}