Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/eyrafabdullayev/crypto-currencies
Simple example about how to Asynchronous Call using Retrofit and Observables in Kotlin
https://github.com/eyrafabdullayev/crypto-currencies
asynchronous crypto-currencies kotlin retrofit rx-java
Last synced: 12 days ago
JSON representation
Simple example about how to Asynchronous Call using Retrofit and Observables in Kotlin
- Host: GitHub
- URL: https://github.com/eyrafabdullayev/crypto-currencies
- Owner: eyrafabdullayev
- Created: 2020-07-11T08:10:04.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-07-11T08:54:17.000Z (over 4 years ago)
- Last Synced: 2024-11-05T21:31:48.999Z (2 months ago)
- Topics: asynchronous, crypto-currencies, kotlin, retrofit, rx-java
- Language: Kotlin
- Homepage:
- Size: 146 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# crypto-currencies
## Info
In this app i am going to show you current cryptocurrencies.> I took MY_API_KEY for free from this website [https://nomics.com/]
## Installation
Using this commands you can get it:
git clone https://github.com/eyrafabdullayev/crypto-currencies.git
## About
There are two cases: **if you are not using RXJava** or **if you are using it**
1) If you are NOT using RXJava
```kotlinval service = retrofit.create(CryptoApi::class.java)
val call = service.getData()call.enqueue(object : Callback> {
override fun onFailure(call: Call>, t: Throwable) {}
override fun onResponse(
call: Call>,
response: Response>
) {
if (response.isSuccessful) {
response.body()?.let {
}
}
}});
// and interface CryptoAPI
@GET("prices?key=YOUR_KEY")
fun getData(): Call>
```2) If you are using RXJava
> At first you must define this variable
```kotlin
//it used for when the Activity has been destroyed closing calls
private var compositeDisposable: CompositeDisposable? = null
```
> Then you must initialize this variable in onCreate() method like below
```kotlin
compositeDisposable = CompositeDisposable()
```
> After that
```kotlin
val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build().create(CryptoApi::class.java)//we can add other calls to 'compositeDisposable' as we trying below
compositeDisposable?.add(retrofit.getData() //getting data
.subscribeOn(Schedulers.io()) //here we trying listen to it on another thread
.observeOn(AndroidSchedulers.mainThread()) //here we trying execute data on main thread
.subscribe(this::handleResponse)) // sending response to the method
private fun handleResponse(cryptoList: List){
}
//and interface CryptoAPI can be like that
@GET("prices?key=YOUR_KEY")
fun getData(): Observable>
```