Android 调用Webservice查询手机归属地

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/confusing_awakening/article/details/79831678

这里写图片描述

http://www.webxml.com.cn/zh_cn/index.aspx中查看调用方式
这里写图片描述

这里写图片描述

这里写图片描述

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="查询归属地" />

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
public class MainActivity extends AppCompatActivity {
    ///手机归属地Webservice的参数信息
    private static final String nameSpaceAddress = "http://WebXml.com.cn/";//targetNamespace
    private static final String urlAddress = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx"; //soap:address location
    private static final String methodNameAddress = "getMobileCodeInfo";//s:element name
    private static final String soapActionAddress = "http://WebXml.com.cn/getMobileCodeInfo";//soap:operation soapAction
    private TextView telAddress = null;
    private EditText tel = null;
    private Button btnAddress = null;

    private String txtAddress;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnAddress = (Button) this.findViewById(R.id.btn);
        telAddress = (TextView) this.findViewById(R.id.tv);
        tel = (EditText) this.findViewById(R.id.et);
        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.bodyOut = soapObject;
//        envelope.setOutputSoapObject(soapObject);
        HttpTransportSE httpTransportSE = new HttpTransportSE(urlAddress);
        try {
            //调用服务
            httpTransportSE.call(soapActionAddress, envelope);
        } catch (Exception e) {
            Log.e("kj",e.getMessage());
            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();
        }
    };

}

参考:WebService

猜你喜欢

转载自blog.csdn.net/confusing_awakening/article/details/79831678