现在的位置: 首页 > 移动开发 > 正文

Android 的网络编程(3)-HttpURLConnection接口

2019年09月08日 移动开发 ⁄ 共 3095字 ⁄ 字号 评论关闭

Android中提供的HttpURLConnection和HttpClient接口可以用来开发HTTP程序。以下是本人在学习中的总结与归纳。
HttpURLConnection接口

    首先需要明确的是,Http通信中的POST和GET请求方式的不同。GET可以获得静态页面,也可以把参数放在URL字符串后面,传递给服务器。而POST方法的参数是放在Http请求中。因此,在编程之前,应当首先明确使用的请求方法,然后再根据所使用的方式选择相应的编程方式。

    HttpURLConnection是继承于URLConnection类,二者都是抽象类。其对象主要通过URL的openConnection方法获得。创建方法如下代码所示:

 

 

  1. URL url = new URL("http://www.51cto.com/index.jsp?par=123456");  
  2. HttpURLConnection urlConn=(HttpURLConnection)url.openConnection(); 

   

    通过以下方法可以对请求的属性进行一些设置,如下所示:

 

 

  1. //设置输入和输出流  
  2. urlConn.setDoOutput(true);  
  3. urlConn.setDoInput(true);  
  4. //设置请求方式为POST  
  5. urlConn.setRequestMethod("POST");  
  6. //POST请求不能使用缓存  
  7. urlConn.setUseCaches(false);  
  8. //关闭连接  
  9. urlConn.disConnection();  
  10.  



HttpURLConnection默认使用GET方式,例如下面代码所示:

 

 

  1. //使用HttpURLConnection打开连接  
  2.                 HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();  
  3.                 //得到读取的内容(流)  
  4.                 InputStreamReader in = new InputStreamReader(urlConn.getInputStream());  
  5.                 // 为输出创建BufferedReader  
  6.                 BufferedReader buffer = new BufferedReader(in);  
  7.                 String inputLine = null;  
  8.                 //使用循环来读取获得的数据  
  9.                 while (((inputLine = buffer.readLine()) != null))  
  10.                 {  
  11.                     //我们在每一行后面加上一个"\n"来换行  
  12.                     resultData += inputLine + "\n";  
  13.                 }           
  14.                 //关闭InputStreamReader  
  15.                 in.close();  
  16.                 //关闭http连接  
  17.                 urlConn.disconnect(); 



    如果需要使用POST方式,则需要setRequestMethod设置。代码如下:

 

 

  1. String httpUrl = "http://192.168.1.110:8080/httpget.jsp";  
  2.         //获得的数据  
  3.         String resultData = "";  
  4.         URL url = null;  
  5.         try 
  6.         {  
  7.             //构造一个URL对象  
  8.             url = new URL(httpUrl);   
  9.         }  
  10.         catch (MalformedURLException e)  
  11.         {  
  12.             Log.e(DEBUG_TAG, "MalformedURLException");  
  13.         }  
  14.         if (url != null)  
  15.         {  
  16.             try 
  17.             {  
  18.                 // 使用HttpURLConnection打开连接  
  19.                 HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();  
  20.                 //因为这个是post请求,设立需要设置为true  
  21.                 urlConn.setDoOutput(true);  
  22.                 urlConn.setDoInput(true);  
  23.                 // 设置以POST方式  
  24.                 urlConn.setRequestMethod("POST");  
  25.                 // Post 请求不能使用缓存  
  26.                 urlConn.setUseCaches(false);  
  27.                 urlConn.setInstanceFollowRedirects(true);  
  28.                 // 配置本次连接的Content-type,配置为application/x-www-form-urlencoded的  
  29.                 urlConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");  
  30.                 // 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成,  
  31.                 // 要注意的是connection.getOutputStream会隐含的进行connect。  
  32.                 urlConn.connect();  
  33.                 //DataOutputStream流  
  34.                 DataOutputStream out = new DataOutputStream(urlConn.getOutputStream());  
  35.                 //要上传的参数  
  36.                 String content = "par=" + URLEncoder.encode("ABCDEFG""gb2312");  
  37.                 //将要上传的内容写入流中  
  38.                 out.writeBytes(content);   
  39.                 //刷新、关闭  
  40.                 out.flush();  
  41.                 out.close();  

抱歉!评论已关闭.