Android 开发工具类 31_WebService 获取手机号码归属地

时间:2023-03-09 06:14:53
Android 开发工具类 31_WebService 获取手机号码归属地

AndroidInteractWithWebService.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>$mobile</mobileCode>
<userID></userID>
</getMobileCodeInfo>
</soap12:Body>
</soap12:Envelope>

WebServiceRequestFromAndroid

 package com.wangjialin.internet.service;

 import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL; import org.xmlpull.v1.XmlPullParser; import android.util.Xml; import com.wangjialin.internet.utils.StreamTool; public class WebServiceRequestFromAndroid {
/**
* 获取手机号归属地
* @param mobile 手机号
* @return
* @throws Exception
*/
public static String getAddress(String mobile) throws Exception{
String soap = readSoap();
soap = soap.replaceAll("\\$mobile", mobile);
byte[] entity = soap.getBytes(); String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
conn.setRequestProperty("Content-Length", String.valueOf(entity.length));
conn.getOutputStream().write(entity);
if(conn.getResponseCode() == 200){
return parseSOAP(conn.getInputStream());
}
return null;
}
/*
<?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 xml)throws Exception{
XmlPullParser pullParser = Xml.newPullParser();
pullParser.setInput(xml, "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 inStream = WebServiceRequestFromAndroid.class.getClassLoader().getResourceAsStream("AndroidInteractWithWebService.xml");
byte[] data = StreamTool.read(inStream);
return new String(data);
}
}

StreamTool

 package com.wangjialin.internet.utils;

 import java.io.ByteArrayOutputStream;
import java.io.InputStream; public class StreamTool {
/**
* 从流中读取数据
* @param inStream
* @return
*/
public static byte[] read(InputStream inStream) throws Exception{ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len = inStream.read(buffer)) != -1){
outputStream.write(buffer, 0, len);
}
inStream.close();
return outputStream.toByteArray();
} }

查询

   <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:onClick="query"
/>
  public void query(View v){

             String mobile = mobileText.getText().toString();
try {
String address = WebServiceRequestFromAndroid.getAddress(mobile);
textView.setText(address);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), R.string.error, 1).show();
}
}