MainActivity如下:
package cc.testwebservice;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.http.HttpStatus;
import org.xmlpull.v1.XmlPullParser;
import android.os.Bundle;
import android.util.Xml;
import android.app.Activity;
/**
* Demo描述:
* 利用WebService查询手机号码归属地
*
* 注意事项:
* 在http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo
* 的SOAP 1.2中明确提示请求的设置:
* POST /WebServices/MobileCodeWS.asmx HTTP/1.1
* Host: webservice.webxml.com.cn
* Content-Type: application/soap+xml; charset=utf-8
* Content-Length: length
* 即:
* 请求方法:POST 采用HTTP协议 路径为/WebServices/MobileCodeWS.asmx
* 主机名称:webservice.webxml.com.cn
* 所以请求路径为http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx
* 亦明确提示在POST请求时需要设置:
* Content-Type和Content-Length字段及其值
*
* 参考文档:
* 1 http://www.webxml.com.cn/zh_cn/index.aspx
* 2 http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx
* 3 http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo
*
* 备注说明:
* 该服务在少数时候,会访问失败403错误.
* 多试几次即可
*/
public class MainActivity extends Activity {
private final String mobileNumber="1500280";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Thread(){
public void run() {
String city=queryMobileCodeByWebService(mobileNumber);
System.out.println("city="+city);
};
}.start();
}
private String queryMobileCodeByWebService(String mobileNumber){
String city=null;
try {
InputStream inputStream=this.getAssets().open("soap.xml");
byte [] soapData=inputStreamToByteArray(inputStream);
String soapContent=new String(soapData, "UTF-8");
//替换soap.xml中的占位符$number
soapContent = soapContent.replaceAll("\\$number", mobileNumber);
byte [] entity=soapContent.getBytes();
String webServicePath = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
URL webServiceURL=new URL(webServicePath);
HttpURLConnection httpURLConnection = (HttpURLConnection) webServiceURL.openConnection();
httpURLConnection.setConnectTimeout(8000);
httpURLConnection.setRequestMethod("POST");
//因为要发送SOAP协议,所以允许对外输出
httpURLConnection.setDoOutput(true);
//设置该SOAP协议要求的Content-Type字段
httpURLConnection.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
//设置该SOAP协议要求的Content-Length字段
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(entity.length));
OutputStream outputStream = httpURLConnection.getOutputStream();
//发送数据
outputStream.write(entity);
if (httpURLConnection.getResponseCode()==HttpStatus.SC_OK) {
city=parseSOAPResponse(httpURLConnection.getInputStream());
}
} catch (Exception e) {
}
return city;
}
//解析服务器以XML形式返回的SOAP
public String parseSOAPResponse(InputStream inputStream) {
String city = null;
try {
XmlPullParser xmlPullParser = Xml.newPullParser();
xmlPullParser.setInput(inputStream, "UTF-8");
int eventType = xmlPullParser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_TAG:
if ("getMobileCodeInfoResult".equals(xmlPullParser.getName())) {
city = xmlPullParser.nextText();
return city;
}
break;
}
eventType = xmlPullParser.next();
}
} catch (Exception e) {
}
return city;
}
public byte[] inputStreamToByteArray(InputStream inputStream){
ByteArrayOutputStream byteArrayOutputStream=null;
try {
byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len= inputStream.read(buffer)) != -1 ){
byteArrayOutputStream.write(buffer, 0, len);
}
byteArrayOutputStream.close();
inputStream.close();
} catch (Exception e) {
}
return byteArrayOutputStream.toByteArray();
}
}
main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="利用WebService查询手机号码归属地"
android:layout_centerInParent="true"
/>
</RelativeLayout>
soap.xml如下:
<?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>$number</mobileCode>
<userID></userID>
</getMobileCodeInfo>
</soap12:Body>
</soap12:Envelope>