归属地数据源
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) 免费用户为空字符串;返回数据:字符串(手机号码:省份 城市 手机卡类型)。
测试结果:如下
新闻热点
疑难解答