WebService的简单使用

示例代码,通过webservice查询电话归属地

设置网络权限

一个EditText  一个Button  还有一个TextView  通过button的点击事件  将输入的电话号码的归属地   设置在textview上

public class MainActivity extends AppCompatActivity {

    private TextView text;
    private EditText edit;
    private Button button;
    private String edtext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        intiView();


    }
    //初始化数据
    private void intiView() {
        text = (TextView) findViewById(R.id.text_1);
        edit = (EditText) findViewById(R.id.edit_1);
        button = (Button) findViewById(R.id.button);
    }

    //点击事件  查询  耗时操作,放在子线程
    public void qurry(View view){
        new Thread(){
            @Override
            public void run() {
                super.run();
                edtext = edit.getText().toString();
                final String s;
                try {
                    s = qurryPhone(edtext);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            text.setText(s);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }.start();

    }

    //查询电话归属地的方法
    public String qurryPhone(String phone){
        try {
            String servers_Uri="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx";
            String name_space="http://WebXml.com.cn/";
            String method_name="getMobileCodeInfo";

            //创建信封
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

            //创建信件
            SoapObject soapObject = new SoapObject(name_space, method_name);
            soapObject.addProperty("mobileCode",phone);
//        soapObject.addProperty("theUserID","3cdded86be454f258aed2a0610295961");

            //将信件装进信封
            envelope.bodyOut=soapObject;

            //设置跨平台的兼容性
            envelope.dotNet=true;
            //创建连接
            HttpTransportSE httpTransportSE = new HttpTransportSE(servers_Uri);

            //发送消息
            httpTransportSE.call(name_space + method_name,envelope);

            //服务器响应  如果消息不为空  证明服务器响应了
            if(envelope.getResponse() != null){
                SoapObject result = (SoapObject) envelope.bodyIn;
                String local=result.getProperty("getMobileCodeInfoResult").toString();
                return local;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }

        return null;
    }

} 

Web Service是一种基于Web的应用程序接口(API),它允许不同的应用程序之间进行交互,使用标准的Web技术进行通信。Web Service通常使用XML格式的数据进行交换,这使得它们可以跨越不同的操作系统和编程语言来实现应用程序集成和数据交换。 Web Service使用可以分为两个方面,一方面是Web Service的提供者,另一方面是Web Service使用者。 Web Service的提供者需要遵守一定的规范来创建Web Service,并将它们发布到互联网上,以便其他应用程序可以访问它们。 Web Service使用者可以通过调用Web Service的方法来访问远程应用程序的功能。使用Web Service时,需要知道Web Service的URL和方法名称,并提供必要的参数。 以下是使用Web Service简单示例: 1. 创建一个Web Service客户端程序 在编程语言中创建一个Web Service客户端程序,例如Java、C#、Python等。 2. 指定Web Service的URL Web Service的URL是Web Service提供者发布Web Service时指定的地址。您需要知道Web Service的URL才能访问它。 3. 创建Web Service请求 创建一个Web Service请求,包括要调用的方法名称和必要的参数。 4. 发送Web Service请求 将Web Service请求发送到Web Service提供者的URL。 5. 处理Web Service响应 接收Web Service提供者返回的响应,并解析响应以获取所需的数据或执行所需的操作。 这些步骤可以在不同的编程语言和开发环境中实现,具体实现方式可能会有所不同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值