Android中HttpUrlConnection使用步骤与总结

1.通过url的openConnection方法来开启一个HttpURLConnection(需要强转)。
2.设置HttpURLConnection对象的请求方式(常用的两种请求方式:GET、POST)。
3.设置HttpURLConnection对象的连接超时时间、读取超时时间。
4.获取HttpURLConnection的响应码。
5.根据响应码来判断(200:成功(HttpURLConnection.HTTP_OK)。
6.成功则获取输入流来读取数据到字节数组(byte[])中,用while循环来判断是否读完,并且获取读取的长度。
7.通过ByteArrayOutputStream字节数组输出流)来写入字节数组byte[])中的数据,
8.然后我们可以将ByteArrayOutputStream转换成字符串用于操作。


注:
1.我们的URL是GET的方式请求,那么就是网址+?+数据(英文状态的"?"问号),组成完整的URL,用来开启连接。
2.android4.0要求必须把和网络相关的操作放到子线程中。
3.使用网络方面和流相关的都是需要抛异常(即try/catch)。


new Thread() {
                    @Override
                    public void run() {
                        try {
//                            URL url = new URL("http://192.168.0.15:8080/androidJieKou/personServlet");
                            URL url = new URL("http://v.juhe.cn/toutiao/index?type=&key=8b13d671c5a2c4ffb94321ea8dcab509");
                            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                            httpURLConnection.setRequestMethod("GET");
                            httpURLConnection.setConnectTimeout(3000);
                            httpURLConnection.setReadTimeout(3000);
                            int responseCode = httpURLConnection.getResponseCode();
                            if (responseCode == httpURLConnection.HTTP_OK) {
                                //获取数据的输入流
                                InputStream inputStream = httpURLConnection.getInputStream();
//                                byte[] bytes = new byte[1024];
//                                inputStream.read();
                                //字节数组输出流,用来存储数据(inputStream中的数据)
                                ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
                                byte[] bytes = new byte[1024];

                                int length = 0;
                                //while循环判断读取inputStream.read(bytes),如果返回-1时,则说明读完了
                                while ((length = inputStream.read(bytes)) != -1) {
                                    //参数:1.字节数组(写入来源),  2.开始下标    3.存入长度
                                    arrayOutputStream.write(bytes, 0, length);
                                    //强制释放缓冲区
                                    arrayOutputStream.flush();
                                }
//                                
                                String s = arrayOutputStream.toString();
                                //sjson数据字符串类型(String                                
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                }.start();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了使用HttpURLConnection来发送和接收HTTP请求和响应,您需要按照以下步骤操作: 1. 创建HttpURLConnection对象,并将其连接到URL对象。例如: URL url = new URL("http://www.example.com"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 2. 设置请求方法和连接属性,如连接超时和读取超时。例如: conn.setRequestMethod("POST"); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); 3. 添加请求头(可选)。例如: conn.setRequestProperty("User-Agent", "Android"); conn.setRequestProperty("Content-Type", "application/json"); 4. 如果POST请求,添加请求体。例如: String requestBody = "{\"username\":\"user123\",\"password\":\"pass123\"}"; OutputStream outputStream = conn.getOutputStream(); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8")); writer.write(requestBody); writer.flush(); writer.close(); outputStream.close(); 5. 发送请求,获取响应码和响应数据。例如: int responseCode = conn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { InputStream inputStream = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder responseData = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { responseData.append(line); } reader.close(); inputStream.close(); String responseString = responseData.toString(); } 以上是使用HttpURLConnection发送和接收HTTP请求和响应的基本步骤。具体实现需根据具体的业务需求和网络情况进行调整和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值