Android Simple weather Forecast

Source: Internet
Author: User

Interface with the weather interface of Baidu:

Http://api.map.baidu.com/telematics/v3/weather?location= Beijing &output=json&ak=yourkey
Baidu AK Application Address:Http://lbsyun.baidu.com/apiconsole/key

The city is stored with the spinner class, reads the XML file, fills in the spinner. The XML file is in the local Res/raw folder.

and add a listen event to three spinner. When choosing a province, the spinner of the city and the region will change accordingly, and the spinner of the area will change accordingly when the city is selected.

When selecting a province, the list of cities and places will change Sp_province.setonitemselectedlistener (new Onitemselectedlistener () {@Overridepublic void Onitemselected (adapterview<?> parent, View view,int position, long id) {Currentpro = Position;cityadapter = new Arra Yadapter<city> (mainactivity.this,android. R.layout.simple_spinner_item,android. R.id.text1, Provinces.get (position). Getcitys ()); Cityadapter.setdropdownviewresource (Android. R.layout.simple_spinner_dropdown_item); Sp_city.setadapter (Cityadapter);} @Overridepublic void onnothingselected (adapterview<?> parent) {}});

When you select a specific region, create an asynchronous class and take the region name as an argument.

When selecting a place, the specific weather conditions are displayed Sp_district.setonitemselectedlistener (new Onitemselectedlistener () {@Overridepublic void Onitemselected (adapterview<?> parent, View view,int position, long ID) {//selected city district dis = Districtadapter.geti TEM (position);//LOG.I ("I", Dis.getname ()); new Weatherasynctask (). Execute (Dis.getname ());} @Overridepublic void onnothingselected (adapterview<?> parent) {}});


In the Doinbackground method of the asynchronous class, get the weather information from the interface, processing the picture here I did it in Weather_ The data class defines two properties of the bitmap type (these two properties are not used when parsing the JSON string of the weather information obtained from the interface), is fetched from the network, assigns values to the two properties, and then passes the encapsulated weather to the OnPostExecute method

Protected Weather doinbackground (String ... params) {string url = Httputils.geturl (params[0]); String jsonstr = httputils.getjsonstr (URL); Weather Weather = Httputils.fromjson (JSONSTR); Result r = Weather.getresults (). get (0); list<map<string,object>> list = new arraylist<map<string,object>> (); list = Httputils.tolistmap (R); for (int i = 0;i<3;i++) {Weather_data w = r.getweather_data (). get (i); W.setdaypicture ( Httputils.getimage (W.getdaypictureurl ())); W.setnightpicture (Httputils.getimage (W.getnightpictureurl ()));} return weather;}

You can also store weather specific information and images obtained through ImageUrl into a list<map<string,object>>, and then Pass to OnPostExecute method

In this method, the UI thread assigns values to individual controls in the layout.

In the process of getting resources, I defined a single tool class,

Includes methods for parsing XML files, parsing JSON strings, and getting an image.

In the display, just use some of the attributes obtained from the Baidu interface, and not use all. But the general principle should be so.

Mistakes, but also see the people are a lot of forgive and point out.

Code Download:

http://download.csdn.net/detail/worst_hacker/8634955

Android Simple weather Forecast

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.