手机归属地查询----自学了这么久总算可以不copy他人代码写出一款APP了 虽然有点丑
2017-08-24 02:08
579 查看
通过利用OKhttp,Gson来制作一款手机归属地查询APP
利用正则判断 如果输入手机号不正确会Toast一条消息,如果输入正确则会进行归属地查询
归属地API查了好久TAT 最后 被我发现了一个免费的API 还是自己拼接的 - - ;
直接看图吧
布局文件很简单 就是一个EditText 一个TextView用来显示查询结果 一个Button
<?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_input_phonenum"
style="@style/etstyle"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="20dp"
android:gravity="center"
android:hint="请输入手机号" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/et_input_phonenum"
android:orientation="vertical"
>
<LinearLayout
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="您的归属地是:"/>
<EditText
android:id="@+id/tv_showinform"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<Button
android:id="@+id/btn_search"
style="@style/btnstyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:text="查询" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
然后是Activity代码
利用正则判断 如果输入手机号不正确会Toast一条消息,如果输入正确则会进行归属地查询
归属地API查了好久TAT 最后 被我发现了一个免费的API 还是自己拼接的 - - ;
直接看图吧
布局文件很简单 就是一个EditText 一个TextView用来显示查询结果 一个Button
<?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_input_phonenum"
style="@style/etstyle"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="20dp"
android:gravity="center"
android:hint="请输入手机号" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/et_input_phonenum"
android:orientation="vertical"
>
<LinearLayout
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="您的归属地是:"/>
<EditText
android:id="@+id/tv_showinform"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<Button
android:id="@+id/btn_search"
style="@style/btnstyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:text="查询" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
然后是Activity代码
package com.example.hc.attributionquery; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.google.gson.Gson; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class MainActivity extends AppCompatActivity { private String mUrl = "http://api.k780.com:88/?app=phone.get&phone=18969344680&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json"; private EditText et_input_phonenum; private Button btn_search; private TextView tv_showinform; public static final int SUCCESS = 110; public static final int FILED = 120; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_main); initView(); } public void initView() { et_input_phonenum = (EditText) findViewById(R.id.et_input_phonenum); btn_search = (Button) findViewById(R.id.btn_search); tv_showinform = (TextView) findViewById(R.id.tv_showinform); btn_search.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (isMobile(et_input_phonenum.getText().toString())){ /** * 利用okhttp访问网络,通过get方式获取相应Json数据 */ OkHttpClient okHttpClient = new OkHttpClient(); Request request = new Request.Builder().get().url("http://api.k780.com:88/?app=phone.get&phone=" + et_input_phonenum.getText().toString() + "&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json").build(); Call call = okHttpClient.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Toast.makeText(MainActivity.this, "出错了", Toast.LENGTH_SHORT).show(); } @Override public void onResponse(Call call, Response response) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.body().byteStream())); System.out.println("1"); StringBuilder stringBuilder = new StringBuilder(); String str = null; while ((str = bufferedReader.readLine()) != null) { stringBuilder.append(str); } System.out.println("2"); /** * 利用Gson解析Json数据,获得手机归属地 */ Gson gson = new Gson(); final GetZoneResult getZoneResult = gson.fromJson(stringBuilder.toString(), GetZoneResult.class); System.out.println(getZoneResult.getResult().getStyle_simcall()); System.out.println("3"); runOnUiThread(new Runnable() { @Override public void run() { tv_showinform.setText(getZoneResult.getResult().getStyle_simcall()); } }); } }); }else { Toast.makeText(MainActivity.this,"请输入正确手机号",Toast.LENGTH_SHORT).show(); } } }); } /** * 验证手机格式 */ public static boolean isMobile(String number) { /* 移动:134、135、136、137、138、139、150、151、157(TD)、158、159、187、188 联通:130、131、132、152、155、156、185、186 电信:133、153、180、189、(1349卫通) 总结起来就是第一位必定为1,第二位必定为3或5或8,其他位置的可以为0-9 */ String num = "[1][358]\\d{9}";//"[1]"代表第1位为数字1,"[358]"代表第二位可以为3、5、8中的一个,"\\d{9}"代表后面是可以是0~9的数字,有9位。 if (TextUtils.isEmpty(number)) { return false; } else { //matches():字符串是否在给定的正则表达式匹配 return number.matches(num); } } }
相关文章推荐
- 一些可以查询IP地理位置、身份证所在地、手机归属地的接口
- Java代码总结【1】_查询手机号码归属地
- 一些可以查询IP地理位置、身份证所在地、手机归属地的接口
- 一些可以查询IP地理位置、身份证所在地、手机归属地的接口
- 一些可以查询IP地理位置、身份证所在地、手机归属地的接口
- 一些可以查询IP地理位置、身份证所在地、手机归属地的接口
- 手机号码归属地查询免费api接口代码
- 手机归属地查询程序代码
- 推荐一款可以保障人身财产安全的免费手机app软件 - 三眼
- PHP一些可以查询IP地理位置、身份证所在地、手机归属地的接口
- PHP一些可以查询IP地理位置、身份证所在地、手机归属地的接口
- PHP一些可以查询IP地理位置、身份证所在地、手机归属地的接口
- PHP一些可以查询IP地理位置、身份证所在地、手机归属地的接口
- Access数据库查询练习专用数据库 -手机号码归属地Access数据库_MobileDB(10万条记录)
- 傻瓜都会写出能够让机器理解的代码,只有好的程序员才能写出人类可以理解的代码。
- 手机归属地免费查询接口,api,wsdl,webservice
- android 安卓APP获取手机设备信息和手机号码的代码示例
- Android使用HttpURLConnect、HttpClient访问WebService查询手机号码归属地