获取电话信息的post网络请求

XML的代码:

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

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/text"
        android:layout_alignParentEnd="true"
        android:text="获取" />

    <TextView
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/text"
        android:layout_margin="5dp"
        />

Activity中的代码:

 	private final int UPDATE_UI=1;
    private String lujing = "http://tcc.taobao.com/cc/json/mobile_tel_segment.htm";
    private EditText text;
    private TextView textView;
    private Button button;
    @SuppressLint("HandlerLeak")
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case UPDATE_UI:
                    textView.setText(msg.obj.toString());
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = findViewById(R.id.text);
        textView = findViewById(R.id.textview);
        button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!keyong()){
                    Toast.makeText(MainActivity.this, "网络不可用", Toast.LENGTH_SHORT).show();
                    return ;
                }
                new Thread(
                        new Runnable() {
                            @Override
                            public void run() {
                                huoqu(text.getText().toString());
                            }
                        }
                ).start();
            }
        });
    }
    //判断是否有可用的网络
    private boolean keyong(){
        //获取网络连接管理服务
        ConnectivityManager cm= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        //需要访问网络状态权限
        NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo();
        return activeNetworkInfo!=null&&activeNetworkInfo.isAvailable();
    }
    private void huoqu(String num){
        try {
            //定义url地址
            URL url=new URL(lujing);
            //打开连接
            HttpURLConnection urlConnection= (HttpURLConnection) url.openConnection();
            //请求方法
            urlConnection.setRequestMethod("POST");
            //读取超时
            urlConnection.setReadTimeout(5000);
            //连接超时
            urlConnection.setConnectTimeout(5000);
            //输出标识
            urlConnection.setDoOutput(true);
            //向服务器输出数据
            urlConnection.getOutputStream().write(("tel="+num).getBytes());
            //刷新适配器
            urlConnection.getOutputStream().flush();
            //获取状态值
            int responseCode = urlConnection.getResponseCode();
            //接受数据
            String result;
            if (responseCode==200){
                result=zifu(urlConnection.getInputStream(),"gbk");
            }else{
                result="error"+responseCode;
            }
            handler.sendMessage(handler.obtainMessage(UPDATE_UI,result));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //字节流转化为字符流
    private String zifu(InputStream zijie,String geshi) throws IOException {
        InputStreamReader inputStreamReader=new InputStreamReader(zijie,geshi);
        BufferedReader buff=new BufferedReader(inputStreamReader);
        StringBuilder builder=new StringBuilder();
        for (String imp=buff.readLine();imp!=null;imp=buff.readLine()){
            builder.append(imp);
        }
        return builder.toString();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值