美文网首页
android-async-http 使用基础

android-async-http 使用基础

作者: 雪中洛阳 | 来源:发表于2016-04-12 01:44 被阅读765次

开源项目链接
android-async-http GitHub: https://github.com/loopj/android-async-http
android-async-http主页:http://loopj.com/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<String, String> map = new HashMap<String, String>();
map.put("first_name", "jesse");
map.put("last_name", "yan");
params.put("user", map);

/*
* url params: "what=haha&like=wowo"
*/
Set<String> set = new HashSet<String>();
set.add("haha");
set.add("wowo");
params.put("what", set);

/*
* url params: "languages[]=Java&languages[]=C"
*/
List<String> list = new ArrayList<String>();
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<Map<String, String>> listOfMaps = new ArrayList<Map<String, String>>();
Map<String, String> user1 = new HashMap<String, String>();
user1.put("age", "30");
user1.put("gender", "male");

Map<String, String> user2 = new HashMap<String, String>();
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持久性存储接口。

相关文章

网友评论

      本文标题:android-async-http 使用基础

      本文链接:https://www.haomeiwen.com/subject/wdyvlttx.html