您的位置:首页 > 移动开发

Android studio 电话号码归属地查询app简易版

2017-08-02 21:29 316 查看
这是个典型的客户端请求服务器数据,服务器返回json数据给客户端,然后再经由客户端对json格式的数据进行解析的例子。
JSON返回示例:

{
"resultcode":"200",
"reason":"Return Successd!",
"result":{
"province":"浙江",
"city":"杭州",
"areacode":"0571",
"zip":"310000",
"company":"中国移动",
"card":"移动动感地带卡"
}
}

这个json比较简单,直接用JSONobject进行解析获取数据即可。

界面代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.administrator.num_find.MainActivity">

<EditText
android:layout_marginTop="20dp"
android:id="@+id/editText"
android:text="请输入号码"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button"
android:text="查询"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_below="@id/editText"
/>
<LinearLayout
android:id="@+id/line"
android:layout_below="@id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="移动号码归属地支持号段:\n
134 135 136 137 138 139 150 151 152 157 158 159 182 183 187 188 147\n
联通号码归属地支持号段:\n
130 131 132 155 156 186 145\n
电信号码归属地支持号段:\n
133 153 189 180\n
移动运营商:\n
170\n"
/>
</LinearLayout>

<RelativeLayout
android:id="@+id/lineR"
android:layout_below="@id/line"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp">
<TextView
android:id="@+id/provinceCity"
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</RelativeLayout>
<RelativeLayout
android:layout_below="@id/lineR"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/relativeLayout"></RelativeLayout>

<TextView
android:id="@+id/zip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/lineR"
android:layout_toRightOf="@+id/button"
android:layout_toEndOf="@+id/button" />

<TextView
android:id="@+id/citycode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/lineR"
android:layout_toLeftOf="@+id/button"
android:layout_toStartOf="@+id/button" />

<TextView
android:id="@+id/card"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/relativeLayout"
android:layout_alignLeft="@+id/zip"
android:layout_alignStart="@+id/zip" />

<TextView
android:id="@+id/company"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/relativeLayout"
android:layout_toRightOf="@+id/lineR"
android:layout_toEndOf="@+id/lineR" />
</RelativeLayout>
本人UI不大好,界面有点丑,可以后期优化
在点击查询按钮后向服务器发出请求,此处开了个线程。有安卓基础的你懂的,因为要将数据显示到UI上。

接收数据后对得到的json进行解析,然后显示到界面上,这个程序就完成了。

代码:
package com.example.administrator.num_find;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends Activity {
private Button button;
private EditText editText;
private String NumString;

private String address = null;
private String response = null;

private TextView provinceCity;
private TextView cityCode;
private TextView zip1;
private TextView card1;
private  TextView company1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
editText = (EditText) findViewById(R.id.editText);
provinceCity = (TextView)findViewById(R.id.provinceCity) ;
cityCode = (TextView)findViewById(R.id.citycode);
zip1 = (TextView)findViewById(R.id.zip);
card1 = (TextView)findViewById(R.id.card);
company1= (TextView)findViewById(R.id.company);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection = null;
NumString = editText.getText().toString();
if (NumString == null) {

return;
}
address = "http://apis.juhe.cn/mobile/get?phone=" + NumString + "&key=你申请的api key";
System.out.println("NUM" + NumString);
System.out.println("addr" + address);
InputStream inputStream = null;
try {
URL url = new URL(address);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(8000);
connection.setConnectTimeout(8000);
inputStream = connection.getInputStream();
if (inputStream == null)
System.out.println("in null");
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
if (builder!=null){
response = builder.toString();
}
System.out.println(builder);
Json(response);
} catch (Exception e) {
e.printStackTrace();
}

}
}).start();
}
});
}
private void Json(final String response) {

try {

runOnUiThread(new Runnable() {
JSONObject jsonobject = new JSONObject(response);
JSONObject result = jsonobject.getJSONObject("result");
// System.out.println("result"+result);
String prvoince = result.getString("province");
// System.out.println("result"+prvoince);
String city = result.getString("city");
String areacode = result.getString("areacode");
String zip = result.getString("zip");
String company = result.getString("company");
String card = result.getString("card");
@Override
public void run() {
provinceCity.setText(prvoince);
cityCode.setText(areacode);
zip1.setText(zip);
company1.setText(company);
card1.setText(card);
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}

}


bug还有很多,例如没有对号码进行判断,可用正则表达式对后期进行优化。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: