Android通过webservice查询手机归属地

发送XML数据和调用webservice
Android项目————手机号归属地查询
新建person.xml
<?xml version="1.0" encoding="UTF-8"?>
<persons>
    <person id="23">
    <name>liming</name>
<age>30</age>
    </person>
<person id="20">
   <name>zhangxiaoxiao</name>
<age>25</age>
</person>
</persons>
单元测试:把XML以实体数据发送给服务端
配置Manifest
<manifest>
<application
<uses-library android:name="android.test.runner" />
</application>
<instrumentation android:targetPackage="", android:name="android.test.InstrumentationTestRunner">
</manifest>


XMLTest类
import android.test.AndroidTestCase;
public class XMLTest extends AndroidTestCase
{
  public void testSendXML() throws Exception
  {
    Inputstream inStream = this.getClass().getClassLoader().getResourceAsStream("person.xml");
byte[] data = StreamTool.read(inStream);
String path = "http://192.168.1.100:8080/web/XmlServlet"
HttpURLConnection conn=(HttpURLConnection) new URL(path).openConnection();
conn.setConnectTimeout(5000);
coonn.setRequestmethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type",  "text/xml; charset=UTF-8");
conn.setRequestProperty("Content-Length",String.valueOf(data.length));
conn.getOutputStream().write(data);
if(conn.getResponseCode() ==200)
{
 System.out.println("发送成功");
}
else
{
  System.out.println("发送失败");
}

  }
}
服务端
新建XmlServlet
public class XmlServlet extends HttpServlet
{
  private static final long serialVersionUID = 1L;
  protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
  {
  
  }
    protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
  {
     try{
  byte[] data = StreamTool.read(request.getInputStream());
  String xml = new String(data,"UTF-8");
  System.out.println(xml);
} catch(Exception e){
  e.printStackTrace();
}
  }   
}


WebService
可以看做是发布在网络上的API,客户端可以通过SOAP协议发送XML数据给webservice服务器,webservice服务器得到数据后,可以根据XML数据要求调用存在webservice的API,XML指定方法名称和各项参数,返回给客户端XML数据,客户端解析XML得到API的返回值。
所以客户端开发可以是任意语言,只要发送XML数据给webservice服务器即可获得相应API的返回值。
实例:
调用互联网上的webservice API来实现手机归属地查询   该API位于http://www.webxml.com.cn/
具体 http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx   
getMobileCodeInfo
获取国内手机号码归属地省份、地区和手机卡类型信息
输入参数:mobileCode=字符串(手机号码,最少前7位数字),userID=字符串(商业用户ID);返回数据:字符串(手机号码:省份 城市 手机卡类型)。
SOAP1.2
新建soap12.xml
复制示例xml,修改
<soap12:Body>
<getMobileCodeInfo xmlns="http://WebXml.com.cn/">
<mobileCode>$mobile</mobileCode>
<userID></userID>
</getMobileCodeInfo>
</soap12:Body>
</soap12:Envelope>


新建AddressService类
public class AddressService
{
/*用于获取手机号归属地,参数为手机号*/
  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(data.length));
conn.getOutputStream().write(entity);
if(conn.getResponseCode()==200)
{
  /*
    参考返回soap协议
<soap12:Body>
  <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">
  <getMobileCodeInfoResult>string</getMobileCodeInfoResult>
 </getMobileCodeInfoResponse>
 </soap12:Body>
 </soap12:Envelope>
  */
  return parseSOAP(conn.getInputStream());
}
return nulll;
  }
  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()
  {
    Inputstream inStream = AddressService.class.getClassLoader().getResourceAsStream("soap12.xml");
byte[] data = StreamTool.read(inStream);
return new String(data);
  }
}


Android界面
<Button
 android:onClick="query"
>
对按钮添加查询方法
public void query(View v)
{
  String mobile= mobileText.getText().toString();   //从文本输入框取得用户电话号码
  try{
      String address = AddressService.getAddress(mobile);
  }catch(Exception e){
      e.printStackTrace();
 Toast.makeText(getApplicationContext(),R.string.error,1).show();
  } 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值