android网络编程——http post 和 http get
来源:互联网 发布:淘宝企业店铺名字大全 编辑:程序博客网 时间:2024/05/14 11:02
- public class HttpPostDemo extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- BufferedReader in = null;
- try {
- HttpClient client = new DefaultHttpClient();
- HttpPost request = new HttpPost("http://mysomewebsite.com/services/doSomething.do");
- List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
- postParameters.add(new BasicNameValuePair("username", "test"));
- postParameters.add(new BasicNameValuePair("password", "test1234"));
- UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
- postParameters);
- request.setEntity(formEntity);
- HttpResponse response = client.execute(request);
- in = new BufferedReader(
- new InputStreamReader(
- response.getEntity().getContent()));
- StringBuffer sb = new StringBuffer("");
- String line = "";
- String NL = System.getProperty("line.separator");
- while ((line = in.readLine()) != null) {
- sb.append(line + NL);
- }
- in.close();
- String result = sb.toString();
- System.out.println(result);
- } catch(Exception e) {
- // Do something about exceptions
- } finally {
- if (in != null) {
- try {
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
HtttpGet 请求
在Android SDK中提供了Apache HttpClient(org.apache.http.*)模块。在这个模块中涉及到两个重要的类:HttpGet和HttpPost。这一篇一个实例给出httpGet的使用方法:
- public class HttpGetDemo extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- BufferedReader in = null;
- try {
- HttpClient client = new DefaultHttpClient();
- HttpGet request = new HttpGet("http://www.baidu.com");
- HttpResponse response = client.execute(request);
- in = new BufferedReader(
- new InputStreamReader(
- response.getEntity().getContent()));
- StringBuffer sb = new StringBuffer("");
- String line = "";
- String NL = System.getProperty("line.separator");
- while ((line = in.readLine()) != null) {
- sb.append(line + NL);
- }
- in.close();
- String page = sb.toString();
- System.out.println(page);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally {
- if (in != null) {
- try {
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
在Android SDK中提供了Apache HttpClient(org.apache.http.*)模块。在这个模块中涉及到两个重要的类:HttpGet和HttpPost,他们有共性也有不同。
HttpGet和HttpPost创建方式相同:
1、创建HttpGet(或HttpPost)对象,将要请求的URL通过构造方法传入HttpGet(或HttpPost)对象中;
2、使用DefaultHttpClient类的execute方法发送HTTP GET或HTTP POST 请求,并返回HttpResponse对象;
3、通过HttpResponse接口的getEntity方法返回响应信息。
HttpGet和HttpPost不同点,HttpPost在使用是需要传递参数 ,使用List<NameValuePair>添加参数。
- <span style="font-weight: normal;"> List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
- postParameters.add(new BasicNameValuePair("username", "test"));
- postParameters.add(new BasicNameValuePair("password", "test1234"));</span>
- <span style="font-weight: normal;"> List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
- postParameters.add(new BasicNameValuePair("username", "test"));
- postParameters.add(new BasicNameValuePair("password", "test1234"));</span>
0 0
- android网络编程——http post 和 http get
- android网络编程——http post
- android网络编程——http post
- android网络编程——http post
- android网络编程——http get
- android网络编程——http get
- android网络编程——http get
- 【Android】【网络编程】网络协议,HTTP,TCP,Socket,Get,Post
- android网络编程http的get,post方式
- Android网络编程之Http通信(post,get)
- HTTP请求 GET POST 网络编程实现
- Android 网络交互(一) HTTP post和get方法区别
- Android中的http带参数网络请求,GET和POST
- Http访问网络之GET和POST
- HTTP-GET和HTTP-POST- -
- HTTP-GET和HTTP-POST
- HTTP-GET 和 HTTP-POST
- HTTP-GET 和 HTTP-POST
- 程序员面试金典(4)移除未排序链表的重复节点
- Windows I/O模型、同步/异步、阻塞/非阻塞
- js解析xml的浏览器兼容
- tiny6410 :/init: line 103: can't open /r/dev/console: no such file问题的解决
- 使用Dom4j解析XML
- android网络编程——http post 和 http get
- 在HTM上运行单视点全I帧情况
- POJ 1466 - Girls and Boys(二分图匹配)
- Redis学习笔记(一)---redis安装与部署
- 用户模式下线程同步(一)
- c语言常量
- 第7、8、9、10、11、12题
- Android 系统属性SystemProperty分析 Android System Property 一 System Property
- 解决通过PathVariable接收数据的乱码问题