Chinaunix首页 | 论坛 | 博客
  • 博客访问: 209708
  • 博文数量: 102
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1015
  • 用 户 组: 普通用户
  • 注册时间: 2013-06-05 16:45
文章存档

2014年(73)

2013年(29)

我的朋友

分类: Android平台

2014-04-08 21:56:48

09_HTTP操作(三) (HTTP请求方法:1、GET方法发送请求,2POS方法发送请求)

点击(此处)折叠或打开

  1. package com.lwb.my04_09_http03;

  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.io.UnsupportedEncodingException;
  7. import java.util.ArrayList;
  8. import java.util.List;

  9. import org.apache.http.HttpEntity;
  10. import org.apache.http.HttpResponse;
  11. import org.apache.http.NameValuePair;
  12. import org.apache.http.client.ClientProtocolException;
  13. import org.apache.http.client.HttpClient;
  14. import org.apache.http.client.entity.UrlEncodedFormEntity;
  15. import org.apache.http.client.methods.HttpGet;
  16. import org.apache.http.client.methods.HttpPost;
  17. import org.apache.http.impl.client.DefaultHttpClient;
  18. import org.apache.http.message.BasicNameValuePair;

  19. import android.os.Bundle;
  20. import android.app.Activity;
  21. import android.view.Menu;
  22. import android.view.View;
  23. import android.view.View.OnClickListener;
  24. import android.widget.Button;
  25. import android.widget.EditText;

  26. public class MainActivity extends Activity {
  27.     private Button getButton = null;
  28.     private Button postButton = null;
  29.     private EditText nameView = null;
  30.     private EditText ageView = null;
  31.     private String baseUrl ="";
  32.     @Override
  33.     protected void onCreate(Bundle savedInstanceState) {
  34.         super.onCreate(savedInstanceState);
  35.         setContentView(R.layout.activity_main);
  36.         
  37.         nameView = (EditText)findViewById(R.id.nameView);
  38.         ageView = (EditText) findViewById(R.id.ageView);
  39.         getButton = (Button)findViewById(R.id.getButton);
  40.         postButton = (Button)findViewById(R.id.postButton);
  41.         
  42.         /*POST连接方法
  43.          * 1、创建NameValuePair对象,即创建BasicNameValuePair("name",name);类似键值对
  44.          * 2、创建NameValuepair的链表List: nameValuepairs = new ArrrayLisst();
  45.          *         并把要携带的内容全部添加进去:nameValuePairs.add(nameValuePairName);
  46.          * 3、创建一个带url的HttpPost对象(类似创建一个带url的HttpGet对象)
  47.          * 4、为HttpPost对象设置实体(即要发送的内容):httpPost.setEntity(requestHttpEntity);
  48.          * 以下的内容和Get的方法一样
  49.          * 2、创建一个客户端发送请求HttpClient对象,即DefaultHttpClient()
  50.          * 3、客户端HttpClient执行execute(httpGet)方法,将返回一个服务器的响应httpresponse
  51.          * 4、从服务器返回的响应中得到httpEntity实体:httpEntity = httpResponse.getEntity();
  52.          * 5、从实体中得到内容inputStream = httpEntity.getContent();
  53.          * inputSteamReader ->inputStreamReader->BufferedReader -> Str = BufferedReader.readLine()
  54.          * */
  55.         postButton.setOnClickListener(new OnClickListener(){
  56.             @Override
  57.             public void onClick(View v) {
  58.                 String name = nameView.getText().toString();
  59.                 String age = ageView.getText().toString();                    
  60.                 NameValuePair nameValuePairName = new BasicNameValuePair("name",name);
  61.                 NameValuePair nameValuePairAge = new BasicNameValuePair("age",age);
  62.                 
  63.                 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
  64.                 nameValuePairs.add(nameValuePairName);
  65.                 nameValuePairs.add(nameValuePairAge);
  66.                 
  67.                 try {
  68.                     HttpEntity requestHttpEntity = new UrlEncodedFormEntity(nameValuePairs);
  69.                     String url = "";
  70.                     //HttpPost httpPost = new HttpPost(baseUrl);
  71.                     HttpPost httpPost = new HttpPost(url);
  72.                     //httpPost.setEntity(requestHttpEntity);
  73.                     HttpClient httpClient = new DefaultHttpClient();
  74.                     
  75.                     InputStream inputStream = null;    
  76.                     HttpResponse httpResponse = null;    
  77.                     HttpEntity httpEntity = null;    
  78.                     try {
  79.                         //在客户端HttpClient的execute(httpGet)填入httpGet对象发送请求,
  80.                         //将得到服务器的响应并返回httpResponse
  81.                         httpResponse = httpClient.execute(httpPost);
  82.                         //在服务器返回的响应中获得内容实体HttpEntity
  83.                         httpEntity = httpResponse.getEntity();
  84.                         //在内容实体中得到内容放入输入流
  85.                         inputStream = httpEntity.getContent();
  86.                         BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
  87.                         String result = "";
  88.                         String line = "";
  89.                         while((line = reader.readLine()) != null){                        
  90.                             result = result + line;
  91.                             System.out.println(line);
  92.                         }
  93.                         System.out.println(result);
  94.                         
  95.                     } catch (ClientProtocolException e) {
  96.                         e.printStackTrace();
  97.                     } catch (IOException e) {
  98.                         e.printStackTrace();
  99.                     }    
  100.                     
  101.                 } catch (UnsupportedEncodingException e) {
  102.                     e.printStackTrace();
  103.                 }                
  104.             }});
  105.         
  106.         
  107.         /* Get连接方法
  108.          * 1、创建一个带url的HttpGet对象
  109.          * 2、创建一个客户端发送请求HttpClient对象,即DefaultHttpClient()
  110.          * 3、客户端HttpClient执行execute(httpGet)方法,将返回一个服务器的响应httpresponse
  111.          * 4、从服务器返回的响应中得到httpEntity实体:httpEntity = httpResponse.getEntity();
  112.          * 5、从实体中得到内容inputStream = httpEntity.getContent();
  113.          * inputSteamReader ->inputStreamReader->BufferedReader -> Str = BufferedReader.readLine()
  114.          * */
  115.         getButton.setOnClickListener(new OnClickListener(){
  116.             @Override
  117.             public void onClick(View arg0) {
  118.                 String name = nameView.getText().toString();
  119.                 String age = ageView.getText().toString();                
  120.             //    String url = baseUrl + "?" + "name=" + name + "&age=" + age;
  121.                 System.out.println("11111111111111111");
  122.                 String url = "";
  123.                 //生成一个HttpGet请求对象
  124.                 HttpGet httpGet = new HttpGet(url);                
  125.                 //生成一个HttpClient客户端发送请求对象 即DefaultHttpClient对象
  126.                 HttpClient httpClient = new DefaultHttpClient();                
  127.                 InputStream inputStream = null;    
  128.                 HttpResponse httpResponse = null;    
  129.                 HttpEntity httpEntity = null;    
  130.                 try {
  131.                     //在客户端HttpClient的execute(httpGet)填入httpGet对象发送请求,
  132.                     //将得到服务器的响应并返回httpResponse
  133.                     httpResponse = httpClient.execute(httpGet);
  134.                     //在服务器返回的响应中获得内容实体HttpEntity
  135.                     httpEntity = httpResponse.getEntity();
  136.                     //在内容实体中得到内容放入输入流
  137.                     inputStream = httpEntity.getContent();
  138.                     BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
  139.                     String result = "";
  140.                     String line = "";
  141.                     while((line = reader.readLine()) != null){                        
  142.                         result = result + line;
  143.                         System.out.println(line);
  144.                     }
  145.                     System.out.println(result);
  146.                     
  147.                 } catch (ClientProtocolException e) {
  148.                     e.printStackTrace();
  149.                 } catch (IOException e) {
  150.                     e.printStackTrace();
  151.                 }                
  152.             }            
  153.         });    
  154.     }
  155. }
注意:一定要在AndroidMainfest.xml添加访问互联网的权限:
   android:name="android.permission.INTERNET"/>

阅读(1194) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~