HttpURLConnection


private void getid(){
    new Thread(){
        @Override
        public void run() {
            try {
                String data="userPhone="+s_phone+"&userPassword="+s_pass;
                URL url=new URL(urla);
                HttpURLConnection openConnection= (HttpURLConnection) url.openConnection();
                openConnection.setRequestMethod("POST");
                openConnection.setConnectTimeout(5000);
                openConnection.setReadTimeout(5000);
                openConnection.setDoOutput(true);// 准备写出
                openConnection.getOutputStream().write(data.getBytes());// 写出数据
                openConnection.connect();
                if (openConnection.getResponseCode()==200) {
                    InputStream in = openConnection.getInputStream();
                    BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));
                    String str;
                    StringBuffer sb = new StringBuffer();
                    while ((str = br.readLine()) != null) {
                        sb.append(str);
                    }
                    Message message = new Message();
                    message.what=0;
                    message.obj=sb.toString();
                    handler.sendMessage(message);
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }.start();
}
private void getid() {
    new Thread() {
        @Override
        public void run() {
            try {
                URL url = new URL(urla);
                //URL链接对象,通过URL对象打开一个connection链接对像
                HttpURLConnection openConnection = (HttpURLConnection) url.openConnection();

                openConnection.setRequestProperty("Accept-Charset", "GBK");
                openConnection.setRequestProperty("contentType", "GBK");
                openConnection.setConnectTimeout(5 * 1000);
                openConnection.setRequestMethod("GET");

                openConnection.connect();
                int flag = openConnection.getResponseCode();
                if (flag == 200) {
                    //调用urlConnection.getInputStream得到本次请求所返回的结果流
                    InputStream inStream = openConnection.getInputStream();
                    BufferedReader in = new BufferedReader(new InputStreamReader(inStream, "GBK"));

                    String readerli;
                    StringBuffer buffer = new StringBuffer();
                    while ((readerli = in.readLine()) != null) {
                        buffer.append(readerli);
                    }
                    Message message = new Message();
                    message.what = 0;
                    message.obj = buffer.toString();
                    handler.sendMessage(message);
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }.start();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值