Android中提供的HttpURLConnection和HttpClient接口可以用来开发HTTP程序。以下是本人在学习中的总结与归纳。
HttpURLConnection接口
首先需要明确的是,Http通信中的POST和GET请求方式的不同。GET可以获得静态页面,也可以把参数放在URL字符串后面,传递给服务器。而POST方法的参数是放在Http请求中。因此,在编程之前,应当首先明确使用的请求方法,然后再根据所使用的方式选择相应的编程方式。
HttpURLConnection是继承于URLConnection类,二者都是抽象类。其对象主要通过URL的openConnection方法获得。创建方法如下代码所示:


                        
  1. URLurl=newURL("http://www.51cto.com/index.jsp?par=123456");
  2. HttpURLConnectionurlConn=(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();


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


                        
  1. //使用HttpURLConnection打开连接
  2. HttpURLConnectionurlConn=(HttpURLConnection)url.openConnection();
  3. //得到读取的内容(流)
  4. InputStreamReaderin=newInputStreamReader(urlConn.getInputStream());
  5. //为输出创建BufferedReader
  6. BufferedReaderbuffer=newBufferedReader(in);
  7. StringinputLine=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. StringhttpUrl="http://192.168.1.110:8080/httpget.jsp";
  2. //获得的数据
  3. StringresultData="";
  4. URLurl=null;
  5. try
  6. {
  7. //构造一个URL对象
  8. url=newURL(httpUrl);
  9. }
  10. catch(MalformedURLExceptione)
  11. {
  12. Log.e(DEBUG_TAG,"MalformedURLException");
  13. }
  14. if(url!=null)
  15. {
  16. try
  17. {
  18. //使用HttpURLConnection打开连接
  19. HttpURLConnectionurlConn=(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. DataOutputStreamout=newDataOutputStream(urlConn.getOutputStream());
  35. //要上传的参数
  36. Stringcontent="par="+URLEncoder.encode("ABCDEFG","gb2312");
  37. //将要上传的内容写入流中
  38. out.writeBytes(content);
  39. //刷新、关闭
  40. out.flush();
  41. out.close();

更多相关文章

  1. Android字符串资源文件format方法使用实例
  2. Android应用程序换肤实现系列(二)
  3. android fragmnet重叠问题解决方案
  4. Android四大核心组件之一-----Service(服务)的基本知识
  5. 旋屏时,OnCreate方法重复调用的解决
  6. React Native调用Android原生方法
  7. android仿iPhone滚轮控件实现及源码分析(二)
  8. TCL 雏鹰飞翔计划 Android篇
  9. Android开发中怎样调用系统Email发送邮件(多种调用方式)

随机推荐

  1. editView多行光标位置问题和联系人问题
  2. Android(安卓)Edittext 显示光标 获取焦
  3. Android(安卓)之 ImageSwitch图片转换器
  4. Android(安卓)使用 第三方 Sweet Alert D
  5. JS判断客户端是否是iOS或者Android
  6. Android(安卓)NDK学习(1) 简介
  7. EditText 所有属性
  8. 】Android横竖屏总结
  9. Android(安卓)8.1 zygote创建新应用进程
  10. android之layout_toLeftOf和layout_toRig