Implement a simple weather forecast application (9)

Source: Internet
Author: User

9. Cancel the use of the volley library instead of the basic httpurlconnection

Use Asynctask to replace with volley library, use HttpURLConnection to load data

The places to change are Updateservice and mainactivity:

1 Private voidUpdate () {2LOG.D (TAG, "Start in update Service");3         if(!configure.exists ()) {4 Toast.maketext (Getapplicationcontext (), r.string.fetching, Toast.length_short). Show ();5             Newfetchasynctask (). Execute (URL);6}Else{7             Try{8Hasnew =!(Datetools.getdate (). Equals (Filetools.loadconf (Getapplicationcontext () )));9}Catch(IOException e) {Ten e.printstacktrace (); One             } A             if(hasnew) { - Toast.maketext (Getapplicationcontext (), R.string.fetching,toast.length_short). Show (); -                 Newfetchasynctask (). Execute (URL); the             } -         } -     } -  +     Private classFetchasynctaskextendsAsynctask<string,void,string> { -  + @Override A         protectedstring Doinbackground (String ... params) { at BufferedReader Reader; -StringBuilder Builder =NewStringBuilder (); -             Try{ -URL url=NewURL (params[0]); -HttpURLConnection connection=(HttpURLConnection) url.openconnection (); -InputStream in=Connection.getinputstream (); inReader=NewBufferedReader (NewInputStreamReader (in)); - String Line; to                  while(line = Reader.readline ())! =NULL){ + Builder.append (line); -                 } the}Catch(IOException e) { * e.printstacktrace (); $             }Panax Notoginseng             returnbuilder.tostring (); -         } the  + @Override A         protected voidOnPostExecute (String s) { the LOG.D (TAG, s); +             Try{ -Arraylist<weatheritem> Weatheritems =Parsetools.getinstance (S, parsetools.request_raw); $  $                 //saving data to a file - Filetools.savedata (Getapplicationcontext (), weatheritems); -Toast.maketext (Getapplicationcontext (), "Update Complete", Toast.length_short). Show (); the  -Filetools.savewidgetdata (Getapplicationcontext (), Weatheritems.get (0));Wuyi  theIntent intent=NewIntent (Getapplicationcontext (), Updateservice.class); - StartService (intent); Wu}Catch(IOException |jsonexception e) { - e.printstacktrace (); About             } $             Super. OnPostExecute (s); -         } -}

And:

1 Private voidUpdate () {2LOG.D (TAG, "Start in the main UI");3         //only when the first load of data has not been downloaded to complete the madapter empty case4         if(Madapter = =NULL){5Madapter =NewItemadapter (mweatheritems);6 Listview.setadapter (madapter);7         }8 9         if(!configure.exists ()) {Ten Toast.maketext (Getapplicationcontext (), R.string.fetching,toast.length_short). Show (); One             Newfetchasynctask (). Execute (URL); A}Else{ -             Try{ -Hasnew =!(Datetools.getdate (). Equals (Filetools.loadconf (Getapplicationcontext () ))); the}Catch(IOException e) { - e.printstacktrace (); -             } -             if(hasnew) { + Toast.maketext (Getapplicationcontext (), R.string.fetching,toast.length_short). Show (); -                 Newfetchasynctask (). Execute (URL); +}Else{ A                 Try{ atmweatheritems=Filetools.loaddata (Getapplicationcontext ()); -Madapter =NewItemadapter (mweatheritems); - Listview.setadapter (madapter); -}Catch(IOException |jsonexception e) { - e.printstacktrace (); -                 } in             } -         } to     } +  -     Private classFetchasynctaskextendsAsynctask<string,void,string>{ the  * @Override $         protectedstring Doinbackground (String ... params) {Panax Notoginseng BufferedReader Reader; -StringBuilder Builder =NewStringBuilder (); the             Try{ +URL url=NewURL (params[0]); AHttpURLConnection connection=(HttpURLConnection) url.openconnection (); theInputStream in=Connection.getinputstream (); +Reader=NewBufferedReader (NewInputStreamReader (in)); - String Line; $                  while(line = Reader.readline ())! =NULL){ $ Builder.append (line); -                 } -}Catch(IOException e) { the e.printstacktrace (); -             }Wuyi             returnbuilder.tostring (); the         } -  Wu @Override -         protected voidOnPostExecute (String s) { About log.d (tag,s); $             Try{ -Mweatheritems =parsetools.getinstance (s,parsetools.request_raw); -  -                 //saving data to a file A Filetools.savedata (Getapplicationcontext (), mweatheritems); +Toast.maketext (Getapplicationcontext (), "Update Complete", Toast.length_short). Show (); the  -Madapter=NewItemadapter (mweatheritems); $ Listview.setadapter (madapter); the  theFiletools.savewidgetdata (Getapplicationcontext (), Mweatheritems.get (0)); the  theIntent intent=NewIntent (Getapplicationcontext (), Updateservice.class); - StartService (intent); in}Catch(IOException |jsonexception e) { the e.printstacktrace (); the             } About             Super. OnPostExecute (s); the         } the}

Because if we volley this library method, we get the same string, so instead of converting from jsonobject to a string it's better to just load the string.

It was previously discovered that an exception occurred when date.conf was changed during the run (simulated date change):

Java.lang.RuntimeException:Handler (Android.os.Handler) {415A25E0} sending message to a Handler on a dead thread

The next stage is to consider how to solve the problem.

Implement a simple weather forecast application (9)

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.