Android Data loading and JSON parsing--original version

Source: Internet
Author: User

1. Create an entity class to download data from

class MyData    {        String imagepath;        String title;        desc;        publicdesc)        {            super();            this.imagepath = imagepath;            this.title = title;            this.descdesc;        }    }

2. Asynctask data loading and JSON parsing class

   class fileasynctask extends asynctask<String, Void, String> {@OverrideprotectedString Doinbackground (String ... params) {InputStream is =NULL; Bytearraybuffer Bytearraybuffer =NewBytearraybuffer (4000);Try{URL url =NewURL (params[0]); URLConnection openconnection = Url.openconnection ();//Set the time when the connection timed outOpenconnection.setconnecttimeout ( the);//Data read time-outOpenconnection.setreadtimeout ( the); is = Openconnection.getinputstream ();byte[] buffer =New byte[1024x768];intLen while(-1! = (len = is.read (buffer))) {Bytearraybuffer.append (buffer,0, Len); }            }Catch(Malformedurlexception e)            {E.printstacktrace (); }Catch(IOException e) {//TODO auto-generated catch blockRunonuithread (NewRunnable () { Public voidRun () {//Set the control when loading the exception, usually showing the progress bar}                   });            E.printstacktrace (); }finally{if(Is! =NULL)                {Try{Is.close (); }Catch(IOException e) {//TODO auto-generated catch blockE.printstacktrace (); }}} String res =NewString (Bytearraybuffer.tobytearray (),0, Bytearraybuffer.length ());returnRes }//Parse Jsonarray data@Overrideprotected voidOnPostExecute (String result) {Try{Jsonarray Jsonarray =NewJsonarray (result); for(inti =0; I < jsonarray.length ();                    i++) {Jsonobject jsonobject = Jsonarray.getjsonobject (i); String ImagePath = jsonobject.getstring ("ImagePath"); String title = Jsonobject.getstring ("title"); Stringdesc= Jsonobject.getstring ("desc"); MyData MyData =NewMyData (ImagePath, title,desc); Datalist.add (MyData);//Add Data} myadapter.notifydatasetchanged ();//Notification adapter data changes}Catch(Jsonexception e) {//TODO auto-generated catch blockE.printstacktrace (); }Super. OnPostExecute (Result); }    }

3. Call

privatenew ArrayList<MainActivity.MyData>();//声明全局变量dataList new FileAsyncTask();        fileAsyncTask.execute("http://192.168.56.1:8080/service.txt");

Android Data loading and JSON parsing--original version

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.