android连接webservice

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();
        }
    };
}

转载 http://www.cnblogs.com/jerehedu/p/4493753.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值