Android使用http请求手机号码归属地查询代码分享

Android使用http请求手机号码归属地查询代码分享,第1张

概述归属地数据源http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmxwebxml网站还支持其他请求方式如SOAP等等

归属地数据源

http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx

webxml网站还支持其他请求方式 如SOAP等等

界面比较简单

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:orIEntation="vertical"androID:layout_wIDth="fill_parent"androID:layout_height="fill_parent"androID:paddingtop="5dip"androID:paddingleft="5dip"androID:paddingRight="5dip"><TextVIEwandroID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:text="手机号码:"/><EditText androID:ID="@+ID/phone_sec"androID:layout_wIDth="fill_parent"androID:layout_height="wrap_content"androID:inputType="textPhonetic"androID:singleline="true"androID:hint="至少输入前七位"/><button androID:ID="@+ID/query_btn"androID:layout_wIDth="wrap_content"androID:layout_height="wrap_content"androID:layout_gravity="right"androID:text="查询"/><TextVIEw androID:ID="@+ID/result_text"androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center_horizontal|center_vertical"/></linearLayout> 

下面是MainActivity.java

package com.sphere.guishudi;import java.util.ArrayList;import java.util.List;import org.apache.http.httpResponse;import org.apache.http.httpStatus;import org.apache.http.nameValuePair;import org.apache.http.clIEnt.httpClIEnt;import org.apache.http.clIEnt.entity.UrlEncodedFormEntity;import org.apache.http.clIEnt.methods.httpPost;import org.apache.http.impl.clIEnt.DefaulthttpClIEnt;import org.apache.http.message.BasicnameValuePair;import org.apache.http.protocol.http;import org.apache.http.util.EntityUtils;import androID.app.Activity;import androID.app.ProgressDialog;import androID.os.Bundle;import androID.os.Handler;import androID.os.Message;import androID.vIEw.Menu;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.TextVIEw;/*** 手机号码归属地查询 */public class MainActivity extends Activity {private EditText phonesecEditText; private TextVIEw resultVIEw; private button querybutton;private ProgressDialog proDialog;private Thread thread;//定义消息private static final int NUMBER_FORMAT_ERROR = 0;private static final int query_SUCCESS_MSG = 1;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentVIEw(R.layout.main);phonesecEditText = (EditText) findVIEwByID(R.ID.phone_sec); resultVIEw = (TextVIEw) findVIEwByID(R.ID.result_text); querybutton = (button) findVIEwByID(R.ID.query_btn);proDialog = new ProgressDialog(MainActivity.this);//proDialog.setTitle("查询归属地");proDialog.setMessage("正在查询,请您耐心等待...");querybutton.setonClickListener(new queryOnClickListener());}Handler handler = new Handler(){@OverrIDepublic voID handleMessage(Message msg) {super.handleMessage(msg);switch (msg.what) {case NUMBER_FORMAT_ERROR:phonesecEditText.setText("");resultVIEw.setText("您输入的号码格式有误");break;case query_SUCCESS_MSG:resultVIEw.setText(msg.obj.toString());proDialog.dismiss();break;default:break;}}};String phonesec;class queryOnClickListener implements OnClickListener{@OverrIDepublic voID onClick(VIEw arg0) {//得到手机号phonesec = phonesecEditText.getText().toString().trim();if("".equals(phonesec)||phonesec.length()<7){//发送消息 显示查询结果的TextVIEw清空 handler.sendEmptyMessage(NUMBER_FORMAT_ERROR);//锁定焦点phonesecEditText.requestFocus();return;}// 查询手机号码(段)信息 //getRemoteInfo(phonesec);thread = new Thread(new queryThread());thread.start();proDialog.onStart();proDialog.show();}}class queryThread implements Runnable{@OverrIDepublic voID run() {getRemoteInfo(phonesec);}}/** * 手机号段归属地查询 * @param phonesec 手机号段 */ private voID getRemoteInfo(String phonesec) {// Todo auto-generated method stub// 定义待请求的URLString requestUrl = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";// 创建httpClIEnt实例httpClIEnt clIEnt = new DefaulthttpClIEnt();// 根据URL创建httpPost实例httpPost post = new httpPost(requestUrl);List<nameValuePair> params = new ArrayList<nameValuePair>();// 设置需要传递的参数params.add(new BasicnameValuePair("mobileCode",phonesec));params.add(new BasicnameValuePair("userID",""));try {// 设置URL编码post.setEntity(new UrlEncodedFormEntity(params,http.UTF_8));// 发送请求并获取反馈httpResponse response = clIEnt.execute(post);// 判断请求是否成功处理if (response.getStatusline().getStatusCode() == httpStatus.SC_OK) {// 解析返回的内容String result = EntityUtils.toString(response.getEntity());// 将查询结果经过解析后显示在TextVIEw中//resultVIEw.setText(filterHTML(result));Message msg = new Message();msg.what = query_SUCCESS_MSG;msg.obj = filterHTML(result);handler.sendMessage(msg);}} catch (Exception e) {e.printstacktrace();}}/** * 使用正则表达式过滤HTML标记 * * @param source 待过滤内容 * @return */ private String filterHTML(String source) { if(null == source){ return ""; } return source.replaceAll("</?[^>]+>","").trim(); } @OverrIDepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main,menu);return true;}} 

记得在AndroIDManifest.xml中配置<uses-permission androID:name="androID.permission.INTERNET" />

给予程序访问网络的权限。

使用子线程访问网络查询数据,handler做消息处理。

上面所讲解的只是httpClIEnt最基本的功能(发起POST请求);我们在浏览器客户端所执行的大多数 *** 作httpClIEnt都能够模拟,例如:提交表单、查询数据、上传下载文档、页面跳转、Session存储等。

getMobileCodeInfo

获得国内手机号码归属地省份、地区和手机卡类型信息

输入参数:mobileCode = 字符串(手机号码,最少前7位数字),userID = 字符串(商业用户ID) 免费用户为空字符串;返回数据:字符串(手机号码:省份 城市 手机卡类型)。

测试结果:如下

总结

以上是内存溢出为你收集整理的Android使用http请求手机号码归属地查询代码分享全部内容,希望文章能够帮你解决Android使用http请求手机号码归属地查询代码分享所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1149173.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存