Android requests Asynchronous Network Loading

Source: Internet
Author: User

/*** Encapsulate the ProecssDialog dialog box **/public class LoadDialog extends ProgressDialog {private String title = ""; private String message = "loading data .... "; public LoadDialog (Context context, int theme) {super (context, theme );} /*** use the default title and content to create the dialog box * @ param context */public LoadDialog (Context context) {super (context); initDialog ();} /*** create a dialog box with the specified title and content * @ param context * @ param title * @ param message */public LoadDialog (Context context, String title, String message) {super (context); if (title! = Null) {this. title = title;} if (message! = Null) {this. message = message;} initDialog ();}/*** parameters in the initialization dialog box. The default dialog box cannot cancel */public void initDialog () {setTitle (title ); setMessage (message); setProgressStyle (ProgressDialog. STYLE_SPINNER); setCancelable (false);}/*** open the dialog box, set the callback method, and pass the class template, method name, and parameter list for the business method to be executed. * @ param callback method, this method is called back after the dialog box is closed, obtain the returned data * @ param serviceClass class template for executing the Business method * @ param method the method name for executing the Business method * @ param params parameter list for executing the Business method */public void execute (callback callback, class serviceClass, String method, Object... params) {super. show (); ServiceAysnTask task = new serviceaysntask(callback,serviceclass,method=task.exe cute (params);}/*** Callback method interface **/public interface Callback {public void getResult (Map map );} /*** Thread class for communication with remote services * @ author BDK * AsyncTask asynchronous task */private class ServiceAysnTask extends AsyncTask
 
  
{Private Class serviceClass; private String method; private Callback callback; public ServiceAysnTask (Callback callback, Class serviceClass, String method) {this. callback = callback; this. serviceClass = serviceClass; this. method = method ;}@ Overrideprotected Map doInBackground (Object... params) {Map resultMap = null; try {Object obj = serviceClass. newInstance (); // creates a Class template object Class [] paramTypes = new Class [params. length]; for (int I = 0; I <paramTypes. length; I ++) {paramTypes [I] = params [I]. getClass () ;}// obtain Method m = serviceClass based on the class template. getMethod (method, paramTypes); resultMap = (Map) m. invoke (obj, params);} catch (Exception e) {e. printStackTrace ();} LoadDialog. this. cancel (); return resultMap;} @ Overrideprotected void onPostExecute (Map result) {super. onPostExecute (result); if (result = null) {Toast. makeText (LoadDialog. this. getContext (), "Network Communication exception", Toast. LENGTH_LONG ). show (); return;} callback. getResult (result );}}}
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.