Android網(wǎng)絡(luò)請求庫android-async-http介紹
Android網(wǎng)絡(luò)請求庫:android-async-http開源框架
之前有一篇描述了客戶端請求服務(wù)器端的方式—Post的請求方式。今天介紹一個請求服務(wù)器的一個開源庫—android-async-http庫。
1. 概念:
這個網(wǎng)絡(luò)請求庫是基于Apache HttpClient庫之上的一個異步網(wǎng)絡(luò)請求處理庫,網(wǎng)絡(luò)處理均基于Android的非UI線程,通過回調(diào)方法(匿名內(nèi)部類)處理請求結(jié)果。
2. 特征:
(1).處理異步Http請求,并通過匿名內(nèi)部類處理回調(diào)結(jié)果
**(2).**Http異步請求均位于非UI線程,不會阻塞UI操作。
(3).通過線程池處理并發(fā)請求處理文件上傳、下載,響應(yīng)結(jié)果自動打包JSON格式。
3. 相應(yīng)的核心類的介紹:
(1).AsyncHttpResponseHandler:請求返回處理成功、失敗、開始、完成等自定義的消息的類。
(2).BinaryHttpResponseHandler:AsyncHttpResponseHandler的子類。該類是一個字節(jié)流返回處理的類。用于處理圖片等。
(3).JsonHttpResponseHandler:AsyncHttpResponseHandler的子類。這是一個服務(wù)器與客戶端之間用Json數(shù)據(jù)交流時使用的類??蛻舳苏埱蠓?wù)器的參數(shù)是Json數(shù)據(jù)類型的,服務(wù)器返回給客戶端的數(shù)據(jù)也是Json數(shù)據(jù)類型的。
(4).RequestParams:封裝參數(shù)處理的類。將客戶端請求的參數(shù)封裝在該類中。
(5).AsyncHttpClient:異步客戶端請求的類。
(6).SyncHttpClient:同步客戶端請求的類。AsyncHttpClient的子類。
注意:HttpUtil這個類主要列出了我們常用的get方法,在要使用的地方,調(diào)用該類就行了。需要添加android-async-http-1.4.7.jar文件包。
代碼如下:
(1).AsyncJsonUtilGet.java
package com.chengdong.su.util.get; import org.apache.http.Header; import org.json.JSONException; import org.json.JSONObject; import android.content.Context; import android.widget.Toast; import com.loopj.android.http.JsonHttpResponseHandler; import com.loopj.android.http.RequestParams; /** * 異步請求服務(wù)器 * * @author scd * */ public class AsyncJsonUtilGet { private static final String URL = ""; private Context mContext; /** * 構(gòu)造方法 * * @param mContext */ public AsyncJsonUtilGet(Context mContext) { super(); this.mContext = mContext; } /** * 郵箱注冊 */ public void emailRegister(String email, String password, String username) { RequestParams params = new RequestParams(); JSONObject jsonObject = new JSONObject(); try { jsonObject.put("email", email); jsonObject.put("password", password); jsonObject.put("username", username); } catch (JSONException e) { e.printStackTrace(); } params.put("jsonObject", jsonObject); // get請求方式 HttpUtil.get(URL, params, new JsonHttpResponseHandler() { @Override public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) { super.onFailure(statusCode, headers, throwable, errorResponse); Toast.makeText(mContext, "Register failed!", Toast.LENGTH_SHORT) .show(); } @Override public void onSuccess(int statusCode, Header[] headers, JSONObject response) { String errorCode = response.optString("ErrorCode"); // 表示請求成功 if (errorCode.equals("0")) { Toast.makeText(mContext, "注冊成功", Toast.LENGTH_LONG).show(); // response:返回的數(shù)據(jù)都在這個參數(shù)中,根據(jù)業(yè)務(wù)要求進(jìn)行實現(xiàn)功能 } else { super.onSuccess(statusCode, headers, response); } } }); } }
(2).HttpUtil.java工具類:注:需要添加jar包
package com.chengdong.su.util.get; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.List; import java.util.Map; import java.util.Set; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; 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.RequestParams; public class HttpUtil { public static final String STATUS_NETWORK = "network_available"; private static AsyncHttpClient client = new AsyncHttpClient(); static { client.setTimeout(11000); } public static void get(String urlString, AsyncHttpResponseHandler res) { client.get(urlString, res); } public static void get(String urlString, RequestParams params, AsyncHttpResponseHandler res) { client.get(urlString, params, res); } public static void get(String urlString, JsonHttpResponseHandler res) { client.get(urlString, res); } public static void get(String urlString, RequestParams params, JsonHttpResponseHandler res) { client.get(urlString, params, res); } public static void get(String uString, BinaryHttpResponseHandler bHandler) { client.get(uString, bHandler); } public static AsyncHttpClient getClient() { return client; } public static boolean isNetworkConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager .getActiveNetworkInfo(); if (mNetworkInfo != null) { return mNetworkInfo.isAvailable(); } } return false; } // 從UrlConnection中獲取文件名稱 public static String getFileName(String url) { String fileName = null; boolean isOK = false; try { URL myURL = new URL(url); URLConnection conn = myURL.openConnection(); if (conn == null) { return null; } Map<String, List<String>> hf = conn.getHeaderFields(); if (hf == null) { return null; } Set<String> key = hf.keySet(); if (key == null) { return null; } for (String skey : key) { List<String> values = hf.get(skey); for (String value : values) { String result; try { result = value; int location = result.indexOf("filename"); if (location >= 0) { result = result.substring(location + "filename".length()); result = java.net.URLDecoder .decode(result, "utf-8"); result = result.substring(result.indexOf("\"") + 1, result.lastIndexOf("\"")); fileName = result .substring(result.indexOf("=") + 1); isOK = true; } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } if (isOK) { break; } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return fileName; } }
開源之家介紹:http://open.jb51.net/project/app-framework/android-async-http.html
相關(guān)文章
Android?NotificationListenerService通知監(jiān)聽服務(wù)使用
這篇文章主要為大家介紹了Android?NotificationListenerService通知監(jiān)聽服務(wù)使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Android超清晰6.0權(quán)限申請AndPermission
這篇文章主要介紹了Android超清晰6.0權(quán)限申請AndPermission,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11解決Android調(diào)用系統(tǒng)分享給微信,出現(xiàn)分享失敗,分享多文件必須為圖片格式的問題
這篇文章主要介紹了解決Android調(diào)用系統(tǒng)分享給微信,出現(xiàn)分享失敗,分享多文件必須為圖片格式的問題,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09Android 通過webservice上傳多張圖片到指定服務(wù)器詳解
這篇文章主要介紹了Android 通過webservice上傳多張圖片到指定服務(wù)器詳解的相關(guān)資料,需要的朋友可以參考下2017-02-02Android中使用PopupWindow 仿微信點贊和評論彈出
微信朋友圈的點贊和評論功能,有2個組成部分:左下角的“更多”按鈕;點擊該按鈕后彈出的對話框。這篇文章主要介紹了Android中使用PopupWindow 仿微信點贊和評論彈出,需要的朋友可以參考下2017-04-04Android 7.0調(diào)用相機崩潰詳解及解決辦法
這篇文章主要介紹了 Android 7.0調(diào)用相機崩潰詳解及解決辦法的相關(guān)資料,需要的朋友可以參考下2016-12-12