Android用Apache HttpClient 实现POST和Get请求

类 : org.apache.http.client.HttpClient;

1.  GET实现

Java代码   收藏代码
  1. package com.yarin.android.Examples_08_02;  
  2.   
  3. import java.io.IOException;  
  4. import org.apache.http.HttpResponse;  
  5. import org.apache.http.HttpStatus;  
  6. import org.apache.http.client.ClientProtocolException;  
  7. import org.apache.http.client.HttpClient;  
  8. import org.apache.http.client.methods.HttpGet;  
  9. import org.apache.http.impl.client.DefaultHttpClient;  
  10. import org.apache.http.util.EntityUtils;  
  11. import android.app.Activity;  
  12. import android.content.Intent;  
  13. import android.os.Bundle;  
  14. import android.view.View;  
  15. import android.widget.Button;  
  16. import android.widget.TextView;  
  17.   
  18. public class Activity02 extends Activity {  
  19.     /** Called when the activity is first created. */  
  20.     @Override  
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.http);  
  24.         TextView mTextView = (TextView) this.findViewById(R.id.TextView_HTTP);  
  25.         // http地址  
  26.         String httpUrl = "http://192.168.1.110:8080/httpget.jsp?par=HttpClient_android_Get";  
  27.         // HttpGet连接对象  
  28.         HttpGet httpRequest = new HttpGet(httpUrl);  
  29.         try {  
  30.             // 取得HttpClient对象  
  31.             HttpClient httpclient = new DefaultHttpClient();  
  32.             // 请求HttpClient,取得HttpResponse  
  33.             HttpResponse httpResponse = httpclient.execute(httpRequest);  
  34.             // 请求成功  
  35.             if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {  
  36.                 // 取得返回的字符串  
  37.                 String strResult = EntityUtils.toString(httpResponse  
  38.                         .getEntity());  
  39.                 mTextView.setText(strResult);  
  40.             } else {  
  41.                 mTextView.setText("请求错误!");  
  42.             }  
  43.         } catch (ClientProtocolException e) {  
  44.             mTextView.setText(e.getMessage().toString());  
  45.         } catch (IOException e) {  
  46.             mTextView.setText(e.getMessage().toString());  
  47.         } catch (Exception e) {  
  48.             mTextView.setText(e.getMessage().toString());  
  49.         }  
  50.   
  51.         // 设置按键事件监听  
  52.         Button button_Back = (Button) findViewById(R.id.Button_Back);  
  53.         /* 监听button的事件信息 */  
  54.         button_Back.setOnClickListener(new Button.OnClickListener() {  
  55.             public void onClick(View v) {  
  56.                 /* 新建一个Intent对象 */  
  57.                 Intent intent = new Intent();  
  58.                 /* 指定intent要启动的类 */  
  59.                 intent.setClass(Activity02.this, Activity01.class);  
  60.                 /* 启动一个新的Activity */  
  61.                 startActivity(intent);  
  62.                 /* 关闭当前的Activity */  
  63.                 Activity02.this.finish();  
  64.             }  
  65.         });  
  66.     }  
  67. }  

 

 

2.  POST

 

Java代码   收藏代码
  1. package com.yarin.android.Examples_08_02;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6. import org.apache.http.HttpEntity;  
  7. import org.apache.http.HttpResponse;  
  8. import org.apache.http.HttpStatus;  
  9. import org.apache.http.NameValuePair;  
  10. import org.apache.http.client.ClientProtocolException;  
  11. import org.apache.http.client.HttpClient;  
  12. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  13. import org.apache.http.client.methods.HttpPost;  
  14. import org.apache.http.impl.client.DefaultHttpClient;  
  15. import org.apache.http.message.BasicNameValuePair;  
  16. import org.apache.http.util.EntityUtils;  
  17. import android.app.Activity;  
  18. import android.content.Intent;  
  19. import android.os.Bundle;  
  20. import android.view.View;  
  21. import android.widget.Button;  
  22. import android.widget.TextView;  
  23.   
  24. public class Activity03 extends Activity {  
  25.     /** Called when the activity is first created. */  
  26.     @Override  
  27.     public void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.http);  
  30.         TextView mTextView = (TextView) this.findViewById(R.id.TextView_HTTP);  
  31.         // http地址  
  32.         String httpUrl = "http://192.168.1.110:8080/httpget.jsp";  
  33.         // HttpPost连接对象  
  34.         HttpPost httpRequest = new HttpPost(httpUrl);  
  35.         // 使用NameValuePair来保存要传递的Post参数  
  36.         List<NameValuePair> params = new ArrayList<NameValuePair>();  
  37.         // 添加要传递的参数  
  38.         params.add(new BasicNameValuePair("par""HttpClient_android_Post"));  
  39.         try {  
  40.             // 设置字符集  
  41.             HttpEntity httpentity = new UrlEncodedFormEntity(params, "gb2312");  
  42.             // 请求httpRequest  
  43.             httpRequest.setEntity(httpentity);  
  44.             // 取得默认的HttpClient  
  45.             HttpClient httpclient = new DefaultHttpClient();  
  46.             // 取得HttpResponse  
  47.             HttpResponse httpResponse = httpclient.execute(httpRequest);  
  48.             // HttpStatus.SC_OK表示连接成功  
  49.             if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {  
  50.                 // 取得返回的字符串  
  51.                 String strResult = EntityUtils.toString(httpResponse.getEntity());  
  52.                 mTextView.setText(strResult);  
  53.             } else {  
  54.                 mTextView.setText("请求错误!");  
  55.             }  
  56.         } catch (ClientProtocolException e) {  
  57.             mTextView.setText(e.getMessage().toString());  
  58.         } catch (IOException e) {  
  59.             mTextView.setText(e.getMessage().toString());  
  60.         } catch (Exception e) {  
  61.             mTextView.setText(e.getMessage().toString());  
  62.         }  
  63.         // 设置按键事件监听  
  64.         Button button_Back = (Button) findViewById(R.id.Button_Back);  
  65.         /* 监听button的事件信息 */  
  66.         button_Back.setOnClickListener(new Button.OnClickListener() {  
  67.             public void onClick(View v) {  
  68.                 /* 新建一个Intent对象 */  
  69.                 Intent intent = new Intent();  
  70.                 /* 指定intent要启动的类 */  
  71.                 intent.setClass(Activity03.this, Activity01.class);  
  72.                 /* 启动一个新的Activity */  
  73.                 startActivity(intent);  
  74.                 /* 关闭当前的Activity */  
  75.                 Activity03.this.finish();  
  76.             }  
  77.         });  
  78.     }  
  79. }  

 

Apache org.apache.http.client.HttpClient;

猜你喜欢

转载自blog.csdn.net/gvvbn/article/details/39432873
今日推荐