Android simple weather forecast and android Weather Forecast

Source: Internet
Author: User

Android simple weather forecast and android Weather Forecast

The interface uses Baidu's weather interface:

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 in the spinner class, the xml file is read, and the file is filled in the spinner. Xml files are stored in the local res/raw folder.

And add listening events to the three slaves. When you select a province, the spinner of the storage city and region changes accordingly. Similarly, when you select a city, the region's spinner changes accordingly.

// When selecting a province, the city and local lists change to sp_province.setOnItemSelectedListener (new OnItemSelectedListener () {@ Overridepublic void onItemSelected (AdapterView <?> Parent, View view, int position, long id) {currentPro = position; cityAdapter = new ArrayAdapter <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 region, create an asynchronous class and use the region name as the parameter.

// When selecting a location, display the specific weather condition sp_district.setOnItemSelectedListener (new OnItemSelectedListener () {@ Overridepublic void onItemSelected (AdapterView <?> Parent, View view, int position, long id) {// select the city District dis = districtAdapter. getItem (position); // Log. I ("I", dis. getName (); new weatherasynctask(cmd.exe cute (dis. getName () ;}@ Overridepublic void onNothingSelected (AdapterView <?> Parent ){}});


 

In the asynchronous doInBackground method, obtain the weather information from the interface, and process the image here, two Bitmap attributes are defined in the Weather_data class (these two attributes are never used to parse the json string of the weather information obtained from the interface ), assign values to these two attributes after obtaining them from the network, and then pass 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 the specific Weather information and images obtained through imageUrl in a list <Map <String, Object>, and then pass it to the onPostExecute method.

In this method, the UI thread assigns values to each control in the layout.

In the process of obtaining resources, I defined a tool class separately,

Including parsing xml files, parsing json strings, and obtaining images.

In the display, only some properties obtained from the Baidu interface are used, and not all of them are used. But the general principle should be like this.

For more information about the errors, please forgive me and point out.

Download Code:

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

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.