Weather forecast information acquisition is obtained by using JSON, there are many resources on the net, source code. As the above involves a lot of weather information, including humidity, travel advice, etc., as well as a resource bundle that adds all the city codes. In order to practiced hand understand the principle of JSON. I only get the highest temperature, lowest temperature, city name of the honest city.
My layout is through a button to get the city name, the highest temperature, the lowest temperature. The MAIN.XNL code is as follows
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical " > <textview android:id= "@+id/textview1" android:layout_width= "Wrap_content" Android:layout_h eight= "Wrap_content" android:text= "City Name:"/> <edittext android:id= "@+id/tx_cityname" Android : layout_width= "match_parent" android:layout_height= "Wrap_content" android:ems= "ten" > </EditText> <textview android:id= "@+id/textview2" android:layout_width= "Wrap_content" Android:layout_heigh t= "wrap_content" android:text= "Maximum temperature:"/> <edittext android:id= "@+id/tx_height" android:layou T_width= "Match_parent" android:layout_height= "Wrap_content" android:ems= "ten"/> <textview an Droid:id= "@+id/textview3" anDroid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Minimum temperature:"/> <Edi Ttext android:id= "@+id/tx_low" android:layout_width= "match_parent" android:layout_height= "Wrap_conten T "android:ems=" ten "/> <button android:id=" @+id/button_refresh "android:layout_width=" Wrap_cont Ent "android:layout_height=" wrap_content "android:gravity=" center "android:text=" get real-time weather information "> &L T;/button></linearlayout>
This project contains 2 Java files, respectively, Wheatherlck.java and Wheatherinfo.java files. The Wheatherlck.java code is as follows
Package Com.hiden.weatherdemo;import Java.io.bufferedreader;import Java.io.ioexception;import Java.io.inputstreamreader;import Org.apache.http.httpentity;import Org.apache.http.httpresponse;import Org.apache.http.httpstatus;import Org.apache.http.client.clientprotocolexception;import Org.apache.http.client.httpclient;import Org.apache.http.client.methods.httpget;import Org.apache.http.impl.client.defaulthttpclient;import Org.apache.http.util.entityutils;import Org.json.jsonexception;import Org.json.jsonobject;import Android.os.bundle;import Android.os.Handler;import Android.os.message;import Android.app.activity;import Android.content.intent;import Android.util.Log;import Android.view.keyevent;import Android.view.menu;import Android.view.view;import Android.widget.Button;import Android.widget.edittext;import Android.widget.imageview;import Android.widget.textview;public class WeatherLck Extends Activity {//Create three Global text box object EditText tx_cityname,tx_height,tx_low;private String city_str= "Chengdu";Button Button_refresh; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (r.layout.main);//Get text box in Layout tx_cityname= (EditText) This.findviewbyid (R.id.tx_cityname); Tx_cityname.settext (CITY_STR); tx_height= (EditText) This.findviewbyid (r.id.tx_height); tx_low= (EditText) This.findviewbyid (R.id.tx_low); button_refresh= (Button) Findviewbyid (R.id.button_refresh); Button_refresh.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View v) {//TODO Auto-generated method Stubif (V==button_refresh) {refresh (CITY_STR);}}); * Get HTTP// m.weather.com.cn/data/101091106.html above Data * 101091106 is the city code, above I have changed the city code, removed the empty line, the format of UTF-8 * Each time just run a thread, and then add the main thread recycling * @param city_str * @throws jsonexception * @throws Exception * @throws clientprotocolexception */private Thread thread;p Rivate Handler Handler = new Handler () {@Overridepublic void Handlemessage (Message msg) {switch (msg.what) {case 1:jsonobject weather = (jsonobject) msg.obj; Refreshui (weather); try {thread.join ();} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} break;}}};/ * Cannot request the network * thread in the main thread no while loop is only executed once, If there is an overflow may not send message * @param city_str */private void Requestweather (final String city_str) {thread = new thread (new Runnable () {@O verridepublic void Run () {//TODO auto-generated method stubstring url= "http://www.weather.com.cn/data/cityinfo/ 101270101.html "; HttpClient client = new Defaulthttpclient (); HttpGet httpget = new HttpGet (URL); HttpResponse response; String Sbuf = null;try {response = Client.execute (HttpGet); Httpentity httpentity = response.getentity (), if (httpentity! = null) {BufferedReader BR = new BufferedReader (new InputStreamReader (Httpentity.getcontent (), "Utf-8")); sbuf = Br.readline ();} Jsonobject object = new Jsonobject (SBUF); JsonobjeCT data = (jsonobject) object.getjsonobject ("Weatherinfo");//log.i (TAG, data.tostring ()); Message msg = new Message (); msg.what = 1;msg.obj = Data;handler.sendmessage (msg);} catch (Clientprotocolexception e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//TODO Auto-generated catch Blocke.printstacktrace ();} catch (Jsonexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}); Thread.Start ();} protected void Refresh (String city_str) {requestweather (CITY_STR);} private void Refreshui (Jsonobject jsonobject) {Jsonobject jsondata = Jsonobject;try{tx_cityname.settext ( Jsondata.getstring ("City"));//obtain High temperature data Tx_height.settext (jsondata.getstring ("Temp1"));//obtain cryogenic data Tx_low.settext ( Jsondata.getstring ("Temp2"));} catch (Exception e) {e.printstacktrace ();}}}
The Wheatherinfo.java code is as follows:
Package Com.hiden.weatherdemo.ui;public class Weatherinfo {String city = ""; String Temp1 = ""; String temp2 = "";//Starling Wind Central public String getcity () {return city;} public void Setcity (String city) {this.city = city;} @Overridepublic String toString () {return "Weatherinfo [city=" + City + ", temp1=" + temp1+ ", temp2=" + Temp2 + "]";} Public String GetTemp1 () {return temp1;} public void SetTemp1 (String temp) {this.temp1 = temp;} Public String getTemp2 () {return temp2;} public void setTemp2 (String temp) {this.temp2 = temp;}}
Click the button to get the highest temperature and lowest temperature to Chengdu, as follows:
Of course, we can make more detailed according to their own needs, such as adding the next 5 days of weather, positioning, add attention to the city and other functions. More online resources, we can practice more.