`

Android用Apache HttpClient 实现POST和Get请求

阅读更多

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

1.  GET实现

package com.yarin.android.Examples_08_02;

import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Activity02 extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.http);
		TextView mTextView = (TextView) this.findViewById(R.id.TextView_HTTP);
		// http地址
		String httpUrl = "http://192.168.1.110:8080/httpget.jsp?par=HttpClient_android_Get";
		// HttpGet连接对象
		HttpGet httpRequest = new HttpGet(httpUrl);
		try {
			// 取得HttpClient对象
			HttpClient httpclient = new DefaultHttpClient();
			// 请求HttpClient,取得HttpResponse
			HttpResponse httpResponse = httpclient.execute(httpRequest);
			// 请求成功
			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				// 取得返回的字符串
				String strResult = EntityUtils.toString(httpResponse
						.getEntity());
				mTextView.setText(strResult);
			} else {
				mTextView.setText("请求错误!");
			}
		} catch (ClientProtocolException e) {
			mTextView.setText(e.getMessage().toString());
		} catch (IOException e) {
			mTextView.setText(e.getMessage().toString());
		} catch (Exception e) {
			mTextView.setText(e.getMessage().toString());
		}

		// 设置按键事件监听
		Button button_Back = (Button) findViewById(R.id.Button_Back);
		/* 监听button的事件信息 */
		button_Back.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				/* 新建一个Intent对象 */
				Intent intent = new Intent();
				/* 指定intent要启动的类 */
				intent.setClass(Activity02.this, Activity01.class);
				/* 启动一个新的Activity */
				startActivity(intent);
				/* 关闭当前的Activity */
				Activity02.this.finish();
			}
		});
	}
}

 

 

2.  POST

 

package com.yarin.android.Examples_08_02;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Activity03 extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.http);
		TextView mTextView = (TextView) this.findViewById(R.id.TextView_HTTP);
		// http地址
		String httpUrl = "http://192.168.1.110:8080/httpget.jsp";
		// HttpPost连接对象
		HttpPost httpRequest = new HttpPost(httpUrl);
		// 使用NameValuePair来保存要传递的Post参数
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		// 添加要传递的参数
		params.add(new BasicNameValuePair("par", "HttpClient_android_Post"));
		try {
			// 设置字符集
			HttpEntity httpentity = new UrlEncodedFormEntity(params, "gb2312");
			// 请求httpRequest
			httpRequest.setEntity(httpentity);
			// 取得默认的HttpClient
			HttpClient httpclient = new DefaultHttpClient();
			// 取得HttpResponse
			HttpResponse httpResponse = httpclient.execute(httpRequest);
			// HttpStatus.SC_OK表示连接成功
			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				// 取得返回的字符串
				String strResult = EntityUtils.toString(httpResponse.getEntity());
				mTextView.setText(strResult);
			} else {
				mTextView.setText("请求错误!");
			}
		} catch (ClientProtocolException e) {
			mTextView.setText(e.getMessage().toString());
		} catch (IOException e) {
			mTextView.setText(e.getMessage().toString());
		} catch (Exception e) {
			mTextView.setText(e.getMessage().toString());
		}
		// 设置按键事件监听
		Button button_Back = (Button) findViewById(R.id.Button_Back);
		/* 监听button的事件信息 */
		button_Back.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				/* 新建一个Intent对象 */
				Intent intent = new Intent();
				/* 指定intent要启动的类 */
				intent.setClass(Activity03.this, Activity01.class);
				/* 启动一个新的Activity */
				startActivity(intent);
				/* 关闭当前的Activity */
				Activity03.this.finish();
			}
		});
	}
}

 

Apache org.apache.http.client.HttpClient;

分享到:
评论
1 楼 greatwqs 2011-05-05  
ApacheGet :  HttpGet httpRequest = new HttpGet(httpUrl);   
ApachePost:  HttpPost httpRequest = new HttpPost(httpUrl);   


// 取得HttpClient对象   
HttpClient httpclient = new DefaultHttpClient();   
// 请求HttpClient,取得HttpResponse   
HttpResponse httpResponse = httpclient.execute(httpRequest);   



// HttpStatus.SC_OK表示连接成功   
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {   
// 取得返回的字符串   
String strResult = EntityUtils.toString(httpResponse.getEntity());   
mTextView.setText(strResult);   
} else {   
mTextView.setText("请求错误!");   
 } 

相关推荐

    httpclient-android.jar

    为了解决android org.apache.http.legacy 包已有的http和httpclient4.3以上版本冲突。使用时将 org.apache.http包下的资源替换成 org.apache.http4即可。如: import org.apache.http4.client.HttpClient; import ...

    org.apache.http的jar包是使用HttpClient发送http请求需要的jar包

    httpmime-4.5.2.jar httpcore-4.4.4.jar httpclient-win-4.5.2.jar httpclient-cache-4.5.2.jar httpclient-4.5.2.jar 提供以上5个jar包

    Android HttpClient GET或者POST请求基本使用方法

    在Android开发中我们经常会用到网络连接功能与服务器进行数据的交互,为此Android的SDK提供了Apache的HttpClient来方便我们使用各种Http服务.这里只介绍如何使用HttpClient发起GET或者POST请求

    安卓网站交互JSONxmlWebserviceUPnP相关-android-async-http是Android上的一个异步基于回调的HTTP客户端开发包建立在Apache的HttpClient库上.zip

    android-async-http是Android上的一个异步、基于回调的HTTP客户端开发包,建立在Apache的HttpClient库上.zip,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。

    Android HttpClient用到的jar包

    Httpclient上传文件,非常有用的jar包 apache-mime4j-0.6.jar httpmime-4.0.jar

    httpClient

    httpClient的get请求方式2 * @return * @throws Exception */ public static String doGet(String url, String charset) throws Exception { /* * 使用 GetMethod 来访问一个 URL 对应的网页,实现步骤: 1:...

    用HttpClient来模拟浏览器GET POST

    当然了,正如前面说到的,如果我们自己使用java.net.HttpURLConnection来搞定这些问题是很恐怖的事情,因此在开始之前我们先要介绍一下一个开放源码的项目,这个项目就是Apache开源组织中的httpclient,它隶属于...

    Android代码-一款基于Android 6.0 网络请求框架

    1 不再使用HttpClient相关API,因为Android 6.0删除Apache HttpClient相关API,虽然我们可以自己引用,但是既然谷歌删除,还是跟着老大走吧, 所以本项目没有任何HttpClient相关API. 2 一行代码发送请求,提供多种回调...

    Spring 实现远程访问详解——httpclient

    上两章我们分别利用Spring rmi和httpinvoker实现的远程访问功能,具体见《》和《》。本章将通过apache httpclient实现远程访问。说得简单就是直接通过spring requestmapping即请求映射url访问远程服务。 1. 远程访问...

    httpclient4.5.3 jar完整包含所有依赖包

    HttpClient 4.5.3 (GA) is a maintenance release that fixes a number of defects found since 4.5.2. Please note that as of 4.4 HttpClient requires Java 1.6 or newer. Changelog: ------------------- * ...

    httpclient-3.1.zip

    java apache commons HttpClient发送get和post请求. Commons-httpclient项目就是专门设计来简化HTTP客户端与服务器进行各种通讯编程.

    org.apache.http.httpentity jar包-系列jar包

    import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.ssl.SSLSocketFactory; import ...

    org.apache.http jar包

    下载HttpClient,解压,在Eclipse中导入所有JAR import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache...

    HttpClient jar包下载

    使用 HttpClient,调用http接口, 使用HttpClient JAVA发送http get/post请求,

    java学习资料,org.apache.http jar包工具

    通过Apache的httpClient的get, post 请求下载,或者上传文件

    http:简化的、轻量级的 HTTP 客户端库,是 apache httpclient 的包装器

    发出 GET、POST、PUT、PATCH、DELETE、HEAD、OPTIONS 请求 支持所有常用的 MIME 类型,如 JSON、XML、ATOM 和 YAML 启用 Web 服务的 HTTP 和 HTTPS (SSL) 调用 它支持表单参数、文件上传和自定义正文实体 无需丑陋...

    Android网络请求之OkHttp

    OkHttp是一款优秀的HTTP框架,它支持get请求和post请求,支持基于Http的文件上传和下载,支持加载图片,支持下载文件透明的GZIP压缩,支持响应缓存避免重复的网络请求,支持使用连接池来降低响应延迟问题 Get请求 ...

    HttpClient以及获取页面内容应用

    HttpClient已经应用在很多的项目中,比如Apache Jakarta上很著名的另外两个开源项目Cactus和HTMLUnit都使用了HttpClient。 下载地址:  http://hc.apache.org/downloads.cgi 1.2特性 1. 基于标准、纯净的java语言。...

    android平台HttpGet、HttpPost请求实例

    出自网络搜索引擎巨头的Android平台,其对网络的支持自然不用多说,在Android SDK中已经集成了Apache的HttpClient模块。使用HttpClient模块,我们就可以使用HTTP协议进行网络连接了

    HttpClient相关包

    import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org....

Global site tag (gtag.js) - Google Analytics