Android使用http請(qǐng)求手機(jī)號(hào)碼歸屬地查詢代碼分享
歸屬地?cái)?shù)據(jù)源
http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx
webxml網(wǎng)站還支持其他請(qǐng)求方式 如SOAP等等
界面比較簡(jiǎn)單
<?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="手機(jī)號(hào)碼:" /> <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;
/**
* 手機(jī)號(hào)碼歸屬地查詢
*/
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("正在查詢,請(qǐng)您耐心等待...");
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("您輸入的號(hào)碼格式有誤");
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) {
//得到手機(jī)號(hào)
phoneSec = phoneSecEditText.getText().toString().trim();
if("".equals(phoneSec)||phoneSec.length()<7){
//發(fā)送消息 顯示查詢結(jié)果的TextView清空
handler.sendEmptyMessage(NUMBER_FORMAT_ERROR);
//鎖定焦點(diǎn)
phoneSecEditText.requestFocus();
return;
}
// 查詢手機(jī)號(hào)碼(段)信息
//getRemoteInfo(phoneSec);
thread = new Thread(new QueryThread());
thread.start();
proDialog.onStart();
proDialog.show();
}
}
class QueryThread implements Runnable{
@Override
public void run() {
getRemoteInfo(phoneSec);
}
}
/**
* 手機(jī)號(hào)段歸屬地查詢
* @param phoneSec 手機(jī)號(hào)段
*/
private void getRemoteInfo(String phoneSec) {
// TODO Auto-generated method stub
// 定義待請(qǐng)求的URL
String requestUrl = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
// 創(chuàng)建HttpClient實(shí)例
HttpClient client = new DefaultHttpClient();
// 根據(jù)URL創(chuàng)建HttpPost實(shí)例
HttpPost post = new HttpPost(requestUrl);
List<NameValuePair> params = new ArrayList<NameValuePair>();
// 設(shè)置需要傳遞的參數(shù)
params.add(new BasicNameValuePair("mobileCode", phoneSec));
params.add(new BasicNameValuePair("userId", ""));
try {
// 設(shè)置URL編碼
post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
// 發(fā)送請(qǐng)求并獲取反饋
HttpResponse response = client.execute(post);
// 判斷請(qǐng)求是否成功處理
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 解析返回的內(nèi)容
String result = EntityUtils.toString(response.getEntity());
// 將查詢結(jié)果經(jīng)過解析后顯示在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();
}
}
/**
* 使用正則表達(dá)式過濾HTML標(biāo)記
*
* @param source 待過濾內(nèi)容
* @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" />
給予程序訪問網(wǎng)絡(luò)的權(quán)限。
使用子線程訪問網(wǎng)絡(luò)查詢數(shù)據(jù),handler做消息處理。
上面所講解的只是HttpClient最基本的功能(發(fā)起POST請(qǐng)求);我們?cè)跒g覽器客戶端所執(zhí)行的大多數(shù)操作HttpClient都能夠模擬,例如:提交表單、查詢數(shù)據(jù)、上傳下載文檔、頁(yè)面跳轉(zhuǎn)、Session存儲(chǔ)等。
getMobileCodeInfo
獲得國(guó)內(nèi)手機(jī)號(hào)碼歸屬地省份、地區(qū)和手機(jī)卡類型信息
輸入?yún)?shù):mobileCode = 字符串(手機(jī)號(hào)碼,最少前7位數(shù)字),userID = 字符串(商業(yè)用戶ID) 免費(fèi)用戶為空字符串;返回?cái)?shù)據(jù):字符串(手機(jī)號(hào)碼:省份 城市 手機(jī)卡類型)。
測(cè)試結(jié)果:如下


- 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編程實(shí)現(xiàn)號(hào)碼歸屬地查詢的方法
- kotlin實(shí)現(xiàn)快遞與號(hào)碼歸屬地查詢案例詳解
相關(guān)文章
Android基于訊飛語音SDK實(shí)現(xiàn)語音識(shí)別
本例子是一個(gè)調(diào)用訊飛語音識(shí)別SDK的例子源碼是一個(gè)最純凈的Demo比較容易看懂。實(shí)現(xiàn)的是點(diǎn)擊按鈕開始語音監(jiān)聽,手機(jī)需要聯(lián)網(wǎng),2/3G的均可,希望本文對(duì)大家學(xué)習(xí)Android有所幫助2016-06-06
Android中recyclerView底部添加透明漸變效果
這篇文章主要給大家介紹了關(guān)于Android中recyclerView如何實(shí)現(xiàn)底部添加透明漸變效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2018-04-04
Android通過PHP服務(wù)器實(shí)現(xiàn)登錄功能
這篇文章主要為大家詳細(xì)介紹了Android通過PHP服務(wù)器實(shí)現(xiàn)登錄功能的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Android編程實(shí)現(xiàn)帶漸變效果的圓角矩形示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)帶漸變效果的圓角矩形,涉及Android界面布局及屬性設(shè)置相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
Android中ProgressBar用法簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android中ProgressBar用法,以簡(jiǎn)單實(shí)例形式分析了Android中ProgressBar進(jìn)度條控件的功能與布局相關(guān)技巧,需要的朋友可以參考下2016-01-01

