π Android μμ Retrofit μ μ¬μ©νμ¬ api ν΅μ μ ν λ, λνμ μΌλ‘ xml νμΌμ΄λ json νμΌμ μμ²νμ¬ λ°λλ€. μ΄ μ€ xml νμΌμ μμ²νμ¬ λ°λ κ³Όμ μ μμ보μ.
μ΄λ² android νλ‘μ νΈμμ μΌμΆ μΌλͺ° API λ₯Ό μ¬μ©νμ¬ μ€λ λ μ§μ μΌμΆ μκ°κ³Ό μΌλͺ° μκ°μ κ°μ Έμ€λ κ³Όμ μ΄ νμνμλ€.
곡곡λ°μ΄ν° ν¬νΈμμ μΌμΆ, μΌλͺ° μκ°μ μ 곡ν΄μ£Όλ API λ₯Ό λ°κ²¬νμ¬ μ¬μ©νκΈ°λ‘ νμλ€.
곡곡λ°μ΄ν°ν¬νΈ : https://www.data.go.kr/data/15012688/openapi.do
Retrofit μ νμ©νλ©΄ λλΆλΆ Json μΌλ‘ μλ΅μ΄ μ€κΈ° λλ¬Έμ GsonConverter λ₯Ό μ¬μ©νλ€. νμ§λ§ 곡곡λ°μ΄ν°λ μΌλΆ μ€λλ λ°μ΄ν°λ Xml κ²°κ³Όλ§μ μ 곡νλ€.
μΌμΆ μΌλͺ° API λ Xml κ²°κ³Όλ§μ μ 곡νκΈ° λλ¬Έμ xml Converter κ° νμνμλλ°, κ°νΈνκ² μμ£Ό μ¬μ©λλ SimpleXmlConverter κ° deprecated λμ΄ Tikxml λΌμ΄λΈλ¬λ¦¬λ₯Ό μ¬μ©νμλ€.
Tikxml λΌμ΄λΈλ¬λ¦¬λ μ΄λ
Έν
μ΄μ
μ μ¬μ©νμ¬ xml μ νμ±νλ€.
Tikxml λ‘ xml μλ΅μ νμ±νλ κ³Όμ μ μμ보μ.
https://github.com/Tickaroo/tikxml/blob/master/docs/AnnotatingModelClasses.md
Β
<response>
<header>
<resultCode>00</resultCode>
<resultMsg>NORMAL SERVICE.</resultMsg>
</header>
<body>
<items>
<item>
<aste>2141 </aste>
<astm>0334 </astm>
<civile>2023 </civile>
<civilm>0453 </civilm>
<latitude>3734</latitude>
<latitudeNum>37.5666660</latitudeNum>
<location>μμΈ</location>
<locdate>20230717</locdate>
<longitude>12659</longitude>
<longitudeNum>126.9833330</longitudeNum>
<moonrise>0420 </moonrise>
<moonset>1957 </moonset>
<moontransit>1211 </moontransit>
<naute>2100 </naute>
<nautm>0416 </nautm>
<sunrise>0524 </sunrise>
<sunset>1952 </sunset>
<suntransit>123814</suntransit>
</item>
</items>
<numOfRows>10</numOfRows>
<pageNo>1</pageNo>
<totalCount>1</totalCount>
</body>
</response>
public static final String API_SUNSET_BASE_URL = "https://apis.data.go.kr/";
public static final String API_SERVICE_KEY = "μλΉμ€ν€";
implementation 'com.tickaroo.tikxml:annotation:0.8.13'
implementation 'com.tickaroo.tikxml:core:0.8.13'
implementation 'com.tickaroo.tikxml:retrofit-converter:0.8.13'
annotationProcessor 'com.tickaroo.tikxml:processor:0.8.13'
@Xml(name = "response")
public class Sunset {
@Element(name = "header")
Header header;
@Element(name = "body")
Body body;
public Header getHeader() {
return header;
}
public Body getBody() {
return body;
}
}
@Xml(name = "body")
public class Body {
@Element(name = "items")
Items items;
public Items getItems() {
return items;
}
}
@Xml(name = "header")
public class Header {
@PropertyElement(name = "resultCode")
String resultCode;
@PropertyElement(name = "resultMsg")
String resultMsg;
public String getResultCode() {
return resultCode;
}
public String getResultMsg() {
return resultMsg;
}
}
@Xml(name = "items")
public class Items {
@Element(name = "item")
Item item;
public Item getItem() {
return item;
}
}
@Xml(name = "item")
public class Item {
@PropertyElement(name = "sunrise")
String sunrise;
@PropertyElement(name = "sunset")
String sunset;
public String getSunrise() {
return sunrise;
}
public String getSunset() {
return sunset;
}
}
public interface SunsetApi {
@GET("/B090041/openapi/service/RiseSetInfoService/getAreaRiseSetInfo")
Call<Sunset> getSunset(
@Query(value = "serviceKey", encoded = true) String serviceKey,
@Query("locdate") String locdate,
@Query("location") String location
);
}
TikXml parser = new TikXml.Builder().exceptionOnUnreadXml(false).build();
// Retrofit
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_SUNSET_BASE_URL)
.addConverterFactory(TikXmlConverterFactory.create(parser))
.build();
SunsetApi sunsetApi = retrofit.create(SunsetApi.class);
sunsetApi.getSunset(API_SERVICE_KEY, "20230718", "μμΈ")
.enqueue(new Callback<Sunset>() {
@Override
public void onResponse(Call<Sunset> call, Response<Sunset> response) {
if (response.isSuccessful() && response.body() != null) {
String sunrise = response.body().getBody().getItems().getItem().getSunrise();
String sunset = response.body().getBody().getItems().getItem().getSunset();
// μΆκ° λ‘μ§
} else {
Log.d("μ€λ₯", String.valueOf(response.code()));
}
}
@Override
public void onFailure(Call<Sunset> call, Throwable t) {
Log.d("μ€λ₯", t.getMessage());
}
});
μ λ§ μ’μ μ 보 κ°μ¬ν©λλ€!