- 浏览: 505613 次
- 性别:
- 来自: 深圳
文章分类
- 全部博客 (200)
- java基础 (30)
- ajax (19)
- 乱写 (5)
- groovy (2)
- db (8)
- gwt (0)
- jee (2)
- 我关注的开源 (1)
- RIA AIR (1)
- spring (11)
- lucene (0)
- 工具 (10)
- 百科 (2)
- linux (6)
- android (40)
- 移动开发 (21)
- 代码片断 (15)
- tomcat (1)
- css (1)
- html5 (2)
- jquery (2)
- playframework (3)
- web (2)
- nio (3)
- design (1)
- nosql (3)
- 日志 (12)
- mysql (4)
- 图表 (1)
- python (3)
- ruby (1)
- git (0)
- hibernate (1)
- springboot (1)
- guava (1)
- mybatis (0)
- 工作问题 (3)
- php (1)
最新评论
-
linzm1990:
踩了很多坑啊。。。。
hibernate @Nofound 与@ManyToOne fetch lazy的问题 -
Ccccrrrrrr:
...
转: Spring boot 文件上传 -
rmzdb:
兄弟,你这个东西,在ie内核的浏览器,貌似不识别 文件名
工作问题:http下载文件,中文文件名在firefox下乱码问题 -
107x:
问题解决了,谢谢!
工作问题:http下载文件,中文文件名在firefox下乱码问题 -
klxqljq:
额鹅鹅鹅
android布局实现头尾固定, 中间多余内容可以滚动
转自: loopj.com/android-async-http/
github上的地址: https://github.com/loopj/android-async-http
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.
Features
- Make asynchronous HTTP requests, handle responses in anonymous callbacks
- HTTP requests happen outside the UI thread
- Requests use a threadpool to cap concurrent resource usage
- GET/POST params builder (RequestParams)
- Multipart file uploads with no additional third party libraries
- Tiny size overhead to your application, only 19kb for everything
- Automatic smart request retries optimized for spotty mobile connections
- Automatic gzip response decoding support for super-fast requests
- Optional built-in response parsing into JSON (JsonHttpResponseHandler)
- Optional persistent cookie store , saves cookies into your app’s SharedPreferences
Who is Using It?
Send me a message on github to let me know if you are using this library in a released android application!
Installation & Basic Usage
Download the latest .jar file from github and place it in your Android app’s
libs/
folder.
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); } });
Recommended Usage: Make a Static Http Client
In this example, we’ll make a http client class with static accessors to make it easy to communicate with Twitter’s API.
import com.loopj.android.http.*; public class TwitterRestClient { private static final String BASE_URL = "http://api.twitter.com/1/"; private static AsyncHttpClient client = new AsyncHttpClient(); public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) { client.get(getAbsoluteUrl(url), params, responseHandler); } public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) { client.post(getAbsoluteUrl(url), params, responseHandler); } private static String getAbsoluteUrl(String relativeUrl) { return BASE_URL + relativeUrl; } }
This then makes it very easy to work with the Twitter API in your code:
import org.json.*; import com.loopj.android.http.*; class TwitterRestClientUsage { public void getPublicTimeline() throws JSONException { TwitterRestClient.get("statuses/public_timeline.json", null, new JsonHttpResponseHandler() { @Override public void onSuccess(JSONArray response) { // Pull out the first event on the public timeline JSONObject firstEvent = timeline.get(0); String tweetText = firstEvent.getString("text"); // Do something with the response System.out.println(tweetText); } }); } }
Check out the AsyncHttpClient , RequestParams and AsyncHttpResponseHandler Javadocs for more details.
Persistent Cookie Storage with PersistentCookieStore
This library also includes a PersistentCookieStore
which is an implementation
of the Apache HttpClient CookieStore
interface that automatically saves
cookies to SharedPreferences
storage on the Android device.
This is extremely useful if you want to use cookies to manage authentication sessions, since the user will remain logged in even after closing and re-opening your app.
First, create an instance of AsyncHttpClient
:
AsyncHttpClient myClient = new AsyncHttpClient();
Now set this client’s cookie store to be a new instance of
PersistentCookieStore
, constructed with an activity or application context
(usually this
will suffice):
PersistentCookieStore myCookieStore = new PersistentCookieStore(this); myClient.setCookieStore(myCookieStore);
Any cookies received from servers will now be stored in the persistent cookie store.
To add your own cookies to the store, simply construct a new cookie and
call addCookie
:
BasicClientCookie newCookie = new BasicClientCookie("cookiesare", "awesome"); newCookie.setVersion(1); newCookie.setDomain("mydomain.com"); newCookie.setPath("/"); myCookieStore.addCookie(newCookie);
See the PersistentCookieStore Javadoc for more information.
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.
Uploading Files with RequestParams
The RequestParams
class additionally supports multipart file uploads as
follows:
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.
Building from Source
To build a .jar
file from source, first make a clone of the
android-async-http github repository. You’ll then need to copy the
local.properties.dist
file to local.properties
and edit the sdk.dir
setting to point to where you have the android sdk installed. You can then run:
ant package
This will generate a file named android-async-http-version.jar
.
Reporting Bugs or Feature Requests
Please report any bugs or feature requests on the github issues page for this project here:
https://github.com/loopj/android-async-http/issues
Credits & Contributors
RequestParams
SimpleMultipartEntity
code
License
The Android Asynchronous Http Client is released under the Android-friendly Apache License, Version 2.0. Read the full license here:
http://www.apache.org/licenses/LICENSE-2.0
About the Author
I'm James Smith, CTO of heyzap.com . Originally from London, UK I now live in San Francisco.
发表评论
-
android listview
2012-07-13 17:37 927ListView与Button的共存问题解决, 解决在list ... -
演化理解 Android 异步加载图片
2011-11-09 09:55 902LinearLayout 布局,其下放了5个ImageView ... -
android常用颜色
2011-11-07 08:49 1277常用颜色值: 可以完美的颜色比对的网站: http://w ... -
dialog,activity 屏蔽Home键详解
2011-11-03 09:39 0http://www.iteye.com/topic/1116 ... -
android SlidingDrawer example
2011-11-03 09:35 0http://disanji.net/2010/12/16/a ... -
play flash swf file in android with webview
2011-11-03 09:34 0http://androidforums.com/applic ... -
AnimationDrawable 在Dialog中不能动画的原因(转)
2011-11-03 09:33 1302原来在dialog的onCreate onStart调用的时候 ... -
Free Android UI library & component roundup
2011-11-03 09:27 1146http://java.dzone.com/articles/ ... -
Android Fundamentals: Scheduling Recurring Tasks
2011-11-03 09:26 982http://mobile.tutsplus.com/tuto ... -
Android ListView pull up to refresh 改造(转)
2011-11-03 09:25 2097转自: http://dengyin2000.iteye.co ... -
Android中dp和px之间进行转换
2011-11-03 09:02 2258在xml布局文件中,我们既可以设置px,也可以设置dp(或者d ... -
view的setTag() 和 getTag()应用
2011-10-31 12:19 29932View中的setTag(Onbect)表示给View添加一个 ... -
使用getIdentifier()获取资源Id
2011-10-31 12:15 8457使用getIdentifier()获取资源Id int i ... -
ListView的长按菜单___源码分析
2011-10-24 09:28 2591ListView的长按菜单___源码分析 Android的l ... -
让你的Android程序兼容多种分辨率
2011-10-24 09:20 1023http://www.android123.com.cn/an ... -
andr菜单
2011-10-24 09:18 1208Android 菜单 菜单分为两种:系统菜单和上下文菜单。 ... -
Android 长按显示上下文菜单代码
2011-10-24 09:14 5938Android 长按显示上下文 ... -
Android canvas.drawBitmap实现透明效果
2011-09-02 14:22 23433以下是针对,canvas.drawBitmap方法实施透明效 ... -
android资源别名
2011-08-30 14:24 2320详细请参考: http://developer.android ... -
Android触摸屏手势识别
2011-08-24 13:50 1113转自: http://www.cnblogs.com/-OYK ...
相关推荐
// 1.创建异步请求的客户端对象 ...其他有什么问题或者想具体了解详细说明,可以参考官网 http://loopj.com/android-async-http/ 其他参考链接 http://blog.csdn.net/redarmy_chen/article/details/26980613
cc.itbox.babysay.activities |--BaseActivity 基类Activity |--GuideActivity 引导页Activity ... Android Asynchronous Http Client Holoeverywhere SmoothProgressBar ActionBar-PullToRefresh
This library requires Android Asynchronous Http Client by James Smith. You have to maintain cookies with com.loopj.android.http.PersistentCookieStore class. Or you can try my fork of Android ...
Asynchronous Http Client for Android An asynchronous, callback-based Http client for Android built on top of Apache's HttpClient libraries. Changelog See what is new in version 1.4.9 released on 19th...
在Android开发中,网络通信是应用的核心功能之一,而`Android Asynchronous HTTPClient`(也称为AsyncHttpClient)是一个流行的库,用于实现异步HTTP请求,它使得开发者可以在不阻塞主线程的情况下执行网络操作,...
Asynchronous Http Client for Android Build Status An asynchronous, callback-based Http client for Android built on top of Apache's HttpClient libraries. Changelog See what is new in version 1.4.9 ...
框架目前主要包含的功能有View ...UltimateAndroid框架是如同flask框架(python)那样包含了许多其他的开源项目的框架,比如 Butter Knife,Asynchronous Http Client for Android, Universal Image Loader for Android
代码是基于loopj (Asynchronous Http Client for Android) 的文件上传Demo,loopj 是基于 Apache's HttpClient 的异步http客户端。
An asynchronous, callback-based Http client for Android built on top of Apache's HttpClient libraries.
4. **网络通信**:介绍如何使用HttpURLConnection或OkHttp进行网络请求,JSON数据的解析,以及Asynchronous Http Client等第三方库的使用。 5. **多媒体**:涵盖音频、视频的播放与录制,图像加载库如Glide或...
- Asynchronous HTTP Client:了解异步网络请求处理,提高应用性能。 **第09章 手机的Google服务功能** - Google Play Services:了解Google提供的API和服务,如地图、定位、Google Sign-In等。 - Maps API:学习...
`Asynchronous HTTP Client`(简称`AsyncHTTP`)是一个流行的库,用于在Android平台上执行异步HTTP请求,它可以帮助开发者高效地处理网络数据交互,避免了主线程被阻塞的问题,提高了用户体验。本项目通过`async...
网络通信使用 Asynchronous Http Client 实现 图片下载采用 Volley, 图片缓存为 LruCache DiskLruCache 的双重缓存 使用 largeHeap 避免加载大量图片的帖子时出现 OOM 使用 Jsoup分析返回的网络请求 缓存了论坛列表...
3. Asynchronous HTTP Client(异步HTTP客户端):由于Android应用需要保持用户界面的响应性,通常我们会使用异步方式处理网络请求。Android的`AsyncTask`类可以方便地实现后台任务,避免阻塞主线程。此外,OkHttp等...
4. **Asynchronous HTTP Client**:考虑到异步处理的重要性,通常会在后台线程(如AsyncTask)中执行HTTP请求,避免阻塞UI。这样可以防止应用因长时间网络操作而冻结,提高用户体验。 5. **Adapter**:适配器是连接...
4. **Jabber Client Library for Android (Smack)**:Smack是Android上常用的XMPP库,它提供了API,简化了Android应用与XMPP服务器的交互。开发者可以使用Smack来创建连接、登录、发送和接收消息,以及处理用户状态...
- 使用Asynchronous Http Client或其他异步库处理网络请求,避免阻塞主线程。 5. **安全与优化** - 对于HTTPS请求,使用SSLSocketFactory处理SSL/TLS连接,确保数据传输的安全性。 - 使用Gson或Jackson库将对象...
Mina(MINA,全称Java Minimal Asynchronous Network Library)是一个高效的、基于NIO(Non-blocking I/O)的网络应用程序框架,主要用于简化网络编程,尤其是TCP和UDP协议的开发。在Android平台上,由于系统资源...
3. **Asynchronous Http Client**: 对于复杂的网络请求,开发者经常选择异步处理,以避免阻塞主线程。Android的Volley库或者第三方库如OkHttp和Retrofit提供了这样的异步网络请求支持。 4. **JSON数据解析**: ...
4. **Asynchronous HTTP Client**:如果项目使用了`AsyncTask`或自定义线程进行网络请求,这是异步处理的简单方式。理解`doInBackground()`和`onPostExecute()`方法的生命周期对于处理后台任务至关重要。 5. **JSON...