Android实践:基于聚合数据的手机号码归属地查询
前段时间,自学第一行代码,学到 json 数据解析,也看了很多大神的作品,为此新人第一次发作品,一个非常简单的手机号码归属地查询app,为了巩固一下学过去的东西。
步骤
- 依赖库
- 聚合数据接口申请
- 数据解析
- 界面布局
- 代码
- 最终效果
依赖库
我们需要用到Okhttp网路通信库,这里我们在build.gradle(app)文件里引入一下
compile 'com.google.code.gson:gson:2.8.0'
compile 'com.squareup.okhttp3:okhttp:3.4.1'
聚合数据接口申请
我的手机号码归属地查询接口是在聚合数据申请的。
聚合数据官网:聚合数据官网
首先需要注册一个账号,完成实名认证,就可以免费申请了,
申请成功后,在个人中心,就会有一个对应的key
数据解析
根据给出的json返回实例,进行相应的布局设计
界面布局
activity_main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.song1.pnsearch.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/inputNp_et"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="请输入11位手机号或前7位:" />
<Button
android:id="@+id/search_bt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="查询" />
</LinearLayout>
<TextView
android:id="@+id/province_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:textSize="18sp" />
<TextView
android:id="@+id/city_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:textSize="18sp" />
<TextView
android:id="@+id/areacode_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:textSize="18sp" />
<TextView
android:id="@+id/zip_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:textSize="18sp" />
<TextView
android:id="@+id/company_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:textSize="18sp" />
</LinearLayout>
代码
修改MainActivity.java代码
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.net.HttpURLConnection;
import java.net.URL;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
private EditText editText;
private Button button;
int resultcode;
String province, city, areacode, zip, company;
private TextView show_area, show_areacode, show_zip, show_company;
public static final String ADRESS = "http://apis.juhe.cn/mobile/get?phone=";
public static final String KEY = "&key=自己申请的KEY"; //替换成自己申请的KEY
private static String TAG = "text";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.inputNp_et);
show_area = (TextView) findViewById(R.id.area_tv);
show_areacode = (TextView) findViewById(R.id.areacode_tv);
show_zip = (TextView) findViewById(R.id.zip_tv);
show_company = (TextView) findViewById(R.id.company_tv);
button = (Button) findViewById(R.id.search_bt);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendRequestWithOkHttp();
}
});
}
/*
网络连接获取json数据
*/
private void sendRequestWithOkHttp() {
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection = null;
BufferedReader reader = null;
String ID = editText.getText().toString().trim();//获取输入框的手机号码
Log.e(TAG, "ID: " + ID);
try {
URL url = new URL(ADRESS + ID + KEY); //拼接成网址
Log.e(TAG, "url:" + url);
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
String responseData = response.body().string();
Log.e(TAG, "response:" + responseData);
showResponse(responseData);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
/*
解析JSON数据
*/
private void showResponse(final String data) {
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
JSONObject jsonObject = new JSONObject(data);
resultcode = jsonObject.optInt("resultcode");
JSONObject jsonObject1 = jsonObject.getJSONObject("result");
province = jsonObject1.optString("province").toString();
city = jsonObject1.optString("city").toString();
areacode = jsonObject1.optString("areacode").toString();
zip = jsonObject1.optString("zip").toString();
company = jsonObject1.optString("company").toString();
} catch (JSONException e) {
e.printStackTrace();
}
if (resultcode == 200) {
show_area.setText("号码归属地:" + province + "省" + city + "市");
show_areacode.setText("区号:" + areacode);
show_zip.setText("邮政编码:" + zip);
show_company.setText("运营商:中国" + company);
} else {
Toast.makeText(MainActivity.this, "查询失败,请输入正确的手机号码", Toast.LENGTH_SHORT).show();
}
}
});
}
}
因为我们的app需要联网,最后不要忘了,在AndroidMainfest.xml文件中添加网络权限。
<uses-permission android:name="android.permission.INTERNET"/>
最终效果
结束
新人第一次发,望多多支持,有啥不合理或者不懂的地方,欢迎留言讨论。