Android Simple package like jquery asynchronous request

Source: Internet
Author: User

in Android development often use asynchronous request data, usually use handler or Asynctask to do, handler with the message used to be more troublesome, asynctask thread pool only allow 128 threads to work, there will be overflow problem, (Of course there are not so many threads working at the same time) so write this code, also look high finger positive! [Java] Code01 PackageCom.xbl.task;02 03ImportJava.io.BufferedReader;04ImportJava.io.InputStream;05ImportJava.io.InputStreamReader;06ImportJava.net.URL;07ImportJava.util.concurrent.Executor;08ImportJava.util.concurrent.ExecutorService;09Importjava.util.concurrent.Executors;10 11ImportAndroid.os.Bundle;12ImportAndroid.os.Handler;13ImportAndroid.os.Message;14ImportAndroid.util.Log;15 16 17 Public classJQuery {18 19Private Static FinalString key = "Key";20Private StaticString TAG = "JQuery";21stPrivate Static Final intN = 5;//5 Workers22Private Static FinalExecutor worker = Executors.newfixedthreadpool (N);//thread Pool23Private Static FinalHandler Handler =NewHandler () {24@Override25 Public voidhandlemessage (Message msg) {Tasklistener listener =(Tasklistener) msg.obj;Object result =Msg.getdata ();28if(Listener! =NULL ){29listener.callback (result);30                }31destory ();32            }33        };34 35PrivateJQuery () {36        }37 38 Public Static voidExecuteFinalString URL,FinalTasklistener Listener) {Worker.execute (NewRunnable () {40@Override41 Public voidrun () {The String result =NULL;43Try {result =webutil.get (URL);45}Catch(Exception e) {46log.e (TAG, E.getmessage ());47                    }The Message msg =NewMessage ();Bundle data =NewBundle ();51data.putserializable (KEY, result);52msg.setdata (data);Msg.obj =Listener;54handler.sendmessage (msg);55                }56            });57        }58 59//Destroying Threads60Private Static voiddestory () {61if(Worker! =NULL&& workerinstanceofExecutorservice) {Executorservice es =(executorservice) worker;63if(!Es.isshutdown ()) {64Es.shutdown ();65                }66            }67        }  68 69 Public Interfacetasklistener{70 Public voidcallBack (Object result);71        }72/**73 *74 * Don't be too concerned with the code here, just an example of *76*/77 Public Static classwebutil{78 79 Public StaticString get (string url)throwsexception{InputStream InputStream =Newurl (url). OpenStream ();Bayi StringBuilder Strbuilder =NewStringBuilder ();BufferedReader reader =NewBufferedReader (Newinputstreamreader (InputStream));83 for(String s = reader.readline (); s! =NULL; s =reader.readline ())84Strbuilder.append (s);85 86inputstream.close ();87returnstrbuilder.tostring ();88            }89        }90}[Code] [Java] Code01//Call Method:Jquery.execute ("Http://weather.news.qq.com/inc/minisite_125.js", Newjquery.tasklistener () {03@Override04 Public voidcallBack (Object result) {TextView T =NewTextView (taskdemoactivity. This);06T.settext (string.valueof (Result));LinearLayout layout =(LinearLayout) Findviewbyid (R.ID.LAYOUT1);08Layout.addview (t);09                    }10});

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.