JSON之三:擷取JSON文本並解釋(以google的天氣API為例)

來源:互聯網
上載者:User

標籤:android   des   style   blog   class   code   


google提供了天氣的api,以廣州天氣為例,地址為:

http://api.openweathermap.org/data/2.5/weather?q=guangzhou

返回的結果為:

{    "coord": {        "lon": 113.25,        "lat": 23.12    },    "sys": {        "message": 0.2088,        "country": "CN",        "sunrise": 1400017567,        "sunset": 1400065233    },    "weather": [        {            "id": 501,            "main": "Rain",            "description": "moderate rain",            "icon": "10d"        }    ],    "base": "cmc stations",    "main": {        "temp": 299.818,        "temp_min": 299.818,        "temp_max": 299.818,        "pressure": 1004.54,        "sea_level": 1014.72,        "grnd_level": 1004.54,        "humidity": 97    },    "wind": {        "speed": 4.42,        "deg": 201.501    },    "rain": {        "3h": 6    },    "clouds": {        "all": 44    },    "dt": 1400055192,    "id": 1809858,    "name": "Guangzhou",    "cod": 200}

因此,在本範例中,寫一個天氣查詢的DEMO,用於輸入地點,並查詢天氣情況。

效果如下:



詳細步驟如下:

1、主介面布局檔案

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   xmlns:tools="http://schemas.android.com/tools"   android:layout_width="match_parent"   android:layout_height="match_parent"   android:paddingBottom="@dimen/activity_vertical_margin"   android:paddingLeft="@dimen/activity_horizontal_margin"   android:paddingRight="@dimen/activity_horizontal_margin"   android:paddingTop="@dimen/activity_vertical_margin"   tools:context=".MainActivity" >   <TextView      android:id="@+id/textView1"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_alignParentLeft="true"      android:layout_alignParentTop="true"      android:layout_marginTop="15dp"      android:text="@string/location"      android:textAppearance="?android:attr/textAppearanceMedium" />   <EditText      android:id="@+id/editText1"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_alignBottom="@+id/textView1"      android:layout_alignParentRight="true"      android:ems="10" />   <TextView      android:id="@+id/textView2"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_alignLeft="@+id/textView1"      android:layout_below="@+id/textView1"      android:layout_marginTop="68dp"      android:text="@string/country"      android:textAppearance="?android:attr/textAppearanceSmall" />   <TextView      android:id="@+id/textView3"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_below="@+id/textView2"      android:layout_marginTop="19dp"      android:text="@string/temperature"      android:textAppearance="?android:attr/textAppearanceSmall" />   <TextView      android:id="@+id/textView4"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_alignLeft="@+id/textView3"      android:layout_below="@+id/textView3"      android:layout_marginTop="32dp"      android:text="@string/humidity"      android:textAppearance="?android:attr/textAppearanceSmall" />   <TextView      android:id="@+id/textView5"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_alignLeft="@+id/textView4"      android:layout_below="@+id/textView4"      android:layout_marginTop="21dp"      android:text="@string/pressure"      android:textAppearance="?android:attr/textAppearanceSmall" />   <EditText      android:id="@+id/editText2"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_above="@+id/textView3"      android:layout_toRightOf="@+id/textView3"      android:ems="10" >      <requestFocus />   </EditText>   <EditText      android:id="@+id/editText3"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_alignBaseline="@+id/textView3"      android:layout_alignBottom="@+id/textView3"      android:layout_alignLeft="@+id/editText2"      android:ems="10" />   <EditText      android:id="@+id/editText4"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_above="@+id/textView5"      android:layout_alignLeft="@+id/editText1"      android:ems="10" />   <EditText      android:id="@+id/editText5"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_alignBaseline="@+id/textView5"      android:layout_alignBottom="@+id/textView5"      android:layout_alignRight="@+id/editText4"      android:ems="10" />   <Button      android:id="@+id/button1"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_alignLeft="@+id/editText2"      android:layout_below="@+id/editText1"      android:onClick="open"      android:text="@string/weather" /></RelativeLayout>

2、定義String.xml

<?xml version="1.0" encoding="utf-8"?><resources>   <string name="app_name">JSONParser</string>   <string name="action_settings">Settings</string>   <string name="hello_world">Hello world!</string>   <string name="location">Location</string>   <string name="country">Country:</string>   <string name="temperature">Temperature:</string>   <string name="humidity">Humidity:</string>   <string name="pressure">Pressure:</string>   <string name="weather">Weather</string></resources>

3、在AndroidManifest.xml中添加internet存取權限。


4、

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.