android-async-http详解
android-async-http开源项目可以是我们轻松的获取网络数据或者向服务器发送数据,使用起来非常简单,关于android-async-http开源项目的介绍内容来自于官方:http://loopj.com/android-async-http/.下面我对此主页上内容进行大体上的翻译,希望能够对你理解android-async-http开源项目有所帮助
1.1 Overview(概况)
An asynchronous callback-based Http client for Android built on top of Apache’s HttpClient libraries.
All requests are made outside of your app’s main UI thread, but any callback logic will be executed on the same thread as the callback was created using Android’s Handler message passing.
译文:
异步基于回调的Http客户端为Android构建,是基于Apache HttpClient库的。所有的请求都是位于应用程序主线程 UI 之外,但任何回调逻辑将相同的线程上执行回调,使用Android的处理程序创建消息传递。
1.2 Features(特征)
- Make asynchronous HTTP requests, handle responses in anonymous
callbacks - 进行异步HTTP请求,处理响应在匿名回调中完成
- HTTP requests happen outside the UI thread
- HTTP请求发生在UI线程之外
- Requests use a threadpool to cap concurrent resource usage
- 请求使用threadpool,限制并发资源使用情况
- GET/POST params builder (RequestParams)
- GET / POST参数构建使用(RequestParams)
- Multipart file uploads with no additional third party libraries
- Multipart 文件上传,没有额外的第三方库
- Tiny size overhead to your application, only 25kb for everything
- 在你的应用程序上利用很小的开销,仅仅25 kb就可以做一切
- Automatic smart request retries optimized for spotty mobile connections
- 自动智能请求重试,优化了质量不一的移动连接
- Automatic gzip response decoding support for super-fast requests
- 自动解码支持gzip反应速度超快的请求
- Binary file (images etc) downloading with
BinaryHttpResponseHandler 二进制文件(图片等)的下载,使用BinaryHttpResponseHandler
- Built-in response parsing into JSON with
JsonHttpResponseHandler 内置响应解析成JSON,使用JsonHttpResponseHandler
- Persistent cookie store, saves cookies into your app’s SharedPreferences
- 持久化cookie存储,保存cookie到你的应用程序的SharedPreferences
2.Installation
& Basic Usage(安装和基本用法)
Download the latest .jar file from github and place it in your Android app’s libs/ folder.
从github上下载最新的最新的jar文件.并将其放置在你的Android应用程序的libs /文件夹.
2.1下载方式:
1.从http://loopj.com/android-async-http/的页面下载
点击DownLoad即可下载最新的jar文件
2.从https://github.com/loopj/android-async-http的页面下载
找到DownLoad ZIP进行下载文件,解压后的目录如下
examples:里面有简单的例子
library:里面存放的是android-async-http开源项目的源码(方法一:可以把library\src\main\java文件下面的文件拷贝到,你应用的src下也可以直接使用)
releases:里面存放的是各个版本的jar文件,(方法二:只需把最新的jar文件拷贝到你应用的libs目录下即可.)
samples:里面存放的也是例子(可供参考)
备注:方法一和方法二只能采用其中之一,建议采用方法二
2.2使用方法
Import
the http package.
import com.loopj.android.http.*;
Create a new AsyncHttpClient instance and make a request:
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://www.google.com", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
System.out.println(response);
}
});
Adding GET/POST Parameters with RequestParams
The RequestParams class is used to add optional GET or POST parameters to your requests.RequestParams can
be built and constructed in various ways:
Create empty RequestParams and immediately add some parameters:
RequestParams params = new RequestParams();
params.put("key", "value");
params.put("more", "data");
Create RequestParams for a single parameter:
RequestParams params = new RequestParams("single", "value");
Create RequestParams from an existing Map of
key/value strings:
HashMap<String, String> paramMap = new HashMap<String, String>();
paramMap.put("key", "value");
RequestParams params = new RequestParams(paramMap);
See the RequestParams Javadoc for more information.
Add an InputStream to the RequestParams to
upload:
InputStream myInputStream = blah;
RequestParams params = new RequestParams();
params.put("secret_passwords", myInputStream, "passwords.txt");
Add a File object to the RequestParams to
upload:
File myFile = new File("/path/to/file.png");
RequestParams params = new RequestParams();
try {
params.put("profile_picture", myFile);
} catch(FileNotFoundException e) {}
Add a byte array to the RequestParams to upload:
byte[] myByteArray = blah;
RequestParams params = new RequestParams();
params.put("soundtrack", new ByteArrayInputStream(myByteArray), "she-wolf.mp3");
See the RequestParams Javadoc for more information.
Downloading Binary Data with BinaryHttpResponseHandler
The BinaryHttpResponseHandler class can be used to fetch binary data such as images and other files. For example:
AsyncHttpClient client = new AsyncHttpClient();
String[] allowedContentTypes = new String[] { "image/png", "image/jpeg" };
client.get("http://example.com/file.png", new BinaryHttpResponseHandler(allowedContentTypes) {
@Override
public void onSuccess(byte[] fileData) {
// Do something with the file
}
});
See the BinaryHttpResponseHandler Javadoc for more information.
android-async-http详解的更多相关文章
- android:ToolBar详解
android:ToolBar详解(手把手教程) 泡在网上的日子 发表于 2014-11-18 12:49 第 124857 次阅读 ToolBar 42 来源 http://blog.mosil.b ...
- Android之canvas详解
首先说一下canvas类: Class Overview The Canvas class holds the "draw" calls. To draw something, y ...
- 【转】Android Canvas绘图详解(图文)
转自:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1212/703.html Android Canvas绘图详解(图文) 泡 ...
- Android 核心分析 之八Android 启动过程详解
Android 启动过程详解 Android从Linux系统启动有4个步骤: (1) init进程启动 (2) Native服务启动 (3) System Server,Android服务启动 (4) ...
- Android GLSurfaceView用法详解(二)
输入如何处理 若是开发一个交互型的应用(如游戏),通常需要子类化 GLSurfaceView,由此可以获取输入事件.下面有个例子: java代码: package eoe.ClearTes ...
- Android编译过程详解(一)
Android编译过程详解(一) 注:本文转载自Android编译过程详解(一):http://www.cnblogs.com/mr-raptor/archive/2012/06/07/2540359 ...
- android屏幕适配详解
android屏幕适配详解 官方地址:http://developer.android.com/guide/practices/screens_support.html 一.关于布局适配建议 1.不要 ...
- Android.mk文件详解(转)
源:Android.mk文件详解 从对Makefile一无所知开始,折腾了一个多星期,终于对Android.mk有了一个全面些的了解.了解了标准的Makefile后,发现Android.mk其实是把真 ...
- Android Studio 插件开发详解四:填坑
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/78265540 本文出自[赵彦军的博客] 在前面我介绍了插件开发的基本流程 [And ...
- Android Studio 插件开发详解三:翻译插件实战
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/78113868 本文出自[赵彦军的博客] 一:概述 如果不了解插件开发基础的同学可以 ...
随机推荐
- target-densitydpi=device-dpi会使其他ui插件布局变小
target-densitydpi=device-dpi会使其他ui插件布局变小 东哥说:不用rem了,把meta改成这样<meta name="viewport" cont ...
- ES6数组新增的几个方法
关于数组中forEach() .map().filter().reduce().some().every()的总结 1.forEach() var arr = [1,2,3,4]; arr.forEa ...
- jenkins更新后出现JNLP-connect,JNLP2-connect警告
在更新jenkins后出现提示 This Jenkins instance uses deprecated protocols: JNLP-connect,JNLP2-connect. It may ...
- Java常量初始化后不会再去重新获取
Java虚拟机编译机制:更改常量部分 最近一个Java项目中需要修改一个静态常量的值,本地修改编译以后调试正常,然后把对应的entity类的class文件上传到服务器对应的目录以后系统依旧我行我素,各 ...
- [ SSH框架 ] Hibernate框架学习之二
一.Hibernate持久化类的编写规范 1.什么是持久化类 Hibernate是持久层的ORM影射框架,专注于数据的持久化工作.所谓持久化,就是将内存中的数据永久存储到关系型数据库中.那么知道了什么 ...
- Linux——makefile编写
以前对makefile的编写,限于刚开始接触,我都比较局限一些死板的格式,有时候就会显得有些繁琐.在进一步了解一些系统编译和链接的知识后,对makefile编写流程有了一些新的认识,所以来此梳理梳理, ...
- bootstrap 栅格系统 HTTP协议 软件架构 B/S C/S 常见的WEB服务器
Day32 bootstrap Bootstrap就是响应式布局最成功的实现,为了兼容不同的浏览器采用jQuery,为了适配不同的终端采用CSS3 Media Query (媒体查询) 1.1.1 栅 ...
- net框架运行原理
核心是CLR(通用语言运行时), c#或者其它各种语言编译原理:将原代码通过相对的编译器(语法检查原代码分析)生成IL代码托管(IL也称托管代码),最后得到一个托管模块,一个或多个托管模块组成程序集( ...
- R语言中函数调试
有时候会用R语言写一下简单的脚本处理函数,加入需要调试的话可以按照下面的步骤进行: fun <- function(x , y){ x + y x - y x * y x / y } debug ...
- MySQL系列教程(五)
MyCAT MyCat是基于阿里开源的Cobar产品而研发,Cobar的稳定性.可靠性.优秀的架构和性能以及众多成熟的使用案例使得MYCAT一开始就拥有一个很好的起点,站在巨人的肩膀上,我们能看到更远 ...