android-async-http开源项目介绍及使用方法

  • 自动解码支持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

  1. 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值