- 浏览: 124825 次
- 性别:
- 来自: 深圳
最新评论
-
T_bag:
...
TabHost 中的Activity执行顺序 -
ihopethatwell:
楼主,你能否写一个 int类型的一维数组的结构体?
linux NDK实例 -
gf_crazy:
刚好找第二种,其他地方全是第一种。
TabHost -
gangbener:
我们是可以把不同分辨率的图片放到不同的图片文件夹中去,问题是: ...
android程序中屏幕问题解决方案 -
shusanzhan:
学习了,Mark
android应用收费渠道
使用 HTTP 服务:
1.
Apache HttpClinet
Http GET
Http POST
a.创建 HttpClient
b.初始 HTTP GET 方法或 POST 方法.
c.设置参数 键值对
d.执行 HTTP 调用
e.处理 HTTP 回复
HTTP GET 示例:
Java代码
- public class TestHttpGetMethod{
- public void get(){
- BufferedReader in = null;
- try{
- HttpClient client = new DefaultHttpClient();
- HttpGet request = new HttpGet();
- request.setURI("http://w26.iteye.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();
- Log.i(TAG, page);
- }catch(Exception e){
- Log.e(TAG,e.toString())
- }finally{
- if(in != null){
- try{
- in.close();
- }catch(IOException ioe){
- Log.e(TAG, ioe.toString());
- }
- }
- }
- }
- }
public class TestHttpGetMethod{ public void get(){ BufferedReader in = null; try{ HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(); request.setURI("http://w26.iteye.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(); Log.i(TAG, page); }catch(Exception e){ Log.e(TAG,e.toString()) }finally{ if(in != null){ try{ in.close(); }catch(IOException ioe){ Log.e(TAG, ioe.toString()); } } } } }
带参数的 HTTP GET:
Java代码
- HttpGet request = new HttpGet("http://www.baidu.com/s?wd=amos_tl");
- client.execute(request);
HttpGet request = new HttpGet("http://www.baidu.com/s?wd=amos_tl"); client.execute(request);
HTTP POST 示例:
Java代码
- public class TestHttpPostMethod{
- public void post(){
- BufferedReader in = null;
- try{
- HttpClient client = new DefaultHttpClient();
- HttpPost request = new HttpPost("http://localhost/upload.jsp");
- List<NameValuePair> postParams = new ArrayList<NameValuePair>();
- postParams.add(new BasicNameValuePair("filename", "sex.mov"));
- UrlEncodeFormEntity formEntity = new UrlEncodeFormEntity(postParams);
- 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();
- Log.i(TAG, result );
- }catch(Exception e){
- Log.e(TAG,e.toString())
- }finally{
- if(in != null){
- try{
- in.close();
- }catch(IOException ioe){
- Log.e(TAG, ioe.toString());
- }
- }
- }
- }
- }
public class TestHttpPostMethod{ public void post(){ BufferedReader in = null; try{ HttpClient client = new DefaultHttpClient(); HttpPost request = new HttpPost("http://localhost/upload.jsp"); List<NameValuePair> postParams = new ArrayList<NameValuePair>(); postParams.add(new BasicNameValuePair("filename", "sex.mov")); UrlEncodeFormEntity formEntity = new UrlEncodeFormEntity(postParams); 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(); Log.i(TAG, result ); }catch(Exception e){ Log.e(TAG,e.toString()) }finally{ if(in != null){ try{ in.close(); }catch(IOException ioe){ Log.e(TAG, ioe.toString()); } } } } }
multipart POST 支持:
需要以下支持:
Commons IO
http://commons.apache.org/io/
Mime4j
http://james.apache.org/mime4j/
HttpMime
http://hc.apache.org/httpcomponents-client/httpmime/index.html
下载全部JAR网址:
http://www.sayedhashimi.com/downloads/android/multipart-android.zip
multipart POST 示例:
Java代码
- public class TestHttpMultipartPost{
- public void mulPost(){
- try{
- InputStram in = this.getAssets().open("data.xml");
- HttpClient client = new HttpDefaultHttpClient();
- HttpPost request = new HttpPost("http://localhost/upload.jsp");
- byte[] data = IOUtils.toByteArray(in);
- InputStreamBody isb = new InputStreamBody(new ByteArrayIntputStream(data), "uploadedFile");
- StringBody sb1 = new StringBody("some text");
- StringBoyd sb2 = new StringBody("some text too");
- MultipartEntity me = new MultipartEntity();
- me.addPart("uploadedFile", isb);
- me.addPart("one" ,sb1);
- me.addPart("two" ,sb2);
- request.setEntity(me);
- HttpRespones response = client.excute(request);
- res.getEntity().getContent().close();
- } catch(Throwable e){
- Log.e(TAG, e.toString());
- }
- }
- }
public class TestHttpMultipartPost{ public void mulPost(){ try{ InputStram in = this.getAssets().open("data.xml"); HttpClient client = new HttpDefaultHttpClient(); HttpPost request = new HttpPost("http://localhost/upload.jsp"); byte[] data = IOUtils.toByteArray(in); InputStreamBody isb = new InputStreamBody(new ByteArrayIntputStream(data), "uploadedFile"); StringBody sb1 = new StringBody("some text"); StringBoyd sb2 = new StringBody("some text too"); MultipartEntity me = new MultipartEntity(); me.addPart("uploadedFile", isb); me.addPart("one" ,sb1); me.addPart("two" ,sb2); request.setEntity(me); HttpRespones response = client.excute(request); res.getEntity().getContent().close(); } catch(Throwable e){ Log.e(TAG, e.toString()); } } }
异常处理
重试处理
多线程问题
使用 ClientConnectionManager ,创建一个线程安全的 HttpClient.
Java代码
- public class ApplicationEx extends Application{
- public static final String TAG = "amos_tl";
- private HttpClient client = null;
- @override
- public void onCreate(){
- super.onCreate();
- client = createHttpClient();
- }
- @override
- public void onLowMemory(){
- super.onLowMemory();
- shutdownHttpClient();
- }
- @override
- public void onTerminate(){
- super.onTerminate();
- shutdownHttpClient();
- }
- private void shutdownHttpClient(){
- if(client != null && client.getConnectionManager() != null){
- client.getConnectionManager().shutdown();
- client = null;
- }
- }
- private HttpClient createHttpClient(){
- Log.d(TAG, "create httpclient ...");
- HttpParams params = new BasicHttpParams();
- HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
- HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
- HttpProtocolParams.setUseExpectContinue(params, true);
- SchemaRegistry sr = new SchemaRegistry();
- sr.register(new Schema("http", PlainSocketFactory.getSocketFactory(), 80));
- sr.register(new Schema("https", SLLSocketFactory.getSocketFactory(), 443));
- ClientConnectionManager cm = new ThreadSafeClientConnManager(params, sr);
- return new DefaultHttpClient(cm, params);
- }
- public HttpClient getHttpClient(){
- return client;
- }
- }
public class ApplicationEx extends Application{ public static final String TAG = "amos_tl"; private HttpClient client = null; @override public void onCreate(){ super.onCreate(); client = createHttpClient(); } @override public void onLowMemory(){ super.onLowMemory(); shutdownHttpClient(); } @override public void onTerminate(){ super.onTerminate(); shutdownHttpClient(); } private void shutdownHttpClient(){ if(client != null && client.getConnectionManager() != null){ client.getConnectionManager().shutdown(); client = null; } } private HttpClient createHttpClient(){ Log.d(TAG, "create httpclient ..."); HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET); HttpProtocolParams.setUseExpectContinue(params, true); SchemaRegistry sr = new SchemaRegistry(); sr.register(new Schema("http", PlainSocketFactory.getSocketFactory(), 80)); sr.register(new Schema("https", SLLSocketFactory.getSocketFactory(), 443)); ClientConnectionManager cm = new ThreadSafeClientConnManager(params, sr); return new DefaultHttpClient(cm, params); } public HttpClient getHttpClient(){ return client; } }
HttpActivity.java
Java代码
- public class HttpActivity extends Activity{
- @override
- public void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- Log.d(TAG, "httpactivity startup...");
- getHttpContent();
- }
- private void getHttpContent(){
- try{
- ApplicationEx app = (ApplicationEx)this.getApplication();
- HttpClient client = app.getHttpClient();
- HttpGet request = new HttpGet();
- request.setURI("http://w26.iteye.com");
- HttpResponse response = client.excute(resquest);
- String page = EntityUtils.toString(response.getEntity());
- Log.i(TAG, page);
- }catch(Exception e){
- Log.e(TAG, e.toString());
- }
- }
- }
发表评论
-
android.os.NetworkOnMainThreadException
2011-12-24 13:14 1234不能在android的主线程中,执行一个网络操作 ... -
转载:为什么要对URI进行编码
2011-12-15 15:49 1078为什么需要Url编码,通常如果一样东西需要编码,说明这样东 ... -
multipart form-data boundary
2011-12-15 15:23 1022含义 ENCTYPE="multipart/ ... -
android http 附件
2011-12-15 14:17 1663一:服务器端: 1:struts-config.xml ... -
post 附件
2011-12-15 10:24 1006在做嘀咕客户端的时候,要实现拍照上传的功能。根据嘀咕api ... -
让Android应用获取系统权限
2011-12-08 18:46 1004在 android 的API中有提供 SystemCloc ... -
Android源码目录结构详解
2011-12-01 20:22 850Android 2.1 |-- Makefile |-- ... -
两个activity跳转
2011-11-25 16:06 1278Activity A跳转到 Activity B /**A. ... -
线程同步之wait()/notify()的使用
2011-11-21 11:24 1001wait()/notify() 通常,多 ... -
游戏中渲染线程与更新线程交替执行
2011-11-21 11:21 945private final State mThreadLock ... -
android colormatrix
2011-11-03 17:32 1497在编程中有时候需要 ... -
java栈,堆,池
2011-07-08 09:38 751今天复习了一下这些知识,顺便做了下笔记.1.寄存器:最快的存储 ... -
3D开发的境界
2011-06-04 20:12 704第一阶段:初学者阶 ... -
获取手机的Opengl的支持版本
2011-05-27 09:28 1464public int getGLVersion() { ... -
性能优化
2011-05-27 09:26 791如果你想写一个 Java 程序,观察某对象什么时候会被垃圾收集 ... -
Android游戏中其他语言数据类型之间的转换方法
2011-05-17 11:43 1236Java与其他语言数据类型之间的转换方法实例程序 /* ... -
android canvas.getClipBounds
2011-05-13 17:41 8439一种是传参数: Rect dstRect = new Re ... -
获取屏幕大小的方法
2011-05-13 17:38 603// one DisplayMetrics dm = n ... -
Android Lock 使用
2011-05-13 16:43 3210PowerManager 和PowerManager.Wa ... -
为Android编写实时游戏
2011-05-06 15:57 1312为Android编写实时游戏 —-学习笔记 这篇文章是 ...
相关推荐
在Java编程中,HTTP POST和GET是两种基本的HTTP请求方法,用于客户端向服务器发送数据。为了在Java中实现这些功能,我们需要引入特定的库,这些库通常被打包成JAR(Java Archive)文件。本篇文章将详细讲解HTTP POST...
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.impl.client.DefaultHttpClient; ...
《深入理解HTTPCore与HTTPMIME:构建高效网络通信》 HTTPCore与HTTPMIME是Apache HttpClient库中的核心组件,这两个jar包在Java开发者中有着广泛的应用,尤其在处理HTTP请求和响应时不可或缺。本篇将详细介绍这两个...
在这个特定的场景中,我们关注的是如何利用VC通过HttpGet和HttpPost方法与WebService进行交互,并处理返回的Json数据。 HttpGet和HttpPost是HTTP协议中的两种主要请求方法。HttpGet是一种无状态、幂等的请求方法,...
1. **HTTP协议支持**:httpcore提供了对HTTP/1.0和HTTP/1.1协议的支持,包括GET、POST等各种HTTP方法,以及头部处理、状态码解析等。 2. **连接管理**:它包含连接池(Connection Pooling)功能,可以复用HTTP连接...
import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.HttpVersion; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; ...
org.apache.http jar包 import org.apache.http.Header; import org.apache.http.HttpException; import org.apache.http.HttpRequest; import org.apache.http.HttpRequestInterceptor; import org....
**HTTP协议详解** HTTP(Hypertext Transfer Protocol)超文本传输协议是互联网上应用最广泛的一种网络协议。它是用于从万维网服务器传输超文本到本地浏览器的传输协议,是Web应用的基础。HTTP协议定义了客户端...
在IT行业中,网络通信是软件开发的一个重要领域,而HTTP(超文本传输协议)作为互联网上应用最广泛的一种网络协议,被广泛应用于网页浏览、数据传输等场景。本篇文章将详细探讨如何使用C++来实现HTTP的POST和GET请求...
构建一个简单的HTTP服务器的C#程序实例。实现响应GET、POST请求。在服务端创建一个tcp通信来负责监听客户端连接。每次客户端发出请求后,我们根据请问报文来判断客户端的请求类型,然后根据不同的请求类型进行相应的...
标题 "VB做的HTTP简单服务器源码" 描述的是一个使用Visual Basic(VB)编程语言编写的简易HTTP服务器的源代码。这个服务器能够响应HTTP请求,为客户端提供服务,可能是为了教学目的或者作为小型项目的起点。VB是一种...
在IT行业中,HTTP代理是一种网络通信机制,它允许客户端(如Web浏览器)通过另一台服务器作为中介来访问互联网上的资源。这种技术常用于提高匿名性、优化网络性能或绕过特定的网络限制。本篇文章将深入探讨如何使用...
在IT行业中,网络通信是至关重要的一个领域,而HTTP(超文本传输协议)作为网络通信的基础,被广泛应用于Web应用程序的开发。在这个场景下,我们关注的是使用VC++(Microsoft Visual C++)来实现HTTP客户端和服务器...
### 图解HTTP:全面解析HTTP协议 #### 一、引言 HTTP协议作为互联网的核心组成部分,对于每一个从事Web开发或维护的技术人员来说都是必须掌握的基础知识。《图解HTTP》一书通过丰富的图表和深入浅出的文字解释,...
在IT领域,网络编程是不可或缺的一部分,特别是在操作系统如Linux中,开发者经常需要通过HTTP协议进行数据交换。本篇将深入探讨使用C++在Linux环境下实现HTTP请求,包括GET和POST方法。 HTTP(超文本传输协议)是...
import org.apache.http.Header; import org.apache.http.HttpException; import org.apache.http.HttpRequest; import org.apache.http.HttpRequestInterceptor; import org.apache.http.HttpResponse; import org....
在本示例中,我们将关注的是"C# HTTP接口设计及调用demo",这通常涉及到如何创建一个HTTP服务端接口,以及如何使用C#客户端进行调用。HTTP接口在分布式系统中扮演着重要角色,它允许不同组件之间通过HTTP协议交换...
在本项目中,"网络编程HttpServer c++实现" 是一个使用C++语言编写的HTTP服务器,它允许用户创建和管理基于HTTP协议的服务。HTTP服务器是互联网上的关键组件,它们接收HTTP请求并返回HTTP响应,使得网页和其他资源...
在VC++编程环境中,HTTP(超文本传输协议)Get和Post请求是常见的网络通信方法,用于从或向服务器发送数据。这两个方法是Web应用程序与服务器交互的基础,理解它们的工作原理和如何在VC++中实现至关重要。 **HTTP ...