Android Request network asynchronous loading

Source: Internet
Author: User

/** * Package Proecssdialog dialog box * */public class Loaddialog extends ProgressDialog {private string title = Progress Dialog;p Rivate String Message = "Load data ....";p ublic Loaddialog (context context, int theme) {Super (context, theme);} /** * Create dialog box with default title and content * @param context */public Loaddialog (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,st Ring message) {super (context); if (title! = null) {this.title = title;} if (message! = null) {this.message = message;} InitDialog ();} /** * Initialize the dialog, the default dialog cannot be canceled */public void InitDialog () {settitle (title); setmessage (message); Setprogressstyle ( Progressdialog.style_spinner); setcancelable (false);} /** * Opens the dialog box. Set the callback method, pass the class template that needs to run the business method, the method name and the list of parameters * @param the callback callback method, callback after the dialog box closes, and get the returned data * @param class template running the business method * @param met Hod method name for running business methods * @param params run business method's list */public void execute (Callback callback,class serviceclass,string method,objec T. params) {SUPER.Show (); Serviceaysntask task = new Serviceaysntask (Callback,serviceclass,method); Task.execute (params);} /** * Callback Method interface * */public interface callback{public void GetResult (map map);} /** * Thread class communicating with remote service * @author BDK * Asynctask Asynchronous Task */private class Serviceaysntask extends ASYNCTASK&LT;OBJECT,OBJECT,MAP&G T {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 ( );//Create class Template Object class [] Paramtypes = new Class[params.length];for (int i = 0; i < paramtypes.length; i++) {Paramtypes[i] = Params[i].getclass ();} Based on the class template get method M = Serviceclass.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);}}}

Android Request network asynchronous loading

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.