Skip to content

Commit 5ea0b61

Browse files
authored
Merge pull request #2 from EbenezerGH/add-basic-networking
Add basic networking
2 parents f3a7a48 + a1d70a9 commit 5ea0b61

File tree

7 files changed

+133
-5
lines changed

7 files changed

+133
-5
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@
2121
</activity>
2222
</application>
2323

24+
<uses-permission android:name="android.permission.INTERNET" />
25+
2426
</manifest>

app/src/main/java/jfyg/etherscan/helloetherescan/MainActivity.kt

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,52 @@
11
package jfyg.etherscan.helloetherescan
22

33
import android.os.Bundle
4-
import android.support.design.widget.Snackbar
54
import android.support.v7.app.AppCompatActivity
5+
import android.util.Log
66
import android.view.Menu
77
import android.view.MenuItem
8+
import io.reactivex.android.schedulers.AndroidSchedulers
9+
import io.reactivex.schedulers.Schedulers
10+
import jfyg.etherscan.helloetherescan.network.responses.BaseResponse
11+
import jfyg.etherscan.helloetherescan.network.RestClient
812

913
import kotlinx.android.synthetic.main.activity_main.*
1014

1115
class MainActivity : AppCompatActivity() {
12-
16+
private val TAG = javaClass.name!!
1317
override fun onCreate(savedInstanceState: Bundle?) {
1418
super.onCreate(savedInstanceState)
1519
setContentView(R.layout.activity_main)
1620
setSupportActionBar(toolbar)
1721

18-
fab.setOnClickListener { view ->
19-
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
20-
.setAction("Action", null).show()
22+
23+
fab.setOnClickListener {
24+
queryPrice("1I7CRNU2QIU253UBPFVB5UV2C2PBDURAIY")
2125
}
2226
}
2327

28+
private fun queryPrice(apiKey: String) {
29+
RestClient().getQuery()
30+
.getEtherStats("stats", "ethprice", apiKey)
31+
.observeOn(AndroidSchedulers.mainThread())
32+
.subscribeOn(Schedulers.io())
33+
.subscribe(this::handleResponse, this::handleError)
34+
}
35+
36+
private fun handleResponse(retrieveQuery: BaseResponse) {
37+
Log.d(TAG, "status: " + retrieveQuery.status)
38+
Log.d(TAG, "message: " + retrieveQuery.message)
39+
Log.d(TAG, "Eth in Btc: " + retrieveQuery.etherPriceresult?.ethBtc)
40+
Log.d(TAG, "Eth in Btc Timestamp: " + retrieveQuery.etherPriceresult?.ethBtcTimestamp)
41+
Log.d(TAG, "Eth in Usd: " + retrieveQuery.etherPriceresult?.ethUsd)
42+
Log.d(TAG, "Eth in Usd Timestamp: " + retrieveQuery.etherPriceresult?.ethUsdTimestamp)
43+
//Log.d(TAG, "Eth supply: " + retrieveQuery.etherSupplyResult)
44+
}
45+
46+
private fun handleError(error: Throwable) {
47+
Log.d(TAG, "The error " + error.message)
48+
}
49+
2450
override fun onCreateOptionsMenu(menu: Menu): Boolean {
2551
// Inflate the menu; this adds items to the action bar if it is present.
2652
menuInflater.inflate(R.menu.menu_main, menu)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package jfyg.etherscan.helloetherescan.network
2+
3+
import io.reactivex.Observable
4+
import jfyg.etherscan.helloetherescan.network.responses.BaseResponse
5+
import retrofit2.http.GET
6+
import retrofit2.http.Query
7+
8+
/**
9+
* Etherscan request endpoints
10+
*/
11+
interface NetworkService {
12+
13+
@GET("api")
14+
fun getEtherStats(@Query("module") module: String,
15+
@Query("action") action: String,
16+
@Query("apikey") apikey: String): Observable<BaseResponse>
17+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package jfyg.etherscan.helloetherescan.network
2+
3+
import retrofit2.Retrofit
4+
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
5+
import retrofit2.converter.gson.GsonConverterFactory
6+
import com.google.gson.GsonBuilder
7+
8+
/**
9+
* Client used to create the network call
10+
*/
11+
class RestClient {
12+
13+
//TODO: Retrieve baseUrl from string resources
14+
private var baseUrl: String = "http://api.etherscan.io/"
15+
private var networkService: NetworkService
16+
17+
init {
18+
val gson = GsonBuilder()
19+
.setLenient()
20+
.create()
21+
22+
val retrofit = Retrofit.Builder()
23+
.baseUrl(baseUrl)
24+
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
25+
.addConverterFactory(GsonConverterFactory.create(gson))
26+
.build()
27+
28+
networkService = retrofit.create(NetworkService::class.java)
29+
}
30+
31+
fun getQuery(): NetworkService {
32+
return networkService
33+
}
34+
35+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package jfyg.etherscan.helloetherescan.network.responses
2+
3+
import com.google.gson.annotations.SerializedName
4+
5+
/**
6+
* Base response that all queries will utilize
7+
*/
8+
open class BaseResponse {
9+
10+
@SerializedName("status")
11+
var status: String = ""
12+
13+
@SerializedName("message")
14+
var message: String = ""
15+
16+
@SerializedName("result")
17+
open var etherPriceresult: EtherPriceResponse? = null
18+
19+
//TODO: Figure out how to handle the same response name coming in from different queries
20+
//@SerializedName("result")
21+
//open var etherSupplyResult: String = ""
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package jfyg.etherscan.helloetherescan.network.responses
2+
3+
import com.google.gson.annotations.SerializedName
4+
5+
/**
6+
* Response for Ether prices
7+
*/
8+
class EtherPriceResponse {
9+
10+
@SerializedName("ethbtc")
11+
var ethBtc: String = ""
12+
13+
@SerializedName("ethbtc_timestamp")
14+
var ethBtcTimestamp: String = ""
15+
16+
@SerializedName("ethusd")
17+
var ethUsd: String = ""
18+
19+
@SerializedName("ethusd_timestamp")
20+
var ethUsdTimestamp: String = ""
21+
}

app/src/main/res/values/strings.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
<resources>
22
<string name="app_name">Hello Ethere Scan</string>
33
<string name="action_settings">Settings</string>
4+
5+
<string name="base_url">http://api.etherscan.io/</string>
6+
7+
<!--TODO: Remove Before Publishing-->
8+
<string name="my_api_key">1I7CRNU2QIU253UBPFVB5UV2C2PBDURAIY</string>
49
</resources>

0 commit comments

Comments
 (0)