前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WebService实例:手机号查询

WebService实例:手机号查询

作者头像
提莫队长
发布2019-02-21 11:34:48
9010
发布2019-02-21 11:34:48
举报
文章被收录于专栏:刘晓杰

关于webservice的介绍在这里(http://blog.csdn.net/lxj1137800599/article/details/50929741) 接下来实现一个示例 工程目录如下:

这里写图片描述
这里写图片描述

mobilewebservice网站上soap1.2请求如下

这里写图片描述
这里写图片描述

而我们这里要改成这样

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" >

    <soap12:Body>
        <getMobileCodeInfo xmlns="http://WebXml.com.cn/" >
            <mobileCode>$mobile</mobileCode>
            <!-- 为了让用户可以输入手机号才设置的占位符$mobile -->

            <userID></userID>
            <!-- 这是测试代码,不用填写userID -->
        </getMobileCodeInfo>
    </soap12:Body>
</soap12:Envelope>

<!-- 以上是 SOAP 1.2 请求示例 -->
<!-- 网址:http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo -->

接下来编写业务处理类 WebServiceRequestFromAndroid.java

代码语言:javascript
复制
package com.example.androidinteractwithwebservice;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

public class WebServiceRequestFromAndroid {
    public static String getAddress(String mobile) throws Exception {
        String soap = readSoap();// 读取xml文件所有字符
        soap = soap.replaceAll("\\$mobile", mobile);// 转义字符
        byte[] entity = soap.getBytes();

        String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
        HttpURLConnection connection = (HttpURLConnection) new URL(path)
                .openConnection();
        connection.setConnectTimeout(5000);
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type",
                "application/soap+xml;charset=utf-8");
        connection.setRequestProperty("Content-Length", entity.length + "");
        connection.getOutputStream().write(entity);
        //传向webservice,
        //然后getInputStream得到响应
        if (connection.getResponseCode() == 200) {
            return parseSoap(connection.getInputStream());//解析响应部分
        }
        return null;
    }

    //注释部分是SOAP 1.2 响应示例
    // <?xml version="1.0" encoding="utf-8"?>
    // <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    // xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    // xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
    // <soap12:Body>
    //      <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">
    //      <getMobileCodeInfoResult>string</getMobileCodeInfoResult>
    //      </getMobileCodeInfoResponse>
    // </soap12:Body>
    // </soap12:Envelope>
    private static String parseSoap(InputStream inputStream) throws Exception {
        XmlPullParser pullParser = Xml.newPullParser();   
        pullParser.setInput(inputStream, "UTF-8");  
        int event = pullParser.getEventType();//触发第一个事件  
        while (event != XmlPullParser.END_DOCUMENT) {
            switch (event) {
            case XmlPullParser.START_TAG:
                if("getMobileCodeInfoResult".equals(pullParser.getName())){  
                        return pullParser.nextText(); 
                        }
                break;
            }
            event = pullParser.next();
        }
        return null;
    }

    private static String readSoap() throws Exception {
        InputStream inputStream = WebServiceRequestFromAndroid.class
                .getClassLoader().getResourceAsStream(
                        "AndroidInteractWithWebService.xml");
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        int len = -1;
        byte[] buffer = new byte[1024];
        while ((len = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);
        }
        byte[] data = outputStream.toByteArray();
        inputStream.close();
        outputStream.close();
        return new String(data);
    }
}

MainActivity.java

代码语言:javascript
复制
package com.example.androidinteractwithwebservice;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
    private EditText text;
    private Button button;

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

        text = (EditText) findViewById(R.id.editText1);
        button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String mobile = text.getText().toString();
                        String address = null;
                        try {
                            address = WebServiceRequestFromAndroid
                                    .getAddress(mobile);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        Log.i("address", address);
                    }
                }).start();
            }
        });
    }
}

这个可以自己测试,我不展示了。毕竟我用的是自己的手机号

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016年04月07日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档