- 浏览: 122026 次
- 性别:
- 来自: 深圳
最新评论
-
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 1188不能在android的主线程中,执行一个网络操作 ... -
转载:为什么要对URI进行编码
2011-12-15 15:49 1007为什么需要Url编码,通常如果一样东西需要编码,说明这样东 ... -
multipart form-data boundary
2011-12-15 15:23 960含义 ENCTYPE="multipart/ ... -
android http 附件
2011-12-15 14:17 1597一:服务器端: 1:struts-config.xml ... -
post 附件
2011-12-15 10:24 958在做嘀咕客户端的时候,要实现拍照上传的功能。根据嘀咕api ... -
让Android应用获取系统权限
2011-12-08 18:46 967在 android 的API中有提供 SystemCloc ... -
Android源码目录结构详解
2011-12-01 20:22 784Android 2.1 |-- Makefile |-- ... -
两个activity跳转
2011-11-25 16:06 1223Activity A跳转到 Activity B /**A. ... -
线程同步之wait()/notify()的使用
2011-11-21 11:24 935wait()/notify() 通常,多 ... -
游戏中渲染线程与更新线程交替执行
2011-11-21 11:21 892private final State mThreadLock ... -
android colormatrix
2011-11-03 17:32 1460在编程中有时候需要 ... -
java栈,堆,池
2011-07-08 09:38 705今天复习了一下这些知识,顺便做了下笔记.1.寄存器:最快的存储 ... -
3D开发的境界
2011-06-04 20:12 684第一阶段:初学者阶 ... -
获取手机的Opengl的支持版本
2011-05-27 09:28 1440public int getGLVersion() { ... -
性能优化
2011-05-27 09:26 760如果你想写一个 Java 程序,观察某对象什么时候会被垃圾收集 ... -
Android游戏中其他语言数据类型之间的转换方法
2011-05-17 11:43 1184Java与其他语言数据类型之间的转换方法实例程序 /* ... -
android canvas.getClipBounds
2011-05-13 17:41 8398一种是传参数: Rect dstRect = new Re ... -
获取屏幕大小的方法
2011-05-13 17:38 571// one DisplayMetrics dm = n ... -
Android Lock 使用
2011-05-13 16:43 3146PowerManager 和PowerManager.Wa ... -
为Android编写实时游戏
2011-05-06 15:57 1276为Android编写实时游戏 —-学习笔记 这篇文章是 ...
相关推荐
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; ...
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....
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....
httppost和httpget需要的jar包
2. 整体打包,已配置好nginx.conf的http-flv直播流,以及http web环境。无需任何配置即可使用 3. 自带windows的服务注册程序,使用如下方式,将nginx注册为windows的服务,实现无人值守维护管理 windows服务注册: 1...
3、打开httppost.exe 即可运行本软件。 Jadder Http 测试工具 E-Mail: jadderbao@163.com 软件功能: ver 0.3 1、添加检测POST/GET返回内容格式,如为json格式就自动格式化显示 2、添加打开,保存文件时,自动...
超文本传输协议(HTTP)的设计目的是保证客户机与服务器之间的通信。 HTTP 的工作方式是客户机与服务器之间的请求-应答协议。 web 浏览器可能是客户端,而计算机上的网络应用程序也可能作为服务器端。 举例:...
已编译好的nginx-http-flv-molule(windows版) 基于: --> openssl-1.1.1g --> pcre-8.44 --> zlib-1.2.11 --> nginx-http-module-v1.2.10 --> nginx-1.21.6 ======================== 在网上查找半天都只有教程,...
C++ 实现 HTTP HTTPS POST GET(包含curl版本和winhttp两种实现)。 玩过抓包,网络协议分析的朋友肯定都知道http https post get,web端和用户的交互主要是通过post get完成的。 我这里有两种实现: 1:libcurl实现的...
本资源是一个封装类,采用异步方式解决了Wininet不能设置超时的问题,当前异步采用C++...MFC程序中通过HttpGet和HttpPost方式向WebService发送请求,WebService以Json的方式返回数据,MFC程序解析Json,得到指定数据。
能够处理http协议:(1)解析 (2)构造http格式
MFC程序中通过HttpGet和HttpPost方式向WebService发送请求,WebService以Json的方式返回数据,MFC程序解析Json,得到指定数据。
C语言实现的HTTP请求,重写Http协议,并发送http报送的源文件
目前在WWW中使用的是HTTP/1.0的第六版,HTTP/1.1的规范化工作正在进行之中,而且HTTP-NG(Next Generation of HTTP)的建议已经提出。 HTTP协议的主要特点可概括如下: 1.支持客户/服务器模式。 2.简单快速:客户向...
里面是httpcore-4.3.2.jar和httpmime-4.3.5.jar两个jar包,其它csdn资源居然还要积分,我无语了,现在把我下载到的免费提供给大家。
android 当中涉及到网络编程的部分经常会用到http通信,同时android也为我么您提供了HttpUrlConnection接口和HttpClient接口,大大的方便了开发。Http通信又分为两种方式:get和post,get可以uoqu静态页面,传入参数...
HTTP协议详解 RFC2616(HTTP)中文版 pdf 格式,高清
HTTP服务器+测试客户端(含源码) 介绍 * 里面已经搭建好简单http服务器框架,方便添加自定义的http协议数据处理。 * 轻量级的http服务器,简单实用(IIS是航空母舰,WCF过于复杂而且技术耦合大...)。 * Socket网络...