归属地数据源
http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx
webxml网站还支持其他请求方式 如SOAP等等
界面比较简单
class="code_img_closed" src="/Upload/Images/2015030417/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('b28386d5-1cd3-44a7-a3f9-beee18af5282',event)" src="/Upload/Images/2015030417/2B1B950FA3DF188F.gif" alt="" /><?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" > <TextView android: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>View Code
下面是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.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 Thread thread; //定义消息 private static final int NUMBER_FORMAT_ERROR = 0; private static final int QUERY_SUCCESS_MSG = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); phoneSecEditText = (EditText) findViewById(R.id.phone_sec); resultView = (TextView) findViewById(R.id.result_text); queryButton = (Button) findViewById(R.id.query_btn); queryButton.setOnClickListener(new QueryOnClickListener()); } Handler handler = new Handler(){ @Override public 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()); break; default: break; } } }; String phoneSec; class QueryOnClickListener implements OnClickListener{ @Override public 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(); } } class QueryThread implements Runnable{ @Override public void run() { getRemoteInfo(phoneSec); } } /** * 手机号段归属地查询 * @param phoneSec 手机号段 */ private void getRemoteInfo(String phoneSec) { // TODO Auto-generated method stub // 定义待请求的URL String 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(); } @Override public 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) 免费用户为空字符串;返回数据:字符串(手机号码:省份 城市 手机卡类型)。
测试结果:如下
参考:http://blog.csdn.net/lyq8479/article/details/6413216