android开发httpGet httpPost httpURLConnection httpClient
<p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; line-height: 18px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif;"><strong><span style="font-size:18px;">一、HttpClinet方式</span></strong></p><p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; line-height: 18px; color: rgb(68, 68, 68); font-family: tahoma, arial, sans-serif;"><strong><span style="font-size:18px;">1、HTTP GET 示例:</span></strong></p>
<span style="font-size:14px;"> 1 public class TestHttpGetMethod{ 2 public void get(){ 3 BufferedReader in = null; 4 try{ 5 HttpClient client = new DefaultHttpClient(); 6 HttpGet request = new HttpGet(); 7 request.setURI("http://w26.javaeye.com"); 8 HttpResponse response = client.execute(request); 9 in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 10 StringBuffer sb = new StringBuffer(""); 11 String line = ""; 12 String NL = System.getProperty("line.separator"); 13 while((line = in.readLine()) != null){ 14 sb.append(line + NL); 15 16 } 17 in.close(); 18 String page = sb.toString(); 19 Log.i(TAG, page); 20 }catch(Exception e){ 21 Log.e(TAG,e.toString()) 22 }finally{ 23 if(in != null){ 24 try{ 25 in.close(); 26 }catch(IOException ioe){ 27 Log.e(TAG, ioe.toString()); 28 } 29 } 30 } 31 } 32 }</span>
2、HTTP POST 示例:
<span style="font-size:14px;">1 public class TestHttpPostMethod{ 2 public void post(){ 3 BufferedReader in = null; 4 try{ 5 HttpClient client = new DefaultHttpClient(); 6 HttpPost request = new HttpPost("http://localhost/upload.jsp"); 7 List<NameValuePair> postParams = new ArrayList<NameValuePair>(); 8 postParams.add(new BasicNameValuePair("filename", "sex.mov")); 9 UrlEncodeFormEntity formEntity = new UrlEncodeFormEntity(postParams); 10 request.setEntity(formEntity); 11 HttpResponse response = client.execute(request); 12 in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 13 StringBuffer sb = new StringBuffer(""); 14 String line = ""; 15 String NL = System.getProperty("line.separator"); 16 while((line = in.readLine()) != null){ 17 sb.append(line + NL); 18 } 19 in.close(); 20 String result = sb.toString(); 21 Log.i(TAG, result ); 22 }catch(Exception e){ 23 Log.e(TAG,e.toString()) 24 }finally{ 25 if(in != null){ 26 try{ 27 in.close(); 28 }catch(IOException ioe){ 29 Log.e(TAG, ioe.toString()); 30 } 31 } 32 } 33 } 34 }</span>
<span style="font-size:14px;"> 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 //设置连接属性 11 conn.setDoOutput(true);// 使用 URL 连接进行输出 12 conn.setDoInput(true);// 使用 URL 连接进行输入 13 conn.setUseCaches(false);// 忽略缓存 14 conn.setConnectTimeout(30000);//设置连接超时时长,单位毫秒 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 if (data != null) { 22 out = conn.getOutputStream(); 23 out.write(data); 24 } 25 int code = conn.getResponseCode(); 26 if(code ==200){ 27 in = conn.getInputStream();// 可能造成阻塞 28 long len = conn.getContentLength(); 29 byte[] bs = new byte[(int) len];//返回结果字节数组 30 int all = 0; 31 int dn = 0; 32 while ((dn = in.read(bs, all, 1)) > 0) { 33 all += dn; 34 if (all == len) { 35 break; 36 } 37 } 38 } 39 }</span>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。