loopj----Android Asynchronous Http Client(AHC)

来源:互联网 发布:如何把iphone投影到mac 编辑:程序博客网 时间:2024/05/11 22:18

新建工具类,方便调用:

package com.blueware.rpm.util;import com.loopj.android.http.AsyncHttpClient;import com.loopj.android.http.AsyncHttpResponseHandler;import com.loopj.android.http.BinaryHttpResponseHandler;import com.loopj.android.http.JsonHttpResponseHandler;import com.loopj.android.http.PersistentCookieStore;import com.loopj.android.http.RequestParams;public class HttpUtil {    private static     AsyncHttpClient client; //实例话对象        public static void get(String urlString,AsyncHttpResponseHandler res)    //用一个完整url获取一个string对象    {        client.get(urlString, res);    }    public static void post(String urlString,AsyncHttpResponseHandler res)        {        client.post(urlString, res);    }    public static void get(String urlString,RequestParams params,AsyncHttpResponseHandler res)   //url里面带参数    {        client.get(urlString, params,res);    }    public static void post(String urlString, RequestParams params, AsyncHttpResponseHandler res) {        client.post(urlString, params, res);    }        public static void get(String urlString,JsonHttpResponseHandler res)   //不带参数,获取json对象或者数组    {        client.get(urlString, res);    }    public static void post(String urlString,JsonHttpResponseHandler res)       {        client.post(urlString, res);    }    public static void get(String urlString,RequestParams params,JsonHttpResponseHandler res)   //带参数,获取json对象或者数组    {        client.get(urlString, params,res);    }    public static void post(String urlString,RequestParams params,JsonHttpResponseHandler res)       {        client.post(urlString, params,res);    }    public static void get(String uString, BinaryHttpResponseHandler bHandler)   //下载数据使用,会返回byte数据    {        client.get(uString, bHandler);    }    public static void post(String uString, BinaryHttpResponseHandler bHandler)      {        client.post(uString, bHandler);    }    public static AsyncHttpClient getClient()    {        return client;    }}

就很容易的在需要请求网路的地方发送 网络请求:

import org.json.*;import com.loopj.android.http.*;class TwitterRestClientUsage {    public void getPublicTimeline() throws JSONException {        <pre name="code" class="java">    HttpUtil<span style="font-family: Arial;">.get("statuses/public_timeline.json", null, new JsonHttpResponseHandler() {</span>
@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); } }); }}

使用PersistentCookieStore保存Cookie
这个库包含一个PersistentCookieStore ,这个类是Apache HttpClient CookieStore 接口的实现,它可以自动将cookies保存到SharedPreferences 。
如果你需要使用cookie保持认证会话,这将是特别重要的,因为即使用户关掉了应用仍然可以登录状态。
首先,创建一个AsyncHttpClient对象:
AsyncHttpClient myClient = new AsyncHttpClient();

现在将client的Cookie保存到一个PersistentCookieStore,构造方法需要有一个上下文(Activity,Application都可以,通常this就OK了)。

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

注意:

以上工具类适合于HTTP请求,若是HTTPS请求,须在工具类中加一段代码:

private static boolean isSSL=true;    static    {       if(!isSSL){     client =new AsyncHttpClient();     }else{    client = new AsyncHttpClient(true, 80, 443);    }     client.setEnableRedirects(false);//设置不允许重定向        client.setTimeout(10000);   //设置链接超时,如果不设置,默认为10s    }
大笑OK了

Https请求下将client的Cookie保存到一个PersistentCookieStore

public void sentPostMessage() {String urlString = ApiConfig.LOGIN;RequestParams params = new RequestParams();params.put("username", emailEditText.getText().toString());params.put("password", passwdEditText.getText().toString());<span style="background-color: rgb(255, 204, 153);">AsyncHttpClient myClient=new AsyncHttpClient(true, 80, 443);PersistentCookieStore myCookieStore=new PersistentCookieStore(this);myClient.setCookieStore(myCookieStore);myClient.setEnableRedirects(false);myClient</span>.post(urlString, params, new AsyncHttpResponseHandler() {@Overridepublic void onSuccess(int arg0, Header[] arg1, byte[] arg2) {// TODO Auto-generated method stubToast.makeText(LoginActivity.this, "登录失败,请检查用户名密码是否正确" + arg1[1],Toast.LENGTH_LONG).show();checkEmail();}@Overridepublic void onFailure(int arg0, Header[] arg1, byte[] arg2,Throwable arg3) {。。。。。          }});}




0 0