手机号的归属地查询

布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context="com.example.demo.MainActivity">

    <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edit_text"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="130dp"
        android:text="TextView" />


</RelativeLayout>

MainActivity

package com.example.demo;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends Activity {
    private EditText editText;
    private TextView tv;
    private Handler myHandler = new Handler() {
        public void handleMessage(Message msg) {
            int arg1 = msg.what;
            if (arg1 == 0) {
                tv.setText(msg.obj.toString());
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = (EditText) findViewById(R.id.edit_text);
        tv = (TextView) findViewById(R.id.textView1);
        editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String string = s.toString();
                if (s.length() == 11) {
                    //查询
                    serchPhone(string);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    private void serchPhone(final String s) {
        new Thread() {
            public void run() {
                String path = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=" + s;
                try {
                    URL url = new URL(path);

                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    // 提交模式
                    connection.setRequestMethod("GET");
                    //读取超时 单位毫秒
                    connection.setReadTimeout(5000);
                    //连接超时 单位毫秒
                    connection.setConnectTimeout(5000);


                    //获取
                    int responseCode = connection.getResponseCode();
                    if (responseCode == 200) {
                        InputStream inputStream = connection.getInputStream();
                        String string = streamToString(inputStream, "gbk");

                        String json = string.substring(string.indexOf("{"));
                        Message message = Message.obtain();
                        message.what = 0;
                        message.obj = json;
                        myHandler.sendMessage(message);
                        Log.d("string", json);
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            ;
        }.start();
    }

    private String streamToString(InputStream inputStream, String charset) {
        try {
            //输入流
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, charset);
            //得到缓冲流
            BufferedReader reader = new BufferedReader(inputStreamReader);

            String s = null;
            StringBuilder builder = new StringBuilder();
            while ((s = reader.readLine()) != null) {
                builder.append(s);
            }
            reader.close();
            return builder.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;

    }
}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值