HTTP获取号码的

<?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/ed"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text=""
    android:hint="请输入您的手机号"
    app:layout_constraintRight_toLeftOf="@id/btn"
    app:layout_constraintLeft_toLeftOf="parent"
     />
<Button
    android:id="@+id/btn"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="查询"
    app:layout_constraintRight_toRightOf="parent"
    />
<TextView
    android:id="@+id/tv"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintTop_toBottomOf="@id/btn"
    app:layout_constraintBottom_toBottomOf="parent"
    android:text="十年后尽快"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    />

</android.support.constraint.ConstraintLayout>
页面的布局
package com.example.day02;

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.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class MainActivity extends AppCompatActivity {

private TextView tv;
private Button btn;
private EditText ed;
private final int UPDATE_PHONE_INFO=0;
private final int GET_PHONE_INFO_ERROE=1;
private Handler handler=new Handler(){
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what){
            case UPDATE_PHONE_INFO:
                tv.setText((String) msg.obj);
                break;
            case GET_PHONE_INFO_ERROE:
                tv.setText((String) msg.obj);
                break;
        }
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = findViewById(R.id.tv);
    btn=findViewById(R.id.btn);
    ed=findViewById(R.id.ed);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    requestData(ed.getText().toString());
                }
            }).start();
        }
    });
}
private String apiUrl="https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=";
private void requestData(String s) {
    try {
        //请求地址
        URL url = new URL(apiUrl + s);
        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");
            //展示......................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
            Message message = handler.obtainMessage(UPDATE_PHONE_INFO, result);
            handler.sendMessage(message);
        }else{
            handler.sendMessage(handler.obtainMessage(GET_PHONE_INFO_ERROE,"请求错误"+responseCode));
        }
        urlConnection.disconnect();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值