欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android使用http請求手機號碼歸屬地查詢代碼分享

 更新時間:2016年06月07日 10:34:49   作者:jerrylsxu  
這篇文章主要介紹了Android使用http請求手機號碼歸屬地查詢代碼分享的相關(guān)資料,需要的朋友可以參考下

歸屬地數(shù)據(jù)源

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

webxml網(wǎng)站還支持其他請求方式 如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"
>
<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> 

下面是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){
//發(fā)送消息 顯示查詢結(jié)果的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";
// 創(chuàng)建HttpClient實例
HttpClient client = new DefaultHttpClient();
// 根據(jù)URL創(chuàng)建HttpPost實例
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ā)送請求并獲取反饋
HttpResponse response = client.execute(post);
// 判斷請求是否成功處理
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();
}
}
/** 
* 使用正則表達式過濾HTML標記 
* 
* @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請求);我們在瀏覽器客戶端所執(zhí)行的大多數(shù)操作HttpClient都能夠模擬,例如:提交表單、查詢數(shù)據(jù)、上傳下載文檔、頁面跳轉(zhuǎn)、Session存儲等。

getMobileCodeInfo

獲得國內(nèi)手機號碼歸屬地省份、地區(qū)和手機卡類型信息

輸入?yún)?shù):mobileCode = 字符串(手機號碼,最少前7位數(shù)字),userID = 字符串(商業(yè)用戶ID) 免費用戶為空字符串;返回數(shù)據(jù):字符串(手機號碼:省份 城市 手機卡類型)。

測試結(jié)果:如下

相關(guān)文章

  • Android 開發(fā)隨手筆記之使用攝像頭拍照

    Android 開發(fā)隨手筆記之使用攝像頭拍照

    在Android中,使用攝像頭拍照一般有兩種方法, 一種是調(diào)用系統(tǒng)自帶的Camera,另一種是自己寫一個攝像的界面,本篇文章給大家介紹android開發(fā)隨手筆記之使用攝像頭拍照,感興趣的朋友一起學習吧
    2015-11-11
  • Android無需讀寫權(quán)限通過臨時授權(quán)讀寫用戶文件詳解

    Android無需讀寫權(quán)限通過臨時授權(quán)讀寫用戶文件詳解

    這篇文章主要為大家介紹了Android無需讀寫權(quán)限通過臨時授權(quán)讀寫用戶文件詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • Flutter最小刷新范圍探索ValueListenableBuilder使用詳解

    Flutter最小刷新范圍探索ValueListenableBuilder使用詳解

    這篇文章主要為大家介紹了Flutter最小刷新范圍探索ValueListenableBuilder使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • Android實現(xiàn)簡單購物車功能

    Android實現(xiàn)簡單購物車功能

    這篇文章主要為大家詳細介紹了Android實現(xiàn)二級列表購物車功能 ,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • Android視頻錄制功能的實現(xiàn)步驟

    Android視頻錄制功能的實現(xiàn)步驟

    這篇文章主要介紹了Android視頻錄制功能的實現(xiàn)步驟,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • 如何正確實現(xiàn)Android啟動屏畫面的方法(避免白屏)

    如何正確實現(xiàn)Android啟動屏畫面的方法(避免白屏)

    本篇文章主要介紹了如何正確實現(xiàn)Android啟動屏畫面的方法(避免白屏),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • 探究Android客戶端網(wǎng)絡(luò)預(yù)連接優(yōu)化機制

    探究Android客戶端網(wǎng)絡(luò)預(yù)連接優(yōu)化機制

    一般情況下,我們都是用一些封裝好的網(wǎng)絡(luò)框架去請求網(wǎng)絡(luò),對底層實現(xiàn)不甚關(guān)注,而大部分情況下也不需要特別關(guān)注處理。了解底層的一些實現(xiàn),有益于我們對網(wǎng)絡(luò)加載進行優(yōu)化。本文就是關(guān)于根據(jù)http的連接復(fù)用機制來優(yōu)化網(wǎng)絡(luò)加載速度的原理與細節(jié)
    2021-06-06
  • Flutter仿釘釘考勤日歷的示例代碼

    Flutter仿釘釘考勤日歷的示例代碼

    這篇文章主要介紹了Flutter仿釘釘考勤日歷的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-04-04
  • Android完美實現(xiàn)平滑過渡的ViewPager廣告條

    Android完美實現(xiàn)平滑過渡的ViewPager廣告條

    這篇文章主要為大家詳細介紹了Android完美實現(xiàn)平滑過渡的ViewPager廣告條,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android應(yīng)用UI開發(fā)中Fragment的常見用法小結(jié)

    Android應(yīng)用UI開發(fā)中Fragment的常見用法小結(jié)

    這篇文章主要介紹了Android應(yīng)用UI開發(fā)中Fragment的常見用法小結(jié),Fragment的存在是為了解決不同屏幕分辯率的動態(tài)和靈活UI設(shè)計,需要的朋友可以參考下
    2016-02-02

最新評論