WebService实例:手机号查询

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

mobilewebservice网站上soap1.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>
        <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

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

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

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

你可能感兴趣的:(WebService实例:手机号查询)