Android网络通信之android

android-async-http入门

在此之前先分享个自由门链接: 密码:cgg7 API原文: ***Android Asynchronous Http Client A Callback-Based Http Client Library for Android***

特征在此网站下都是使用AAH库开发的应用,翻墙进去逛逛吧:安装和基本用法

在里下载android-async-http-1.4.6.jar(最新版)并导入到工程中。

import Http包

import .*;

创建一个新的AsyncHttpClient实例和请求:

AsyncHttpClient client = new AsyncHttpClient();client.get(“http://www.google.com”, new AsyncHttpResponseHandler() {() {// called before request is started}(int statusCode, Header[] headers, byte[] response) {// called when response HTTP status is “200 OK”}(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {// called when response HTTP status is “4XX” (eg. 401, 403, 404)}(int retryNo) {// called when request is retried}});建议用法:静态的Http客户端

在本例中,我们做一个http客户端静态类访问器使它容易与Twitter的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(); (String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {client.get(getAbsoluteUrl(url), params, responseHandler); } (String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {client.post(getAbsoluteUrl(url), params, responseHandler); } private static String getAbsoluteUrl(String relativeUrl) {return BASE_URL + relativeUrl; }}

这样之后很容易在你的代码中使用Twitter API:

import org.json.*;import com.loopj.android.http.*;class TwitterRestClientUsage {() throws JSONException {TwitterRestClient.get(“statuses/public_timeline.json”, null, new JsonHttpResponseHandler() {(int statusCode, Header[] headers, JSONObject response) {// If the response is JSONObject instead of expected JSONArray}(int statusCode, Header[] headers, JSONArray timeline) {// Pull out the first event on the public timelineJSONObject firstEvent = timeline.get(0);String tweetText = firstEvent.getString(“text”);// Do something with the responseSystem.out.println(tweetText);}});}}

查看AsyncHttpClient,RequestParams,AsyncHttpResponseHandler Javadocs的更多的细节

持久化Cookie存储PersistentCookieStore

这个库还包含一个实现Apache HttpClient CookieStore接口的PersistentCookieStore,自动在Android设备上使用SharedPreferences存储保存cookie。 如果你想使用cookie来管理身份验证会话这是非常有用的,,因为用户将继续登录即使关闭并重启应用程序。

首先,创建一个实例AsyncHttpClient:

AsyncHttpClient myClient = new AsyncHttpClient();

现在将这个客户的cookie存储为PersistentCookieStore的一个新实例,构造一个活动或应用程序上下文(通常这就足够了):

PersistentCookieStore myCookieStore = new PersistentCookieStore(this);myClient.setCookieStore(myCookieStore);

任何从服务器收到的cookies都会被存储在持久化cookie存储。

添加自己的cookies进行存储,只需构建一个新的cookie和调用addCookie:

BasicClientCookie newCookie = new BasicClientCookie(“cookiesare”, “awesome”);newCookie.setVersion(1);newCookie.setDomain(“mydomain.com”);newCookie.setPath(“http://blog.csdn.net/”);myCookieStore.addCookie(newCookie);添加GET / POST参数RequestParams

RequestParams类是用于添加可选的GET或POST请求参数。RequestParams可以以各种方式建造而成:

建立空RequestParams并立即添加一些参数:

RequestParams params = new RequestParams();params.put(“key”, “value”);params.put(“more”, “data”);

创建一个参数的RequestParams:

RequestParams params = new RequestParams(“single”, “value”);

创建一个存在map键值对的字符串RequestParams:

HashMapparamMap , String>();paramMap.put(“key”, “value”);RequestParams RequestParams(paramMap);

查看RequestParams Javadoc以获取更多信息。

加参数RequestParams的文件上传

另外RequestParams类支持多部分文件上传,如下所示:

添加一个带有inputStream的requestParams参数用来上传:

InputStream myInputStream = blah;RequestParams params = new RequestParams();params.put(“secret_passwords”, myInputStream, “passwords.txt”);不做任何解释。没有人明白,

Android网络通信之android

相关文章:

你感兴趣的文章:

标签云: