Android Retrofit xml

passΒ·2023λ…„ 7μ›” 18일
0

Android

λͺ©λ‘ 보기
18/41

πŸ”₯ Android Retrofit 을 μ‚¬μš©ν•˜μ—¬ xml 파일 νŒŒμ‹±ν•˜κΈ°

🎈 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 응닡을 νŒŒμ‹±ν•˜λŠ” 과정을 μ•Œμ•„λ³΄μž.


μ°Έκ³  : Tikxml μ‚¬μš©λ²• 곡식 λ¬Έμ„œ

https://github.com/Tickaroo/tikxml/blob/master/docs/AnnotatingModelClasses.md



πŸ”₯ μ‚¬μš© ν™˜κ²½



Β 

βœ” xml ꡬ쑰

<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 = "μ„œλΉ„μŠ€ν‚€";
  • μ„œλΉ„μŠ€ν‚€λŠ” 보이지 μ•Šκ²Œ λ”°λ‘œ 관리해주어야 ν•˜μ§€λ§Œ, 이번 μ˜ˆμ œμ—μ„œλŠ” λ„˜μ–΄κ°€λ„λ‘ ν•˜κ² λ‹€.



βœ” Gradle

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'
  • java 이기 λ•Œλ¬Έμ— annotationProcessor λ₯Ό μ‚¬μš©ν•˜μ˜€κ³ , kotlin일 경우 kapt λ₯Ό μ‚¬μš©ν•˜λ©΄ λœλ‹€.



βœ” data

@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;
    }
}
  • java λŠ” public class λ₯Ό ν•˜λ‚˜μ˜ java νŒŒμΌμ— 담을 수 μ—†μ–΄ 각각의 νŒŒμΌμ„ λ§Œλ“€μ–΄ μ£Όμ—ˆλ‹€.
  • kotlin 의 경우 public 을 μ§€μš°κ³ , ν•˜λ‚˜μ˜ java νŒŒμΌμ— 넣을 수 μžˆλ‹€.
  • xml νƒœκ·Έμ— μžˆλŠ” λ‚΄μš©λ“€μ„ λ‹΄κΈ° μœ„ν•΄ 각각의 νƒœκ·Έλ§ˆλ‹€ class λ₯Ό λ§Œλ“€μ–΄ μ£Όμ—ˆλ‹€.
  • Item μ—μ„œ ν•„μš”ν•œ λ‚΄μš©λ§Œ λ³€μˆ˜λ‘œ λ§Œλ“€μ–΄ μ£Όμ—ˆλ‹€.
  • @PropertyElement : 더 이상 μžμ‹ νƒœκ·Έκ°€ μ—†λŠ” λ³€μˆ˜
  • @Element : μžμ‹ νƒœκ·Έκ°€ μžˆλŠ” λ³€μˆ˜
  • @xml(name = "νƒœκ·Έμ΄λ¦„")



βœ” API Service Interface

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
    );
}
  • Get ν˜•μ‹μœΌλ‘œ μš”μ²­
  • μš”μ²­ 인자둜 "serviceKey"와 "locdate", "location" 을 λ„˜κ²¨μ£Όμ–΄μ•Ό ν•˜λ―€λ‘œ Query에 μž‘μ„±



βœ” μ‚¬μš©

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());
            }
        });



profile
μ•ˆλ“œλ‘œμ΄λ“œ 개발자 지망생

1개의 λŒ“κΈ€

comment-user-thumbnail
2023λ…„ 7μ›” 18일

정말 쒋은 정보 κ°μ‚¬ν•©λ‹ˆλ‹€!

λ‹΅κΈ€ 달기