HTTP framework: Implementation of asynchronous access to the network;
How to use:
(1) Create Httputil
Public class httputil {private static asynchttpclient client = new asynchttpclient (); // Instance Session object Static {client.settimeout (11000); // set link timeout, if not set, The default is 10s}/** * url return string * with no parameters @param urlString url * @param res Processing Handler */public static void get (string urlstring, Asynchttpresponsehandler res) { client.get (urlstring, res);} /** * url with parameters return string * @param urlString url * @param params Parameters in the URL * @param res processing handler */public static void get (string Urlstring, requestparams params,asynchttpresponsehandler res) { // url with parameter Client.get ( Urlstring, params, res);} /** * return json * @param urlString url * @param res Handling Handler */public static vOid get (string urlstring, jsonhttpresponsehandler res) { // without parameters, Gets the JSON object or array client.get (urlstring, res);} /** * with parameters, return json * @param urlString url * @param params URL parameters * @param res processing handler */public static void get (string Urlstring, requestparams params,jsonhttpresponsehandler res) { // with parameters, Gets the JSON object or array client.get (urlstring, params, res);} /** * with no parameters, returns byte data * @param uString url * @param bHandler Handling Handler */public static void get (String ustring, binaryhttpresponsehandler bhandler) { // Download data is used, it returns a byte data Client.get (Ustring, bhandler);} Public static asynchttpclient getclient () {return client;}}
(2) use in activity
URL gets string url = getstring (r.string.url);//Initialize network request parameters requestparams params = New requestparams ();p arams.put (username, username);p arams.put (Password, password);//Network request, Processing return Data Httputil.get (Myconsts.url_login, params, new jsonhttpresponsehandler () {//Network request succeeded public void onsuccess (Int statuscode, header[] headers,jsonobject response) {// Successful acquisition of network data, where processing of data returned by the network}//server request failed Public void onfailure (int statuscode, header[] headers , string responsestring, throwable throwable) { //Visit network failed}//request end, regardless of success or failure to call the method public void onfinish () { //executes the method, whether successful or not});
Special Note: The OnFinish method is executed before the onsucess () and OnFailure () methods are executed;
jar--android-async-http-1.4.5