android 实现http请求

一、HttpClinet方式

1、HTTP GET 示例:

[java]  view plain copy
  1. public class TestHttpGetMethod{    
  2.     public void get(){    
  3.         BufferedReader in = null;    
  4.     
  5.         try{    
  6.             HttpClient client = new DefaultHttpClient();    
  7.             HttpGet request = new HttpGet();    
  8.             request.setURI("http://w26.javaeye.com");    
  9.             HttpResponse response = client.execute(request);    
  10.                 
  11.             in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));       
  12.             StringBuffer sb = new StringBuffer("");     
  13.             String line = "";    
  14.             String NL = System.getProperty("line.separator");    
  15.             while((line = in.readLine()) != null){    
  16.                 sb.append(line + NL);    
  17.             }    
  18.             in.close();    
  19.             String page = sb.toString();    
  20.     
  21.             Log.i(TAG, page);    
  22.     
  23.     
  24.     
  25.         }catch(Exception e){    
  26.             Log.e(TAG,e.toString())    
  27.         }finally{    
  28.             if(in != null){    
  29.                 try{    
  30.                     in.close();    
  31.                 }catch(IOException ioe){    
  32.                     Log.e(TAG, ioe.toString());    
  33.                 }    
  34.             }    
  35.     
  36.         }    
  37.     }    
  38. }   

 

带参数的 HTTP GET: 

[java]  view plain copy
  1. HttpGet request = new HttpGet("http://www.baidu.com/s?wd=amos_tl");    
  2. client.execute(request);  

 

2、HTTP POST 示例:

[java]  view plain copy
  1. public class TestHttpPostMethod{    
  2.     public void post(){    
  3.         BufferedReader in = null;    
  4.     
  5.         try{    
  6.             HttpClient client = new DefaultHttpClient();    
  7.             HttpPost request = new HttpPost("http://localhost/upload.jsp");    
  8.     
  9.             List<NameValuePair> postParams = new ArrayList<NameValuePair>();    
  10.             postParams.add(new BasicNameValuePair("filename""sex.mov"));    
  11.                 
  12.             UrlEncodeFormEntity formEntity = new UrlEncodeFormEntity(postParams);    
  13.             request.setEntity(formEntity);    
  14.                 
  15.             HttpResponse response = client.execute(request);    
  16.     
  17.             in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));       
  18.             StringBuffer sb = new StringBuffer("");     
  19.             String line = "";    
  20.             String NL = System.getProperty("line.separator");    
  21.             while((line = in.readLine()) != null){    
  22.                 sb.append(line + NL);    
  23.             }    
  24.             in.close();    
  25.             String result = sb.toString();    
  26.             Log.i(TAG, result );    
  27.     
  28.         }catch(Exception e){    
  29.             Log.e(TAG,e.toString())    
  30.         }finally{    
  31.             if(in != null){    
  32.                 try{    
  33.                     in.close();    
  34.                 }catch(IOException ioe){    
  35.                     Log.e(TAG, ioe.toString());    
  36.                 }    
  37.             }    
  38.     
  39.         }    
  40.     }    
  41. }   

 

二、HttpURLConnection 方式

[java]  view plain copy
  1. URL url = null;  
  2. HttpURLConnection conn = null;  
  3. InputStream in = null;  
  4. OutputStream out = null;  
  5. byte[] data ="测试字符串".getBytes();  
  6. try{  
  7.    url =new URL("www.xxx.com/servlet");  
  8.    conn = (HttpURLConnection) url.openConnection();  
  9.    //设置连接属性  
  10.    conn.setDoOutput(true);// 使用 URL 连接进行输出  
  11.    conn.setDoInput(true);// 使用 URL 连接进行输入  
  12.    conn.setUseCaches(false);// 忽略缓存  
  13.    conn.setConnectTimeout(30000);//设置连接超时时长,单位毫秒  
  14.     
  15.    conn.setRequestMethod("POST");//设置请求方式,POST or GET,注意:如果请求地址为一个servlet地址的话必须设置成POST方式  
  16.     
  17.   //设置请求头  
  18.   conn.setRequestProperty("Accept""*/*");  
  19.   conn.setRequestProperty("Connection""Keep-Alive");  
  20.   conn.setRequestProperty("Accept-Charset""utf-8");  
  21.    
  22.   if (data != null) {  
  23.      out = conn.getOutputStream();  
  24.      out.write(data);  
  25.   }  
  26.   int code = conn.getResponseCode();  
  27.   if(code ==200){  
  28.      in = conn.getInputStream();// 可能造成阻塞  
  29.      long len = conn.getContentLength();  
  30.      byte[] bs = new byte[(int) len];//返回结果字节数组  
  31.      int all = 0;  
  32.      int dn = 0;  
  33.      while ((dn = in.read(bs, all, 1)) > 0) {  
  34.        all += dn;  
  35.        if (all == len) {  
  36.        break;  
  37.        }  
  38.      }  
  39.   }  
  40. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值