1. 程式人生 > >手機號查詢get請求!!!加網路許可權

手機號查詢get請求!!!加網路許可權

package com.example.day1;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
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 java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class MainActivity extends AppCompatActivity {

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

private final int UPDATE_UI = 1;

@SuppressLint(“HandlerLeak”) private Handler mHandler = new Handler() {
@Override public void handleMessage(Message msg) {
switch (msg.what) {
case UPDATE_UI:
info.setText(msg.obj.toString());
break;
}
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//main 主執行緒
setContentView(R.layout.activity_main);

//ctrl alt f
query = findViewById(R.id.query);
input = findViewById(R.id.input);
info = findViewById(R.id.info);

query.setOnClickListener(new View.OnClickListener() {
  @Override public void onClick(View v) {

    new Thread(
        new Runnable() {
          @Override public void run() {
            //子執行緒
            //SystemClock.sleep(6000);

            Log.i("TEST", Thread.currentThread().getName());
            queryPhonNumInfo(input.getText().toString());
          }
        }
    ).start();

    /*new Thread(){
      @Override public void run() {
        super.run();
      }
    }.start();*/
  }
});

}

//private String baseUrlStr = “http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=”;
private String baseUrlStr = “https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=”;

private void queryPhonNumInfo(String phonNum) {
//獲取資訊

try {
  //1 定義 URL 地址
  URL url = new URL(baseUrlStr + phonNum);
  //http https ftp
  //ctrl + h 檢視類的繼承結構
  //URLConnection
  //ctrl + q 檢視方法資訊
  //2 開啟連線
  HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
  //3 設定:請求方法;超時
  //請求 方法 GET POST
  urlConnection.setRequestMethod("GET");
  //超時設定
  //讀取超時
  urlConnection.setReadTimeout(5000);
  //連線超時
  urlConnection.setConnectTimeout(5000);
  //4 連線並獲取狀態碼
  //////////////////////////////////
  int responseCode = urlConnection.getResponseCode();
  if (responseCode == 200) {
    //5 讀取位元組流轉換成字串結果
    String result = stream2String(urlConnection.getInputStream());
    //操作ui  子執行緒不要操作ui
    Message message = mHandler.obtainMessage(UPDATE_UI, result);
    //Message msg = new Message();
    mHandler.sendMessage(message);
    //info.setText(result);
  } else {
    Message message = mHandler.obtainMessage(UPDATE_UI, "error: " + responseCode);
    //info.setText("error: " + responseCode);
    mHandler.sendMessage(message);
  }
} catch (MalformedURLException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
}

//PUT DELETE

//

}

//將位元組流轉換成字元
private String stream2String(InputStream is) throws IOException {

/*InputStream is;
InputStreamReader isr;
BufferedReader br;

OutputStream os;
OutputStreamWriter osw;
BufferedWriter bw;*/

InputStreamReader isr = new InputStreamReader(is, "gbk");
BufferedReader br = new BufferedReader(isr);
StringBuilder stringBuilder = new StringBuilder();

for (String tmp = br.readLine(); tmp != null; tmp = br.readLine()) {
  stringBuilder.append(tmp);
}

return stringBuilder.toString();

}

//網路操作需要申請許可權

//主執行緒不能做網路 及 耗時 操作

//子執行緒中不能更新ui

//Handler 執行緒間通訊
}