Android学习笔记--Http协议

Posted 纯属浪费8818

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android学习笔记--Http协议相关的知识,希望对你有一定的参考价值。

HttpURLConnection 分别有GET和POST请求
Post方式的
 1     public void testPOstbendi() throws Exception {
 2         //构建服务器地址
 3         URL url = new URL("http://192.168.40.194/qiantai/admin/login_check.php");
 4         //获取HttpURLConnection对象
 5         HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
 6         //设置请求时间
 7         httpURLConnection.setReadTimeout(10000);
 8         httpURLConnection.setConnectTimeout(10000);
 9         //设置方式
10         httpURLConnection.setRequestMethod("POST");
11         //建立一个实际的链接
12         httpURLConnection.connect();
13         //获取输出流
14         OutputStream os = httpURLConnection.getOutputStream();
15         //POST提交的字段
16         String message ="username=admin&userpass=123456";
17         //想服务器写入数据
18         os.write(message.getBytes());
19         //获取服务端返回的状态码
20         int code = httpURLConnection.getResponseCode();
21         //switch判断一下
22         switch (code){
23             case 201:
24             case 200:
25                 //获取服务器返回的输入流
26                 InputStream inputStream = httpURLConnection.getInputStream();
27                 StringBuffer stringBuffer = new StringBuffer();
28                 String strings;
29                 //转换成字符输入流
30                 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
31                 while ((strings = bufferedReader.readLine())!= null){
32                     stringBuffer.append(strings);
33                 }
34                 //关闭数据链接
35                 httpURLConnection.disconnect();
36                 Log.e(TAG, "testURl: "+stringBuffer.toString());
37                 break;
38             case 401:
39                 Log.e(TAG, "tes401了啊==========================================================");
40                 break;
41         }
42     }

GET方式的

    public void testURl() throws Exception {
        //构建URL
        URL url = new URL("http://life.tenpay.com/cgi-bin/mobile/MobileQueryAttribution.cgi?chgmobile=15850781443");
        //获取httpURLConnection
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        //设置超市时间
        httpURLConnection.setConnectTimeout(10000);
        httpURLConnection.setReadTimeout(10000);
        //设置请求方式
        httpURLConnection.setRequestMethod("GET");
        //获取请求码
        int code = httpURLConnection.getResponseCode();
        switch (code){
            case 200:
                InputStream inputStream = httpURLConnection.getInputStream();
                StringBuffer sb = new StringBuffer();
                String str;
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                while ((str = reader.readLine()) != null){
                    sb.append(str+"\n");
                }
                Log.e(TAG, "testURl: "+sb.toString());
                break;
            case 401:
                Log.e(TAG, "testURl: 401");
                break;
        }
    }

 

 

以上是关于Android学习笔记--Http协议的主要内容,如果未能解决你的问题,请参考以下文章

androi 多线程

HTTP协议学习笔记

安全牛学习笔记HTTP协议

Android usb学习笔记:Android AOA协议Android端 流程总结

HTTP协议学习笔记

HTTP协议学习笔记