android-async-http读写cookie,android-async-http 使用基础

使用方法直接看他主页介绍, 下面列出个推荐使用方法

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 = "https://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(int statusCode, Header[] headers, JSONObject response) {

// If the response is JSONObject instead of expected JSONArray

}

@Override

public void onSuccess(int statusCode, Header[] headers, JSONArray timeline) {

// 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);

}

});

}

}

RequestParams的基础使用

RequestParams params = new RequestParams();

params.put("username", "yanbober");

params.put("password", "123456");

params.put("email", "yanbobersky@email.com");

/*

* Upload a File

*/

params.put("file_pic", new File("test.jpg"));

params.put("file_inputStream", inputStream);

params.put("file_bytes", new ByteArrayInputStream(bytes));

/*

* url params: "user[first_name]=jesse&user[last_name]=yan"

*/

Map map = new HashMap();

map.put("first_name", "jesse");

map.put("last_name", "yan");

params.put("user", map);

/*

* url params: "what=haha&like=wowo"

*/

Set set = new HashSet();

set.add("haha");

set.add("wowo");

params.put("what", set);

/*

* url params: "languages[]=Java&languages[]=C"

*/

List list = new ArrayList();

list.add("Java");

list.add("C");

params.put("languages", list);

/*

* url params: "colors[]=blue&colors[]=yellow"

*/

String[] colors = { "blue", "yellow" };

params.put("colors", colors);

/*

* url params: "users[][age]=30&users[][gender]=male&users[][age]=25&users[][gender]=female"

*/

List> listOfMaps = new ArrayList>();

Map user1 = new HashMap();

user1.put("age", "30");

user1.put("gender", "male");

Map user2 = new HashMap();

user2.put("age", "25");

user2.put("gender", "female");

listOfMaps.add(user1);

listOfMaps.add(user2);

params.put("users", listOfMaps);

/*

* 使用实例

*/

AsyncHttpClient client = new AsyncHttpClient();

client.post("http://localhost:8080/androidtest/", params, responseHandler);

总结

AsyncHttpResponseHandler是一个请求返回处理、成功、失败、开始、完成等自定义的消息的类。

BinaryHttpResponseHandler是继承AsyncHttpResponseHandler的子类,这是一个字节流返回处理的类,用于处理图片等类。

JsonHttpResponseHandler是继承AsyncHttpResponseHandler的子类,这是一个json请求返回处理服务器与客户端用json交流时使用的类。

AsyncHttpRequest继承自Runnable,是基于线程的子类,用于异步请求类, 通过AsyncHttpResponseHandler回调。

PersistentCookieStore继承自CookieStore,是一个基于CookieStore的子类, 使用HttpClient处理数据,并且使用cookie持久性存储接口。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值