티스토리 뷰
로또 오픈 api를 레트로핏을 활용하여 안드로이드에서 데이터를 가져오는 예제.
사용 java 파일
1. 데이터 오브젝트 - LottoData.java
2. 레트로핏 인터페이스 - retrofitInterface
3. 통신 호출 함수 - retro()
---
<uses-permission android:name="android.permission.INTERNET"/>
매니패스트에 추가 필요.
1.
public class LottoData {
String totSellamnt;
String returnValue;
String drwNoDate;
String firstWinamnt;
String firstPrzwnerCo;
String firstAccumamnt;
String drwtNo1;
String drwtNo2;
String drwtNo3;
String drwtNo4;
String drwtNo5;
String drwtNo6;
String bnusNo;
public String getTotSellamnt() { return totSellamnt; }
public String getReturnValue() { return returnValue; }
public String getDrwNoDate() { return drwNoDate; }
public String getFirstWinamnt() { return firstWinamnt; }
public String getFirstPrzwnerCo() { return firstPrzwnerCo; }
public String getFirstAccumamnt() { return firstAccumamnt; }
public String getDrwtNo1() { return drwtNo1; }
public String getDrwtNo2() { return drwtNo2; }
public String getDrwtNo3() { return drwtNo3; }
public String getDrwtNo4() { return drwtNo4; }
public String getDrwtNo5() { return drwtNo5; }
public String getDrwtNo6() { return drwtNo6; }
public String getBnusNo() { return bnusNo; }
}
2.
public interface retrofitInterface {
String LottoUrl = "https://www.nlotto.co.kr";
@GET("/common.do")
Call getLotto(@Query("method") String method, @Query("drwNo") String drwNum);
}
3.
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(retrofitInterface.LottoUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
retrofitInterface retrofitExService = retrofit.create(retrofitInterface.class);
retrofitExService.getLotto("getLottoNumber","1").enqueue(new Callback() {
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) {
if (response.isSuccessful()) {
LottoData body = response.body();
if (body != null) {
Log.d(TAG,"추첨일 " +body.drwNoDate);
}
}
}
@Override
public void onFailure(@NonNull Call call, @NonNull Throwable t) {
Log.d(TAG,"fail");
}
});
로또 open api의 호출 url는 https://www.nlotto.co.kr/common.do?method=getLottoNumber&drwNo=호출번호
이다.
url에 보면 쿼리 스트링이 ("?") 존재하기 때문에 그에 따른 값을 매핑시키기 위하여
인터페이스에 getlotto 보면 method 및 drwNo를 파라미터 값으로 넣어줌
그와 마찬가지로 호출부분에서도 위와 같은 값을 넣어줌
- 번외 -
만일 위와 같지 쿼리 스트링이 존재하지 않는 url은
/***
https://www.naver.com/posts/2 <- 예를 들면 이런 . url?
***/
url = https://www.naver.com;
@GET("/posts/{data}")
Call<LottoData> getData(@Path("data") String userId);
위와 같은 코드로 적용 시킬 수 있다.
'android' 카테고리의 다른 글
| 인텐트 oem 연동 리스트 (0) | 2019.09.23 |
|---|---|
| QR코드 리더 라이브러리 (0) | 2019.06.14 |
| 외부 키 입력 받기, (0) | 2019.06.12 |
| 안드로이드 텍스트 강조하기, 특정 텍스트 색 바꾸기 (0) | 2019.05.30 |
| 이미지 텍스트뷰 안에 텍스트 처럼 보여주기.. (0) | 2019.05.21 |
- Total
- Today
- Yesterday
- 차번호 정규표현식
- 함수형 코드
- ubunut android
- insert
- not starting .bash_profile
- MongoDB
- 자바
- https://hwan-shell.tistory.com/244
- Android
- 클래스형 코드
- 데이터베이스
- https://medium.com/@limgyumin/%EC%BD%94%ED%8B%80%EB%A6%B0-%EC%9D%98-apply-with-let-also-run-%EC%9D%80-%EC%96%B8%EC%A0%9C-%EC%82%AC%EC%9A%A9%ED%95%98%EB%8A%94%EA%B0%80-4a517292df29
- mac android
- React Native
- 안드로이드
- adb 환경변수
- nosql
- 차번호 정규식
- text 부분 색 칠하기
- not found adb
- ubunut 설치 link
- 귀찮아;;
- spannableText
- 구글 맵 경로 그리기
- react native state
- mongo db
- rn
- 명령어
- not working adb
- 구글 맵 선그리기
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
