HTTP

package com.example.day2;

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.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.Executor;

public class MainActivity extends AppCompatActivity {

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

private final int UPDATE_PHONE_INFO = 0;
private final int GET_PHONE_INFO_ERROR = 1;

//thread
//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);

    input = findViewById(R.id.input);
    input.setText("18679906337");
    //快速提取变量
    //ctrl + alt + v 局部
    //ctrl + alt + f 全局
    query = findViewById(R.id.query);//shift + f6 快捷重构
    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 void s() {
    //线程
    //Thread;
    //线程池
    //Executor;
    //new Thread().run();

    new Thread(){
        @Override
        public void run() {
            super.run();
            requestData("");
        }
    }
            //开启  执行
            .start();
}

private String apiUrl = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=";

//重定向
private void requestData(String phoneNum) {
    //做网络操作,必须在子线程中做
    //查询手机号
    //java对http协议的简单封装 HttpUrlConnection
    try {
        //请求地址
        URL url = new URL(apiUrl + phoneNum);
        //连接  ctrl + h 查看继承结构
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();//
        //请求方法
        urlConnection.setRequestMethod("GET");
        //连接超时时间
        urlConnection.setConnectTimeout(5000);
        //读取超时时间
        urlConnection.setReadTimeout(5000);
        //获取返回结果: 状态码
        //响应状态码  (anr)   NetworkOnMainThreadException
        int responseCode = urlConnection.getResponseCode();
        if(responseCode == 200) {//请求成功
            //获取结果
            String result = stream2String(urlConnection.getInputStream(), "gbk");
            //展示
            //info.setText(result);
            //ctrl + q 查看文档
            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 (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private String stream2String(InputStream inputStream, String charsetName) throws IOException {
    StringBuilder stringBuilder = new StringBuilder();

    InputStreamReader isr = new InputStreamReader(inputStream, charsetName);

    BufferedReader br = new BufferedReader(isr);

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

    return stringBuilder.toString();
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值