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

这篇文章主要介绍了Android使用http请求手机号码归属地查询代码分享的相关资料,需要的朋友可以参考下

归属地数据源

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

界面比较简单

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:paddingTop="5dip"

android:paddingLeft="5dip"

android:paddingRight="5dip"

>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="手机号码:"

/>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:inputType="textPhonetic"

android:singleLine="true"

android:hint="至少输入前七位"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="right"

android:text="查询"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal|center_vertical"

/>

下面是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;

@Override

protected 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(){

@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());

proDialog.dismiss();

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();

proDialog.onStart();

proDialog.show();

}

}

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 params = new ArrayList();

// 设置需要传递的参数

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中配置

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

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

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

getMobileCodeInfo

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

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

测试结果:如下

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值