新建工具类,方便调用:
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
,这个类是Apache
HttpClient CookieStore 接口的实现,它可以自动将cookies保存到SharedPreferences 。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() {
@Override
public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
// TODO Auto-generated method stub
Toast.makeText(LoginActivity.this, "登录失败,请检查用户名密码是否正确" + arg1[1],
Toast.LENGTH_LONG).show();
checkEmail();
}
@Override
public void onFailure(int arg0, Header[] arg1, byte[] arg2,
Throwable arg3) {
。。。。。
}
});
}