Android network request library: android-async-http open source framework
There was a previous article describing the way the client requested the server-side - Post request method. Today, we will introduce an open source library for a request server - android-async-http library.
1. Concept:
This network request library is based on an asynchronous network request processing library on the Apache HttpClient library. The network processing is based on Android's non-UI threads and processes the request results through callback methods (anonymous internal classes).
2. Features:
(1). Process asynchronous Http requests and handle callback results through anonymous internal classes
**(2).**Http asynchronous requests are all located in non-UI threads and will not block UI operations.
(3). Process concurrent requests through thread pool to process files upload and download, and the response results are automatically packaged in JSON format.
3. Introduction to the corresponding core categories:
(1).AsyncHttpResponseHandler: The request returns a class that processes custom messages such as successful, failed, started, and completed.
(2).BinaryHttpResponseHandler: AsyncHttpResponseHandler subclass. This class is a class that is processed by the byte stream return. Used to process pictures, etc.
(3).JsonHttpResponseHandler: AsyncHttpResponseHandler subclass. This is a class used when communicating Json data between servers and clients. The parameters of the client requesting the server are of Json data type, and the data returned by the server to the client are of Json data type.
(4).RequestParams: Class that encapsulates parameter processing. Encapsulate the parameters requested by the client in this class.
(5).AsyncHttpClient: Class requested by asynchronous client.
(6).SyncHttpClient: The class that synchronizes the client request. A subclass of AsyncHttpClient.
Note: The HttpUtil class mainly lists the commonly used get methods. Just call this class where you want to use. Need to add the android-async-http-1.4. file package.
The code is as follows:
(1).
package ; import ; import ; import ; import ; import ; import ; import ; /** * Asynchronous request server * * @author scd * */ public class AsyncJsonUtilGet { private static final String URL = ""; private Context mContext; /** * Constructing method * * @param mContext */ public AsyncJsonUtilGet(Context mContext) { super(); = mContext; } /** * Email registration */ public void emailRegister(String email, String password, String username) { RequestParams params = new RequestParams(); JSONObject jsonObject = new JSONObject(); try { ("email", email); ("password", password); ("username", username); } catch (JSONException e) { (); } ("jsonObject", jsonObject); // get request method (URL, params, new JsonHttpResponseHandler() { @Override public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) { (statusCode, headers, throwable, errorResponse); (mContext, "Register failed!", Toast.LENGTH_SHORT) .show(); } @Override public void onSuccess(int statusCode, Header[] headers, JSONObject response) { String errorCode = ("ErrorCode"); // Indicates that the request is successful if (("0")) { (mContext, "Registered successfully", Toast.LENGTH_LONG).show(); // response: The returned data are all in this parameter, and the functions are implemented according to business requirements } else { (statusCode, headers, response); } } }); } }
(2). Tool class: Note: You need to add a jar package
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class HttpUtil { public static final String STATUS_NETWORK = "network_available"; private static AsyncHttpClient client = new AsyncHttpClient(); static { (11000); } public static void get(String urlString, AsyncHttpResponseHandler res) { (urlString, res); } public static void get(String urlString, RequestParams params, AsyncHttpResponseHandler res) { (urlString, params, res); } public static void get(String urlString, JsonHttpResponseHandler res) { (urlString, res); } public static void get(String urlString, RequestParams params, JsonHttpResponseHandler res) { (urlString, params, res); } public static void get(String uString, BinaryHttpResponseHandler bHandler) { (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 (); } } return false; } // Get file name from UrlConnection public static String getFileName(String url) { String fileName = null; boolean isOK = false; try { URL myURL = new URL(url); URLConnection conn = (); if (conn == null) { return null; } Map<String, List<String>> hf = (); if (hf == null) { return null; } Set<String> key = (); if (key == null) { return null; } for (String skey : key) { List<String> values = (skey); for (String value : values) { String result; try { result = value; int location = ("filename"); if (location >= 0) { result = (location + "filename".length()); result = .decode(result, "utf-8"); result = (("\"") + 1, ("\"")); fileName = result .substring(("=") + 1); isOK = true; } } catch (UnsupportedEncodingException e) { (); } } if (isOK) { break; } } } catch (MalformedURLException e) { (); } catch (IOException e) { (); } return fileName; } }
Open Source Home Introduction:http://open./project/app-framework/