HttpURLConnection

Get 方式:
String result = null;
URL url = null;
HttpURLConnection connection = null;
InputStreamReader in = null;

private void sendRequestWithHttpClient(String url) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                url = new URL(url);
                connection = (HttpURLConnection) url.openConnection();
                in = new InputStreamReader(connection.getInputStream());
                BufferedReader bufferedReader = new BufferedReader(in);
                StringBuffer strBuffer = new StringBuffer();
                String line = null;
                while ((line = bufferedReader.readLine()) != null) {
                    strBuffer.append(line);
                }
                result = strBuffer.toString();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (connection != null) {
                    connection.disconnect();
                }
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }
        }
    }).start();
}

POST方式:
 new Thread(
         new Runnable() {
                        @Override
                        public void run() {
                            String json="{'UserID':'"+account+"','Authenticator':'"+value+"','TerminalFlag':8}";                      
                           postDownloadJson("http://xxx.xxxx",json);
                        }
     }).start();


public String postDownloadJson(String path, String Json) {
    String result = "";
    BufferedReader reader = null;
    HttpURLConnection conn=null;
    try {
        URL url = new URL(path);
        conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setUseCaches(false);
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setRequestProperty("Charset", "UTF-8");
        // 设置文件类型:
        conn.setRequestProperty("Content-Type","application/json; charset=UTF-8");

        conn.setRequestProperty("accept","application/json");
        // 往服务器里面发送数据
        if (Json != null && !TextUtils.isEmpty(Json)) {
            byte[] writebytes = Json.getBytes();
            // 设置文件长度
            conn.setRequestProperty("Content-Length", String.valueOf(writebytes.length));
            OutputStream outwritestream = conn.getOutputStream();
            outwritestream.write(Json.getBytes());
            outwritestream.flush();
            outwritestream.close();
        }
        if (conn.getResponseCode() == 200) {
            reader = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            result = reader.readLine();

        }
        Message msg=new Message();
        msg.obj=result;
        handler.sendMessage(msg);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return result;
}


Handler handler=new Handler(new Handler.Callback() {
    @Override
    public boolean handleMessage(Message msg) {
        Toast.makeText(MainActivity.this,msg.obj+"",Toast.LENGTH_LONG).show();
        return false;
    }
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值