https://opendata.cwb.gov.tw/userLogin
點擊獲取API授權碼按鈕,API授權碼會出現於右側
紅框內的URL是我們要注意的重點
//retrofit
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
<uses-permission android:name="android.permission.INTERNET"/>
/**
*
* 本局所屬地面測站每日雨量資料-每日雨量
* https://opendata.cwb.gov.tw/api/v1/rest/datastore/C-B0025-001?Authorization=CWB-F466F13C-84CD-4A6E-81CB-F3ED6C48A773
*
* 月平均-局屬地面測站資料
* https://opendata.cwb.gov.tw/api/v1/rest/datastore/C-B0027-001?Authorization=CWB-F466F13C-84CD-4A6E-81CB-F3ED6C48A773
*
* 氣象測站基本資料-有人氣象測站基本資料
* https://opendata.cwb.gov.tw/api/v1/rest/datastore/C-B0074-001?Authorization=CWB-F466F13C-84CD-4A6E-81CB-F3ED6C48A773
*
* 把重複的URL 設在 baseUrl
* https://opendata.cwb.gov.tw/api/v1/rest/datastore/
*
**/
public class RetrofitManager {
private static RetrofitManager mInstance = new RetrofitManager();
private APIService apiService;
private RetrofitManager() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://opendata.cwb.gov.tw/api/v1/rest/datastore/")
.addConverterFactory(GsonConverterFactory.create())
.build();
apiService = retrofit.create(APIService.class);
}
//向外提供 RetrofitManager
public static RetrofitManager getInstance() {
return mInstance;
}
//向外提供 APIService
public APIService getAPI() {
return apiService;
}
}
/**
*
* 把重複的URL 設在 baseUrl
* https://opendata.cwb.gov.tw/api/v1/rest/datastore/
*
* 不重複的URL 設在 @GET 或 @POST 後方
* @GET("C-B0025-001?")
*
**/
public interface APIService {
@GET("C-B0025-001?")
Call<DataResponse> getDailyRainfall(@Query("Authorization") String Authorization);
}
這邊的DataResponse如果是紅色請不要緊張,因為我們還沒建立這個檔案。
public class APIAuthorize {
//這邊請替換為申請的授權碼
private final String Authorization = "CWB-F466F13C-84CD-4A6E-81CB-F3ED6C48A773";
//向外提供授權碼
public String getAuthorization() {
return Authorization;
}
}
根據中央氣象局開放資料平臺Execute後的結果,請找到Response body如下:
這邊建立以下幾個檔案,來做為接收API的格式:
/**
* 中括號要用List,大括號則用Class
**/
public class DataResponse {
@SerializedName("records")
private Records records;
public DataResponse(Records records) {
this.records = records;
}
public Records getRecords() {
return records;
}
public void setRecords(Records records) {
this.records = records;
}
}
public class Records {
private List<Location> location = new ArrayList<>();
//Records中包了Location
//這邊使用List的原因是因為被中括號包起來
public Records(List<Location> location){
this.location = location;
}
public List<Location> getLocation() {
return location;
}
public void setLocation(List<Location> location) {
this.location = location;
}
}
public class Location {
private Station station;
private StationObsTimes stationObsTimes;
//Location中包了Station與StationObsTimes
public Location(Station station, StationObsTimes stationObsTimes) {
this.station = station;
this.stationObsTimes = stationObsTimes;
}
public Station getStation() {
return station;
}
public StationObsTimes getStationObsTimes() {
return stationObsTimes;
}
}
public class Station {
private String stationID;
private String stationName;
private String stationNameEN;
private String stationAttribute;
public Station(String stationID, String stationName, String stationNameEN, String stationAttribute) {
this.stationID = stationID;
this.stationName = stationName;
this.stationNameEN = stationNameEN;
this.stationAttribute = stationAttribute;
}
public String getStationID() {
return stationID;
}
public String getStationName() {
return stationName;
}
public String getStationNameEN() {
return stationNameEN;
}
public String getStationAttribute() {
return stationAttribute;
}
}
public class StationObsTimes {
private List<StationObsTime> stationObsTime;
//StationObsTimes中包了StationObsTime
//這邊使用List的原因是因為被中括號包起來
public StationObsTimes(List<StationObsTime> stationObsTime) {
this.stationObsTime = stationObsTime;
}
public List<StationObsTime> getStationObsTime() {
return stationObsTime;
}
}
public class StationObsTime {
private String dataDate;
private WeatherElements weatherElements;
public StationObsTime(String dataDate,WeatherElements weatherElements) {
this.dataDate = dataDate;
this.weatherElements = weatherElements;
}
public String getDataDate() {
return dataDate;
}
public WeatherElements getWeatherElements() {
return weatherElements;
}
}
public class WeatherElements {
private String precipitation;
public WeatherElements(String precipitation) {
this.precipitation = precipitation;
}
public String getPrecipitation() {
return precipitation;
}
}
public class MainActivity extends AppCompatActivity {
APIService apiService;
APIAuthorize apiAuthorize = new APIAuthorize();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
apiService = RetrofitManager.getInstance().getAPI();
Call<DataResponse> call = apiService.getDailyRainfall(apiAuthorize.getAuthorization());
//連線API,獲取資料
call.enqueue(new Callback<DataResponse>() {
@Override
public void onResponse(Call<DataResponse> call, Response<DataResponse> response) {
// 連線成功
// 取得回傳資料
Log.d("TESTTEST","precipitation:" + response.body().getRecords()
.getLocation()
.get(0).getStationObsTimes()
.getStationObsTime()
.get(0).getWeatherElements()
.getPrecipitation());
}
@Override
public void onFailure(Call<DataResponse> call, Throwable t) {
// 連線失敗
Log.e("test",t.toString());
}
});
}
}
結果如下: