今天主要跟大家分享一下如何从API中获取数据,顺便写了一个小的例子,天气情况的获取。数据来源是聚合数据,网址给大家,可以去看看~
聚合数据 https://www.juhe.cn/
天气预报数据信息 https://www.juhe.cn/docs/api/id/39
界面啥的,直接看主页面好了,主要是这样的:
多的也不说了,直接上代码好了,希望大家可以在这种粗暴的方式中,慢慢的享受~~~
首先,界面的xml,其实这个没什么好说的,但是可以看看空间的名字啥的,还有就是我的界面写的忒丑,需要大家的鞭策啊~~~
界面:
<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"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/ll_main_container"
android:layout_width="match_parent"
android:layout_height="280dp"
android:background="@drawable/sun"
android:orientation="vertical"
android:padding="12dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/ib_city"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/position" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<TextView
android:id="@+id/tv_city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingTop="20dp"
android:text="北京"
android:textColor="#ffffff"
android:textSize="20dp" />
<TextView
android:id="@+id/tv_temperature"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingTop="22dp"
android:text="11度"
android:textColor="#ffffff"
android:textSize="28dp" />
<TextView
android:id="@+id/tv_weather"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingTop="12dp"
android:text="小雨"
android:textColor="#ffffff"
android:textSize="15dp" />
<TextView
android:id="@+id/tv_wind"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingBottom="32dp"
android:paddingTop="12dp"
android:text="东北风 2级"
android:textColor="#ffffff"
android:textSize="15dp" />
<TextView
android:id="@+id/tv_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:paddingBottom="2dp"
android:paddingTop="18dp"
android:text="2015年10月1日星期一"
android:textColor="#ffffff"
android:textSize="12dp" />
</LinearLayout>
<GridView
android:id="@+id/gv_forweather"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_below="@+id/ll_main_container"
android:numColumns="5"
android:paddingBottom="10dp"
android:paddingTop="10dp" >
</GridView>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="@drawable/b"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/ib_life"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/life" />
</LinearLayout>
<TextView
android:id="@+id/tv_net"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/linearLayout1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="47dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="20dp"
android:textColor="#cccccc"
android:textSize="15dp" />
</RelativeLayout>
实体类的设计,主要是保存天气的信息,具体的设计要根据自己获取到的数据内容来进行设计,这里我读出了获取的所有信息(json解析),只是为了给大家做一个例子,实际的应用中,需要什么数据,解析什么数据即可,不必逐一的将其解析。
实体类:
package cn.yurui.weather.entity;
import java.util.ArrayList;
public class Weather {
/**
* 返回码
*/
private int code;
/**
* 返回说明
*/
private String reason;
/**
* 当前城市
*/
private String city_name;
/**
* 当前时间
*/
private String date;
private String week;
private String moon;
/**
* 更