AsyncHttpClient
1.在很多时候android都需要进行网络的操作,而android自带的HttpClient可以实现,但要进行很多网络连接的时候(如:下载很多图片),就需要线程池来进行管理,但默认都是阻塞式操作。这种模型效率不高,对并发要求高的 APP 来讲,并不适用,要用AsyncHttpClient 就必须下载一个jar包 ------>> 下载地址。
2.AsyncHttpClient框架是异步的框架,而且已经封装好了了线程池的操作。所以对网络的操作都是很快的!
3.在AsyncHttpClient框架中主要的类如下:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPIAAADgCAIAAAC2FKxjAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAc3ElEQVR4nO2df5AcZZnHnybGZC/eJVSO8xTklMzmQlwLYwG3zEA0YAKza8xEuI1eqeGS2IOXyKw/FjjNlauuULAqMyRyt2NM2LOKxDFHJi6ZThBKj3P3AnendRIDlelclSCiuGQDScgmsPPeH/3r7e737enpnZ6dfuf51NRm9p3u9327552edzbvZ74SIQQQRCwumOkOIEj9wWGNCAgOa0RAcFgjAvKWxjRz8uSkdmfBgrmNaRFpZRpxtT55cvKZ3/7xmi/u2pAbaUBzCNKgq7XJU7tWvDx+0vx1dd8vG9yBmUBJS8UUGUqquUR7oac8momF1kTdK44mjZ5bL/vk4zf2/rd2u2zJNSODy3zspOYSkpRWwumRmkvY63YV1IiSlgzC6jNSheDDemxs7MyZM7XuRYAQQggBQuCt8xYu8jOy1QMFiMfzxYaPEXqA+xvsSlqSuqBEdFLFRE61Ho1lRkmtl+rpvshak+DD+je/+c2jjz76yiuveG9mfljUqFSgQqQpQiqEvHXewtnzFl62pLNw95UeNagHCtAzvFWegXFdI0q6Ky+XqKlAciiMCQdSlWlNQq6//vp9+/a9+OKLvA20D4tf3f3k3v94FgDe9RfzpwhMVUiFwFQFFl6x5R3XfPWvVm57k0j8RtQDBejpjiVTcn7AuvSpuQT1Tq/mEpJ1XVTSUiKnav/k9BkBddW0Jgm2S6kTJS21945BvkuSEomEdT+n6ldQqx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cCoxaYmci6j/QsvdAPPbfKZD1dfvmnX/hziIEdA9IpaNLvpEZdYTT10di+qTuGMEXjtnxZgNz4+ns1mjx07xtxmYuLsk888v7a/sLa/cPuDh149ff7VM+fdPx/6+lXcZvRcNmpFJ7WS0pYbZ0XMaQ+XZKf7wpRP3Ml0zvtW6yUjPI67mtMdjUdvak+bc3aYuXLVfRRm8Fw5KxsPmB1z19Gk62HDJYIuI9vAc5l8HBuPl7vogZcNWRWuLulPPYy4UzhTRNFlDGTg1ZcaohaDxiVWJQpO4UwRSZfRaeAx8bDxaoRlQ3KiFqt0o1qKpIkoTuFMEU2XkWvg0fBtPFZLlOHnVACZNiQnatG7G94pkkz1MOJO4UwReFY+sy5jowy8lvzAFX2i6TIKauAh9SJ6LqPABh5SL9BlRAQEXUZEQNCOQQQEhzUiIP8PeO1TVdDdoo0AAAAASUVORK5CYII=" alt="" />
主要用到的类有AsyncHttpClient , JsonHttpResponseHandler , RequestParams,
3.请求的方法:
AsyncHttpClient.post(arg0,arg1,arg2);
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAlcAAAAWCAIAAABiwi4zAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAQDElEQVR4nO1ba1AbV5Y+jXlEizOUDcFbU7NVMZKwBLhmslXxADLMmhBAxnYAe7w/sls2tgcZi1gksTzgpIpSjQ1D8AMhk4BsY3t+TWGMeTgIw0xiwyCr+LFbW4BaUre2slOp3TWIZALlMqxD3/3RrUdL3UIyAuLQX50fV1f3nvOd75zbrSc2+d9Lm6LgH7ZgohgMBAgQIECAgI2E6P+dR7niTVEYILTeXAQIECBAgIC1RfQSBZuigBJugQIECBAgYOMhCkB4FyhAgAABAjYoogAAASD00hqhz92kaCHWm4ZgggkWKRtQxezWEwgQMlVuUpnWnc/qGd/ly6vAhjK853fdNgRoZvTa79rGZtYobhR9M0Q/ACNbFLGVJgSAwHQqGouNxmKjsVOm9Se2nHnYKlrIIMvIlh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RxsNhwl5my16a406q40tpqTDhelAgJAiTmZSRM2SJckBqmIvOhEnusevRffmpUUtL2TJPJttnu6Kx2js6wxAIAM8CuNuiuNutsjyft+W0aT3JJ1sro02Xyd9q+7YpP/izsE27MvDR0hY7ZzEWbFDV06BIA9wL/fnbLp/5YgTJCf5UkdNehKQbgbeeDUF/wG2r/QbI+QP24Mqbb8Pu3fVh4luJ8h1Za+km+vKlcYJNRwGxCrr/9wFdYkI4aqJCtxEgIifY5W1+2PGsucMvKzvCNwbaxSvLasVgfOfp0lSf1uVtIqx3GN32z9RlFXlLrKcVYG97/mw7cX3shtYk1bWec/VkX87zjkZ3mqYWZsqi4xvllW/Poq+/lPx+SRA8rIJhJhtV8+W1v9nfqmjgptlWQNUlulym74hgnFwmiq4SrpvbLbJ8URibveNjtqwXf+Mmst/p8HL0MrRjMUEYQLBADoRTby4XX12OwLMQkKicqYVvR32GEAAHjzE2JQLXmhEGH4eV09djmyiSAAcH9HskGxdvr/6T3snzvgyD301poIHvFztKpuf1wIo6neMqC3In91Wgc47+sGcNjxTl3Kml1PfvB9iH3p+P4XP9v0YjdCAQIECBAg4KUGZv2fJQTwsy1RUetNRYAAAQIECFhjYN8vIfwJ9WQeLVHLrxYgQIAAAQJ+TMCQ8EmoAAECBAjYqBA+BxUgQIAAARsX/w9AKO4yAFBUXQAAAABJRU5ErkJggg==" alt="" />
arg0:是你post请求的url
arg1:是要发送给服务器的数据 ,他是<key,value>形式的
arg2:发送请求的一连串操作 如下:
JsonHttpResponseHandler是AsyncHttpResponseHandler的子类
JsonHttpResponseHandler handler = new JsonHttpResponseHandler() {
@Override
public void onFailure(Throwable arg0, String arg1) { super.onFailure(arg0, arg1); } @Override
public void onFinish() { super.onFinish(); } @Override
public void onSuccess(JSONObject arg1) {
super.onSuccess(arg1); } @Override
public void onStart() { super.onStart(); }
};
1)onStart() 方法是开始发送请求的时候执行的,一般是把progressDialog放在这里面显示,提示用户等待
2)onFailure() 如果请求失败了,就会执行该方法,其余方法就都不执行了,用于提示用户网络出现故障
3)onSuccess() 是请求成功后,服务器就会返回JSONObject, arg1就是返回的JSONObject, 在里面解析JSONObject来回去数据,并显示在UI上
4)onFinish() 当发送请求成功后,就执行onFinish()
如上就是一些基本的用法。
这里贴一个结合AsyncHttpClient + 单例模式 + 观察者模式的例子
接口:
public interface TaskListener { public abstract void start();
public abstract String contentype(); // text, file
public abstract String filepath();
public abstract String requestUrl();
public abstract JSONObject requestData();
public abstract void messageUpdated(JSONObject msg);
public abstract void failure(String str);
public abstract void finish();
public abstract boolean needCacheTask(); public abstract String readCache();
public abstract void updateCacheDate(List<HashMap<String, Object>> cacheData); }
实现:
public class CommandBase { private static CommandBase m_instance = null; private HashMap<AsyncHttpResponseHandler, TaskListener> m_task = new HashMap<AsyncHttpResponseHandler, TaskListener>(); private AsyncHttpClient m_httpClient = new AsyncHttpClient(); private UploadQueue m_uploadQueue = null;
private HashMap<AsyncHttpResponseHandler, UploadItem> m_tempRequest = new HashMap<AsyncHttpResponseHandler, UploadItem>(); private static String m_account = "";
private static String m_session = ""; private static String HostUrl = "http://222.18.162.187:8080/wasp/webservice/ap/";
private static JSONObject m_postData = new JSONObject(); Context m_context;
Context m_currContext;
Activity m_currActivity; public void setCurrActivityContext(Context context, Activity activity) {
m_currContext = context;
m_currActivity = activity;
} public void setMainActivityContext(Context context) {
m_context = context; if (m_uploadQueue == null)
m_uploadQueue = new UploadQueue(m_context);
} public static CommandBase instance() {
if (m_instance == null)
m_instance = new CommandBase(); return m_instance;
} private CommandBase() {
m_httpClient.addHeader("Content-Type", "application/json");
m_httpClient.addHeader("Request-Client", "mobile/1.0.0"); /*
* JSONObject userData = new JSONObject(); try { userData.put("account",
* m_account); userData.put("session", m_session);
* m_postData.put("user", userData); } catch (JSONException e) {
* e.printStackTrace(); }
*/
} public void setUserInfo(String name, String session) {
m_account = name;
m_session = session;
System.out.println("setUser");
System.out.println("account=" + m_account);
JSONObject userData = new JSONObject();
try {
userData.put("account", m_account);
userData.put("session", m_session);
m_postData.put("user", userData);
} catch (JSONException e) {
e.printStackTrace();
}
} CacheManager manager = new CacheManager(); public void readCache(final TaskListener taskListener) {
List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
String queryStr = taskListener.readCache();
list = manager.getCacheData(queryStr);
taskListener.updateCacheDate(list);
request(taskListener);
} public void request(final TaskListener task) {
RequestParams postParam = new RequestParams();
postParam.put("req", requestData(task.requestData())); JsonHttpResponseHandler handler = new JsonHttpResponseHandler() {
@Override
public void onFailure(Throwable arg0, String arg1) {
task.failure(arg1);
super.onFailure(arg0, arg1);
System.out.println("网络连接失败..");
if (task.needCacheTask() && m_uploadQueue != null
&& m_tempRequest.containsKey(this))
m_uploadQueue.addItemToQueue(m_tempRequest.get(this));
m_tempRequest.remove(this);
Toast.makeText(m_currContext, "网络连接失败", Toast.LENGTH_SHORT)
.show();
} @Override
public void onFinish() {
task.finish();
super.onFinish();
System.out.println("请求结束");
} @Override
public void onSuccess(JSONObject arg1) {
super.onSuccess(arg1);
System.out.println("连接服务器成功!");
System.out.println("结果=" + arg1);
readData(arg1, this);
} @Override
public void onStart() {
task.start();
super.onStart();
System.out.println("请求开始");
}
}; // TODO: save network request to local DB
if (task.needCacheTask()) {
System.out.println("++++++++++++++++++++++++++++++++++++++++");
m_tempRequest.put(
handler,
new UploadItem(0, requestUrl(task.requestUrl()),
requestData(task.requestData()),
task.contentype() == "text" ? 0 : 1, task
.filepath())); } m_task.put(handler, task);
m_httpClient.post(requestUrl(task.requestUrl()), postParam, handler); } private String requestUrl(String ap) {
return HostUrl + ap + ".action";
} private String requestData(JSONObject data) {
JSONObject postData = new JSONObject();
postData = m_postData;
try {
postData.put("data", data);
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println("postData=" + postData);
return postData.toString();
} private void readData(JSONObject data, AsyncHttpResponseHandler handler) {
TaskListener task = m_task.get(handler); if (task == null) {
m_task.remove(handler);
m_tempRequest.remove(handler);
return;
}
// TODO: Check the response data has Error or not
try {
JSONObject object = data.getJSONObject("error");
String code = object.getString("code");
String errorTag = object.getString("string");
System.out.println("code=" + code);
System.out.println("error=" + errorTag);
if (code.equals("0")) {
object = data.getJSONObject("data");
task.messageUpdated(data);
manager.updateCacheData(data);
m_tempRequest.remove(handler);
} else if (code.equals("-99")) {
Toast.makeText(m_currContext, "用户信息已过期,请重新登陆",
Toast.LENGTH_SHORT).show();
m_currActivity.finish();
} else {
System.out.println("#####################");
if (task.needCacheTask() && m_uploadQueue != null
&& m_tempRequest.containsKey(handler)) {
System.out.println("_____________________________");
m_uploadQueue.addItemToQueue(m_tempRequest.get(handler));
}
m_tempRequest.remove(handler);
Toast.makeText(m_currContext, errorTag, Toast.LENGTH_SHORT)
.show();
}
} catch (JSONException e) {
e.printStackTrace();
if (task.needCacheTask() && m_uploadQueue != null
&& m_tempRequest.containsKey(handler)) {
m_uploadQueue.addItemToQueue(m_tempRequest.get(handler));
}
m_tempRequest.remove(handler); } m_task.remove(handler);
} }