Android手機(jī)號(hào)碼歸屬地的查詢
一個(gè)簡(jiǎn)單的Demo,從聚合數(shù)據(jù)申請(qǐng)手機(jī)號(hào)碼歸屬地?cái)?shù)據(jù)接口;
在EditText中輸入待查詢號(hào)碼,獲取號(hào)碼后在子線程中使用HttpUrlconnection獲取JSON數(shù)據(jù),之后進(jìn)行解析;
數(shù)據(jù)獲取完成后,在主線程中更新UI,顯示獲取的號(hào)碼歸屬地信息。
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/et_querylocation"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textColor="#000000"
android:hint="輸入號(hào)碼"/>
<Button
android:onClick="query"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查詢"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_phonelocation"
android:textSize="20sp"
android:textColor="#000000"/>
</LinearLayout>
java代碼
package com.example.phonehome;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText et_phone;
private TextView tv_phone;
private final static int START = 0;
private final static int FINISH = 1;
private String phone;//待查詢號(hào)碼
//號(hào)碼信息
private static String province;
private static String city;
private static String company;
private static String card;
public static final String DEF_CHATSET = "UTF-8";
public static final int DEF_CONN_TIMEOUT = 30000;
public static final int DEF_READ_TIMEOUT = 30000;
public static final String APPKEY ="申請(qǐng)的APP KEY";
//子線程中查詢數(shù)據(jù)開始、完成時(shí)發(fā)送消息,完成相應(yīng)操作
Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case START:
Toast.makeText(MainActivity.this, "正在查詢,請(qǐng)稍候", Toast.LENGTH_SHORT).show();
break;
case FINISH:
//在Textview中顯示查得的號(hào)碼信息(子線程中不能更新UI)
tv_phone.setText(province +" "+ city + " " + company + " " + card);
break;
default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
}
//Button的店家事件,獲取待查詢號(hào)碼后在子線程中進(jìn)行查詢
public void query(View v){
phone = et_phone.getText().toString().trim();
if (!TextUtils.isEmpty(phone)) {
new Thread(){
public void run() {
//開始查詢
handler.obtainMessage(START).sendToTarget();
getRequest(phone);
//查得結(jié)果
handler.obtainMessage(FINISH).sendToTarget();
};
}.start();
}else {
Toast.makeText(MainActivity.this, "輸入號(hào)碼不能為空", Toast.LENGTH_SHORT).show();
}
}
//手機(jī)歸屬地查詢
public static void getRequest(String phone){
String result =null;
String url ="http://apis.juhe.cn/mobile/get";//請(qǐng)求接口地址
Map params = new HashMap();//請(qǐng)求參數(shù)
params.put("phone",phone);//需要查詢的手機(jī)號(hào)碼或手機(jī)號(hào)碼前7位
params.put("key",APPKEY);//應(yīng)用APPKEY(應(yīng)用詳細(xì)頁查詢)
params.put("dtype","");//返回?cái)?shù)據(jù)的格式,xml或json,默認(rèn)json
try {
//得到JSON數(shù)據(jù),并進(jìn)行解析
result =net(url, params, "GET");
JSONObject object = new JSONObject(result);
JSONObject ob = new JSONObject(object.get("result").toString()+"");
province = ob.getString("province");
city = ob.getString("city");
company = ob.getString("company");
card = ob.getString("card");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
* @param strUrl 請(qǐng)求地址
* @param params 請(qǐng)求參數(shù)
* @param method 請(qǐng)求方法
* @return 網(wǎng)絡(luò)請(qǐng)求字符串
* @throws Exception
*/
public static String net(String strUrl, Map params,String method) throws Exception {
HttpURLConnection conn = null;
BufferedReader reader = null;
String rs = null;
try {
StringBuffer sb = new StringBuffer();
if(method==null || method.equals("GET")){
strUrl = strUrl+"?"+urlencode(params);
}
URL url = new URL(strUrl);
conn = (HttpURLConnection) url.openConnection();
if(method==null || method.equals("GET")){
conn.setRequestMethod("GET");
}else{
conn.setRequestMethod("POST");
conn.setDoOutput(true);
}
//conn.setRequestProperty("User-agent", userAgent);
conn.setUseCaches(false);
conn.setConnectTimeout(DEF_CONN_TIMEOUT);
conn.setReadTimeout(DEF_READ_TIMEOUT);
conn.setInstanceFollowRedirects(false);
conn.connect();
if (params!= null && method.equals("POST")) {
try {
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.writeBytes(urlencode(params));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
InputStream is = conn.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, DEF_CHATSET));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sb.append(strRead);
}
rs = sb.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
if (conn != null) {
conn.disconnect();
}
}
return rs;
}
//將map型轉(zhuǎn)為請(qǐng)求參數(shù)型
public static String urlencode(Map<String,String> data) {
StringBuilder sb = new StringBuilder();
for (Map.Entry i : data.entrySet()) {
try {
sb.append(i.getKey()).append("=").append(URLEncoder.encode(i.getValue()+"","UTF-8")).append("&");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return sb.toString();
}
private void initView() {
setContentView(R.layout.activity_main);
et_phone = (EditText) findViewById(R.id.et_querylocation);
tv_phone = (TextView) findViewById(R.id.tv_phonelocation);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
- Android組件WebView編寫有道詞典小案例分享
- Android 有道詞典的簡(jiǎn)單實(shí)現(xiàn)方法介紹
- Android優(yōu)化查詢加載大數(shù)量的本地相冊(cè)圖片
- 淺析Android手機(jī)衛(wèi)士之號(hào)碼歸屬地查詢
- Android編程實(shí)現(xiàn)號(hào)碼歸屬地查詢的方法
- Android編程操作聯(lián)系人的方法(查詢,獲取,添加等)
- Android中的SQL查詢語句LIKE綁定參數(shù)問題解決辦法(sqlite數(shù)據(jù)庫)
- Android 軟件自動(dòng)更新功能實(shí)現(xiàn)的方法
- android實(shí)現(xiàn)倒計(jì)時(shí)功能代碼
- Android實(shí)現(xiàn)上傳文件功能的方法
- Android實(shí)現(xiàn)有道辭典查詢功能實(shí)例詳解
相關(guān)文章
Android使用WebView實(shí)現(xiàn)文件下載功能
這篇文章主要為大家詳細(xì)介紹了Android使用WebView實(shí)現(xiàn)文件下載功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android開發(fā)中ImageView的scaletype屬性用法分析
這篇文章主要介紹了Android開發(fā)中ImageView的scaletype屬性用法,分析了scaletype屬性參數(shù)的常見功能并結(jié)合實(shí)例形式給出了具體的使用方法,需要的朋友可以參考下2016-08-08
Android中分析Jetpack?Compose動(dòng)畫內(nèi)部的實(shí)現(xiàn)原理
這篇文章主要介紹了Android中分析Jetpack?Compose動(dòng)畫內(nèi)部的實(shí)現(xiàn)原理,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-09-09
Android 實(shí)現(xiàn)代碼混淆的實(shí)例
這篇文章主要介紹了Android 實(shí)現(xiàn)代碼混淆的實(shí)例的相關(guān)資料,希望通過本文大家能夠掌握Android代碼混淆的實(shí)現(xiàn)方法,需要的朋友可以參考下2017-09-09
Android中關(guān)于百度糯米app關(guān)閉網(wǎng)頁或窗口的方法(99%人不知)
這篇文章主要介紹了Android中關(guān)于百度糯米app中關(guān)閉網(wǎng)頁或窗口的方法,其實(shí)解決方法到很簡(jiǎn)單,但是很多人都不知道如何解決的,在網(wǎng)上也很難找到答案的,下面小編給大家揭曉答案,需要的朋友可以參考下2016-08-08
詳解Android中提示對(duì)話框(ProgressDialog和DatePickerDialog和TimePickerDi
這篇文章主要介紹了詳解Android中提示對(duì)話框(ProgressDialog和DatePickerDialog和TimePickerDialog&PopupWindow)的相關(guān)資料,需要的朋友可以參考下2016-01-01
Android實(shí)現(xiàn)圖片上傳蒙層進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圖片上傳蒙層進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09

