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();
PersistentCookieStore myCookieStore = new PersistentCookieStore(this);myClient.setCookieStore(myCookieStore);
注意:
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
- loopj----Android Asynchronous Http Client(AHC)
- 开源项目:LoopJ(Android Asynchronous Http Client - 开源http 多线程引擎)
- 开源项目:LoopJ(Android Asynchronous Http Client - 开源http 多线程引擎)
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- Android Asynchronous Http Client
- hdu 1569 最大权独立集
- Libgdx 实现技能冷却效果
- 小心陷阱: 迁移应用到Cluster遇到的Timer问题
- UpdateData、Invalidate、InvalidateRect和UpdateWindow及RedrawWindow的比较
- typedef int(init_fnc_t) (void);这句话是什么意思啊?
- loopj----Android Asynchronous Http Client(AHC)
- 判断一个对象是否为空
- 拿什么拯救你,我的WiFi共享精灵!
- SDL2.0在mfc窗口中显示yuv的一种方法
- 使用socket实现简单数据文件传输
- bind远程Service 过程分析,基于4.4.3源码
- hdu 4912 Paths on the tree LCA + 贪心
- 一张图解释适配器模式
- LeetCode-Binary Tree Inorder Traversal