1. 程式人生 > >-獲取手機號地址

-獲取手機號地址

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
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 java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpRetryException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.Executor;

public class MainActivity extends AppCompatActivity {

private Button query;
private TextView info;
private EditText input;

private final int UPDATE_PHONE_INFO = 0;
private final int GET_PHONE_INFO_ERROR = 1;
//ctrl+p  檢視方法需要的引數
@SuppressLint("HandlerLeak")
 private Handler mHandler = new Handler(){

    @Override
    public void handleMessage(Message msg) {
       switch (msg.what){
               case UPDATE_PHONE_INFO:
                   info.setText((String) msg.obj);
                   break;
               case GET_PHONE_INFO_ERROR:
                   info.setText((String) msg.obj);
                   break;
       }
    }
};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //尋找資源id
    input = findViewById(R.id.input);
    input.setText("18679906337");
    query = findViewById(R.id.quey);
    info = findViewById(R.id.info);
    
    
    
    query.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        requestData(input.getText().toString());
                    }
                }).start();
        }
    });
}



    //手機號地址
    private  String apUrl="https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=";
     private void requestData(String phoneName) {
         //做網路操作、必須在子執行緒中做

    //查詢手機號

         try {
             //請求地址
             URL url = new URL(apUrl+phoneName);
             //連結  ctrl+h  檢視繼承結構
             HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
             //請求方法
             urlConnection.setRequestMethod("GET");
             //連結超時時間
             urlConnection.setConnectTimeout(5000);
             //讀取超時時間
             urlConnection.setReadTimeout(5000);
             //獲取返回結果,狀態碼
             //響應狀態碼
             int responseCode = urlConnection.getResponseCode();
             if (responseCode==200){//請求成功
                    //獲取結果
                 String result = stream2String(urlConnection.getInputStream(),"gbk");
                    //展示
                // info.setText(result);

                 Message message = mHandler.obtainMessage(UPDATE_PHONE_INFO, result);
                mHandler.sendMessage(message);
             }else {

                 //info.setText("請求返回錯誤"+responseCode);
                 mHandler.sendMessage(mHandler.obtainMessage(GET_PHONE_INFO_ERROR,"請求返回錯誤"+responseCode));
             }

             urlConnection.disconnect();
         } catch (IOException e) {
             e.printStackTrace();
         }
     }

     private  String stream2String(InputStream inputStream, String gbk) throws  IOException{
         StringBuilder stringBuilder = new StringBuilder();
         InputStreamReader isr = new InputStreamReader(inputStream,gbk);
         BufferedReader br = new BufferedReader(isr);
        for (String tmp = br.readLine(); tmp!=null; tmp=br.readLine()){
            stringBuilder.append(tmp);
        }

        return  stringBuilder.toString();
     }

}
加粗樣式

這裡是引用

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout 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=".MainActivity">

<EditText
    android:id="@+id/input"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="嗚啦啦啦"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/quey"
    app:layout_constraintTop_toTopOf="parent"
    />

<Button
    android:id="@+id/quey"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="查詢"
    app:layout_constraintRight_toRightOf="parent"
   app:layout_constraintTop_toTopOf="parent"
    />


<TextView
    android:id="@+id/info"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:text="資訊!"
    app:layout_constraintTop_toBottomOf="@id/quey"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
     />

</android.support.constraint.ConstraintLayout>

在這裡插入圖片描述