Asynchronous HTTP requests in Android--fragment

Source: Internet
Author: User

1. Customizing asynchronous task

    /*** Asynchronous acquisition of real-time data*/     Public classTimerdatataskextendsAsynctask<void, Void, boolean>{@OverrideprotectedBoolean doinbackground (Void ... params) {String GetUrl= Murl + "/realtimedata.ashx?type=data"; URL loginurl=NULL; InputStream in=NULL; HttpURLConnection Conn=NULL; //Initialize listMdatalist =NewArraylist<datalistitem>(); Try{loginurl=NewURL (GETURL); Conn=(HttpURLConnection) loginurl.openconnection (); Conn.setrequestproperty ("Charset", "UTF-8"); if(Conn.getresponsecode () = = 200) { in=NewBufferedinputstream (Conn.getinputstream ()); Scanner Scanner=NewScanner (in). Usedelimiter ("\\a"); String result= Scanner.hasnext ()? Scanner.next (): ""; LOG.E (Mlogtimerdata,result);} conn.disconnect (); } Catch(IOException ex) {LOG.E (getString (R.string.log_e_url_error), ex.tostring ()); }Catch(Jsonexception ex) {LOG.E (getString (R.string.log_e_json_error), ex.tostring ()); }            return true; } @Overrideprotected voidOnPostExecute (FinalBoolean Success) {//Message msg = new Message ();//uihandler.sendmessage (msg);            if(Getactivity ()! =NULL) {getactivity (). Runonuithread (Uirefreshthread); } mtask=NULL; } @Overrideprotected voidoncancelled () {Mtask=NULL; }    }

2. Refresh the listview in the main thread

Runnable Uirefreshthread =NewRunnable () {@Override Public voidrun () {if(Getactivity ()! =NULL) {                //Initialize AdapterMadapter =NewDatalistadapter (Getactivity (), R.layout.fragment_data_list_item, mdatalist); //Set the adapterMlistview.setadapter (Madapter); Mswipelayout.setrefreshing (false); Mlistview.setenabled (true); }        }    };
    New Runnable () {        @Override        publicvoid  run () {            mlistview.setenabled ( false );        }    };

3. Scheduled Tasks

    New Timer ();     New TimerTask () {        @Override        publicvoid  run () {            if( Getactivity ()! =null) {                getactivity (). Runonuithread (Uithread)                ; New Timerdatatask ();                 NULL );            }        }    };

Each time the refresh is, re-new a adapter object, using Notifydatasetchanged (), there will be an irregular bug exception

Asynchronous HTTP requests in Android--fragment

Related Article

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.