类 : 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;
分享到:
相关推荐
为了解决android org.apache.http.legacy 包已有的http和httpclient4.3以上版本冲突。使用时将 org.apache.http包下的资源替换成 org.apache.http4即可。如: import org.apache.http4.client.HttpClient; import ...
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开发中我们经常会用到网络连接功能与服务器进行数据的交互,为此Android的SDK提供了Apache的HttpClient来方便我们使用各种Http服务.这里只介绍如何使用HttpClient发起GET或者POST请求
android-async-http是Android上的一个异步、基于回调的HTTP客户端开发包,建立在Apache的HttpClient库上.zip,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
Httpclient上传文件,非常有用的jar包 apache-mime4j-0.6.jar httpmime-4.0.jar
httpClient的get请求方式2 * @return * @throws Exception */ public static String doGet(String url, String charset) throws Exception { /* * 使用 GetMethod 来访问一个 URL 对应的网页,实现步骤: 1:...
当然了,正如前面说到的,如果我们自己使用java.net.HttpURLConnection来搞定这些问题是很恐怖的事情,因此在开始之前我们先要介绍一下一个开放源码的项目,这个项目就是Apache开源组织中的httpclient,它隶属于...
1 不再使用HttpClient相关API,因为Android 6.0删除Apache HttpClient相关API,虽然我们可以自己引用,但是既然谷歌删除,还是跟着老大走吧, 所以本项目没有任何HttpClient相关API. 2 一行代码发送请求,提供多种回调...
上两章我们分别利用Spring rmi和httpinvoker实现的远程访问功能,具体见《》和《》。本章将通过apache httpclient实现远程访问。说得简单就是直接通过spring requestmapping即请求映射url访问远程服务。 1. 远程访问...
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: ------------------- * ...
java apache commons HttpClient发送get和post请求. Commons-httpclient项目就是专门设计来简化HTTP客户端与服务器进行各种通讯编程.
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 ...
下载HttpClient,解压,在Eclipse中导入所有JAR import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache...
使用 HttpClient,调用http接口, 使用HttpClient JAVA发送http get/post请求,
通过Apache的httpClient的get, post 请求下载,或者上传文件
发出 GET、POST、PUT、PATCH、DELETE、HEAD、OPTIONS 请求 支持所有常用的 MIME 类型,如 JSON、XML、ATOM 和 YAML 启用 Web 服务的 HTTP 和 HTTPS (SSL) 调用 它支持表单参数、文件上传和自定义正文实体 无需丑陋...
OkHttp是一款优秀的HTTP框架,它支持get请求和post请求,支持基于Http的文件上传和下载,支持加载图片,支持下载文件透明的GZIP压缩,支持响应缓存避免重复的网络请求,支持使用连接池来降低响应延迟问题 Get请求 ...
HttpClient已经应用在很多的项目中,比如Apache Jakarta上很著名的另外两个开源项目Cactus和HTMLUnit都使用了HttpClient。 下载地址: http://hc.apache.org/downloads.cgi 1.2特性 1. 基于标准、纯净的java语言。...
出自网络搜索引擎巨头的Android平台,其对网络的支持自然不用多说,在Android SDK中已经集成了Apache的HttpClient模块。使用HttpClient模块,我们就可以使用HTTP协议进行网络连接了
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....