android 网络请求中框架类的原理demo

大多数框架封装的都是java.net包中的HttpURLConnection类。


  public void useGet(View view)  {
        new Thread(){
            @Override
            public void run() {
                try {
                    get();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();


    }
    public void get()throws Exception{
        String path = "http://www.baidu.com";
        URL url= new URL(path);


        HttpURLConnection connection = (HttpURLConnection) url.openConnection();




        connection.setConnectTimeout(60000);
        connection.connect();




        if(connection.getResponseCode() == 200){
            InputStream inputStream = connection.getInputStream();
            byte[]data = readStream(inputStream);
            Log.i("wdd","get data is "+new String(data,"UTF-8"));
        }else {
            Log.i("wdd","get data failed ");


        }
    }


    public void usePost(View view) {
        new Thread(){
            @Override
            public void run() {
                try {
                    post();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();


    }
public void post()throws Exception {
    String path = "http://www.baidu.com";
    String params = "id=" + URLEncoder.encode("helloworld", "UTF-8")
            + "&pwd=" + URLEncoder.encode("android", "UTF-8");
    byte[] postData = params.getBytes();
    URL url = new URL(path);
    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();




    httpURLConnection.setConnectTimeout(5*1000);


    httpURLConnection.setDoOutput(true);
    httpURLConnection.setUseCaches(false);
    httpURLConnection.setRequestMethod("POST");
    httpURLConnection.setInstanceFollowRedirects(true);
    httpURLConnection.setRequestProperty("Content-Type",
            "application/x-www-form-urlencode");




    // Send parameter
    DataOutputStream dos = new DataOutputStream(httpURLConnection.getOutputStream());
    dos.write(postData);
    dos.flush();
    dos.close();




    httpURLConnection.connect();


    if (httpURLConnection.getResponseCode() == 200) {
        // 获取返回的数据
        byte[] data = readStream(httpURLConnection.getInputStream());
        Log.i("wdd", "Post请求方式成功,返回数据如下:");
        Log.i("wdd", new String(data, "UTF-8"));
    } else {
        Log.i("wdd", "Post方式请求失败");
    }


}
    public static byte[] readStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = inStream.read(buffer)) != -1) {
            outSteam.write(buffer, 0, len);
        }
        outSteam.close();
        inStream.close();
        return outSteam.toByteArray();
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值