HttpURLConnection的POST请求

来源:互联网 发布:盛发软件 编辑:程序博客网 时间:2024/06/05 04:14

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

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();    }}
0 0
原创粉丝点击