티스토리 뷰
class MainViewModel(val app: Application) : AndroidViewModel(app) {
val tagtag = "tag@"
val retrofit: RetrofitApi = RetrofitBuilder.getApiService("https://api.bithumb.com/")
init {
initCoinRx()
initCoinCoroutine()
}
@SuppressLint("CheckResult")
fun initCoinRx() {
retrofit.getCoinInfoRx("BTC")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { coin ->
Log.d(tagtag, "coin rx : ${coin.data.openingPrice}")
}
}
var job: Job? = null
@SuppressLint("CheckResult")
fun initCoinCoroutine() {
job = CoroutineScope(Dispatchers.IO).launch {
val response = retrofit.getCoinInfoCoroutine("BTC")
withContext(Dispatchers.Main) {
if (response.isSuccessful) {
Log.d(tagtag, "coin coroutine : " + response.body()?.data?.openingPrice)
job?.cancel()
}
}
}
}
}
import io.reactivex.Single
import retrofit2.Response
import retrofit2.http.*
interface RetrofitApi {
@GET("public/ticker/{path}")
fun getCoinInfoRx(
@Path("path") path:String?
):Single<CoinModel>
@GET("public/ticker/{path}")
suspend fun getCoinInfoCoroutine(
@Path("path") path:String?
): Response<CoinModel>
}
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
object RetrofitBuilder {
private fun getRetrofit(url: String?):Retrofit {
val httpLoggingInterceptor = HttpLoggingInterceptor()
httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
val client = OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor).build()
val retrofit = Retrofit.Builder().
baseUrl(url).
addConverterFactory(GsonConverterFactory.create()).
addCallAdapterFactory(RxJava2CallAdapterFactory.create()).
client(client).build()
return retrofit;
}
fun getApiService(url:String?) : RetrofitApi {
return getRetrofit(url).create(RetrofitApi::class.java)
}
}
'android' 카테고리의 다른 글
di 패턴, dagger, hilt (0) | 2023.04.04 |
---|---|
system screen rotate click listener (0) | 2023.03.07 |
android mvvm 예제 (0) | 2023.01.30 |
retrofit (0) | 2022.09.21 |
by viewmodels() 쓰는법, (0) | 2022.08.18 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 클래스형 코드
- 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
- 함수형 코드
- text 부분 색 칠하기
- ubunut 설치 link
- nosql
- MongoDB
- 구글 맵 선그리기
- https://hwan-shell.tistory.com/244
- Android
- rn
- insert
- 귀찮아;;
- not found adb
- React Native
- 구글 맵 경로 그리기
- 차번호 정규식
- 안드로이드
- spannableText
- mongo db
- 명령어
- 차번호 정규표현식
- not working adb
- not starting .bash_profile
- react native state
- 자바
- ubunut android
- 데이터베이스
- adb 환경변수
- mac android
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함