Android編程實(shí)現(xiàn)號(hào)碼歸屬地查詢的方法
本文實(shí)例講述了Android編程實(shí)現(xiàn)號(hào)碼歸屬地查詢的方法。分享給大家供大家參考,具體如下:
我們通過發(fā)送XML訪問 WebService就可以實(shí)現(xiàn)號(hào)碼的歸屬地查詢,我們可以使用代理服務(wù)器提供的XML的格式進(jìn)行設(shè)置,然后請(qǐng)求提交給服務(wù)器,服務(wù)器根據(jù)請(qǐng)求就會(huì)返回給一個(gè)XML,XML中就封裝了我們想要獲取的數(shù)據(jù)。
發(fā)送XML
1.通過URL封裝路徑打開一個(gè)HttpURLConnection
2.設(shè)置請(qǐng)求方式,Content-Type和Content-Length
XML文件的Content-Type為:application/soap+xml; charset=utf-8
3.使用HttpURLConnection獲取輸出流輸出數(shù)據(jù)
WebService
1.WebService是發(fā)布在網(wǎng)絡(luò)上的API,可以通過發(fā)送XML調(diào)用,WebService返回結(jié)果也是XML數(shù)據(jù)
2.WebService沒有語言限制,只要可以發(fā)送XML數(shù)據(jù)和接收XML數(shù)據(jù)即可
3.http://www.webxml.com.cn/網(wǎng)站上提供了一些WebService服務(wù),我們可以對(duì)其進(jìn)行調(diào)用
4.http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo中提供了電話歸屬地查詢的使用說明
效果圖:
核心代碼:
public class XmlService { public String query(String num) throws Exception { InputStream in = this.getClass().getClassLoader().getResourceAsStream("query.xml"); byte[] data = LoadUtils.load(in); String xml = new String(data); //替換 xml = xml.replace("#", num); byte[] sendData = xml.getBytes("UTF-8"); //發(fā)送到代理的地址上 URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8"); conn.setRequestProperty("Content-Length", String.valueOf(sendData.length)); //將請(qǐng)求的xml發(fā)送出去 conn.setDoOutput(true); conn.getOutputStream().write(sendData); //獲取從服務(wù)器傳回來的數(shù)據(jù) if (conn.getResponseCode() == 200) return parse(conn.getInputStream()); return null; } //解析流拿到getMobileCodeInfoResult中的數(shù)據(jù) private String parse(InputStream inputStream) throws Exception { XmlPullParser parser = Xml.newPullParser(); parser.setInput(inputStream, "UTF-8"); //查找getMobileCodeInfoResult標(biāo)簽,獲取標(biāo)簽中的數(shù)據(jù) for (int event = parser.getEventType(); event != XmlPullParser.END_DOCUMENT; event = parser.next()) switch (event) { case XmlPullParser.START_TAG: if ("getMobileCodeInfoResult".equals(parser.getName())) return parser.nextText(); } return null; } }
發(fā)送的xml封裝了電話號(hào)碼(query.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>#</mobileCode> <userID></userID> </getMobileCodeInfo> </soap12:Body> </soap12:Envelope>
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android快遞物流信息布局開發(fā)
- Android實(shí)現(xiàn)快遞單號(hào)查詢快遞狀態(tài)信息
- Android自定義view仿淘寶快遞物流信息時(shí)間軸
- Android實(shí)現(xiàn)快遞物流時(shí)間軸效果
- android實(shí)現(xiàn)快遞跟蹤進(jìn)度條
- Android實(shí)現(xiàn)仿美團(tuán)、順豐快遞數(shù)據(jù)加載效果
- Android實(shí)現(xiàn)快遞物流跟蹤布局效果
- Android使用http請(qǐng)求手機(jī)號(hào)碼歸屬地查詢代碼分享
- kotlin實(shí)現(xiàn)快遞與號(hào)碼歸屬地查詢案例詳解
相關(guān)文章
android利用消息機(jī)制獲取網(wǎng)絡(luò)圖片
這篇文章主要為大家詳細(xì)介紹了android利用消息機(jī)制獲取網(wǎng)絡(luò)圖片的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03Android實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)下拉框 下拉列表spinner的實(shí)例代碼
這篇文章介紹了Android實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)下拉框 下拉列表spinner的實(shí)例代碼,有需要的朋友可以參考一下2013-10-10RecyclerView中使用CheckBox出現(xiàn)勾選混亂的解決方法
這篇文章主要為大家詳細(xì)介紹了RecyclerView中使用CheckBox出現(xiàn)勾選混亂的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android Studio 超級(jí)簡(jiǎn)單的打包生成apk的方法
本篇文章主要介紹了Android Studio 超級(jí)簡(jiǎn)單的打包生成apk的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Android編程實(shí)現(xiàn)將tab選項(xiàng)卡放在屏幕底部的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)將tab選項(xiàng)卡放在屏幕底部的方法,涉及Android界面布局、tab設(shè)置及權(quán)限控制相關(guān)操作技巧,需要的朋友可以參考下2017-03-03