HttpURLConnection的POST请求

手机归属地查询(整个类)

package com.example.day03_post;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    protected static final int SUCCESS = 1;
    protected static final int FAIL = 0;
    private EditText ed_phonenum;
    private TextView tv_showdata;
    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
            case 1:
                String cdata = (String) msg.obj;
                tv_showdata.setText(cdata);
                break;
            case 0:
                String info = (String) msg.obj;
                // 吐司要在主线程中写,写在子线程中报错***********
                Toast.makeText(MainActivity.this, info, 0).show();
                break;
            default:
                break;
            }
        };
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        ed_phonenum = (EditText) findViewById(R.id.ed_phonenum);
        tv_showdata = (TextView) findViewById(R.id.tv_showdata);
    }

    public void loadphonewonership(View v) {
        new Thread() {
            private HttpURLConnection openConnection;

            public void run() {
                String phmum = ed_phonenum.getText().toString();
                // http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=15850781443
                try {
                    // 实例化URL,设置网址
                    URL url = new URL(
                            "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm");
                    // 实例化HttpURLConnection.(打开连接)
                    openConnection = (HttpURLConnection) url.openConnection();
                    // 设置请求方法为POST
                    openConnection.setRequestMethod("POST");
                    // 是否向服务器发送数据
                    openConnection.setDoOutput(true);
                    StringBuffer sb = new StringBuffer();
                    sb.append("tel=").append(phmum);
                    byte[] buffer = sb.toString().getBytes();
                    // 向服务器发送数据(字节)
                    openConnection.getOutputStream().write(buffer);
                    // 如果请求成功
                    if (openConnection.getResponseCode() == 200) {
                        // 得到请求到的数据
                        InputStream inputStream = openConnection
                                .getInputStream();
                        ByteArrayOutputStream arrayout = new ByteArrayOutputStream();
                        byte[] b = new byte[1024];
                        int length = 0;
                        // 边读边写*******************
                        while ((length = inputStream.read(b)) != -1) {
                            arrayout.write(b, 0, length);
                        }
                        // 写完关流
                        arrayout.close();
                        // 以gbk形式写出来
                        String data = arrayout.toString("gbk");

                        Message message = Message.obtain();
                        message.obj = data;
                        message.what = SUCCESS;
                        // 向handle发送消息
                        handler.sendMessage(message);
                    } else {
                        // 请求数据失败,在此和 catch中都需要写所以封装成一个方法
                        requestfail();
                    }
                } catch (MalformedURLException e) {
                    requestfail();
                    e.printStackTrace();
                } catch (IOException e) {
                    requestfail();
                    e.printStackTrace();
                } finally {// 最后断开连接
                    if (openConnection != null) {
                        openConnection.disconnect();
                    }
                }
            }

            private void requestfail() {
                Message mesage = Message.obtain();
                mesage.what = FAIL;
                mesage.obj = "请求数据失败";
                handler.sendMessage(mesage);
            };
        }.start();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值