android HttpURLConnection

本文详细介绍了Android环境下使用HttpURLConnection接口进行HTTP GET和POST请求的方法,包括建立连接、设置超时、输入输出流操作以及读取服务器响应内容等关键步骤。通过实例演示了如何获取网页内容及上传数据到服务器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android中提供的HttpURLConnection和HttpClient接口可以用来开发HTTP程序。
下面要说的是HttpURLConnection ,HttpURLConnection 有两种方式:
get和post

//HttpURLConnection get
               try {
                     URL url = new URL("http://www.51cto.com/index.jsp?par=123456" ); 
                      //使用HttpURLConnection打开连接 
                     HttpURLConnection urlConn=(HttpURLConnection)url.openConnection(); //去服务器求请
                     urlConn.setConnectTimeout(5*1000);
                      //设置输入和输出流 
                     urlConn.setDoOutput( true); 
                     urlConn.setDoInput( true);
                     
            //得到读取的内容(流) 
            InputStreamReader in = new InputStreamReader(urlConn.getInputStream());  //获取服务器返回的东西
            // 为输出创建BufferedReader 
            BufferedReader buffer = new BufferedReader(in); 
            String inputLine = null
            //使用循环来读取获得的数据 
            while (((inputLine = buffer.readLine()) != null)) 
            { 
                //我们在每一行后面加上一个"\n"来换行 
                resultData += inputLine + "\n"
            }          
            //关闭InputStreamReader 
            in.close(); 
            //关闭http连接 
            urlConn.disconnect();
              } catch (Exception e) {
                      // TODO: handle exception
              }


//HttpURLConnection post
               try {
                     URL url = new URL("http://www.51cto.com/index.jsp?par=123456" ); 
                      //使用HttpURLConnection打开连接 
                     HttpURLConnection urlConn=(HttpURLConnection)url.openConnection(); //去服务器求请
                     urlConn.setConnectTimeout(5*1000);
                      //设置输入和输出流 
                     urlConn.setDoOutput( true); 
                     urlConn.setDoInput( true);
                     urlConn.setRequestMethod( "POST");
                     urlConn.setRequestProperty( "Content-Type","application/x-www-form-urlencoded" ); 
            // 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成, 
            // 要注意的是connection.getOutputStream会隐含的进行connect。 
            urlConn.connect();
           
            DataOutputStream out = new DataOutputStream(urlConn.getOutputStream()); 
            //要上传的参数 
            String content = "par=" + URLEncoder.encode( "ABCDEFG", "gb2312" ); 
            //将要上传的内容写入流中 
            out.writeBytes(content);  
            //刷新、关闭 
            out.flush(); 
            out.close();
                     
            //得到读取的内容(流) 
            InputStreamReader in = new InputStreamReader(urlConn.getInputStream());  //获取服务器返回的东西
            // 为输出创建BufferedReader 
            BufferedReader buffer = new BufferedReader(in); 
            String inputLine = null
            //使用循环来读取获得的数据 
            while (((inputLine = buffer.readLine()) != null)) 
            { 
                //我们在每一行后面加上一个"\n"来换行 
                resultData += inputLine + "\n"
            }          
            //关闭InputStreamReader 
            in.close(); 
            //关闭http连接 
            urlConn.disconnect();
              } catch (Exception e) {
                      // TODO: handle exception
              }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值