Skip to content

Instantly share code, notes, and snippets.

@ssaurel
Created July 3, 2018 11:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssaurel/75a5511c957e5b264b2353363f635811 to your computer and use it in GitHub Desktop.
Save ssaurel/75a5511c957e5b264b2353363f635811 to your computer and use it in GitHub Desktop.
Main Activity of the Chuck Norris Facts Android App for the SSaurel's Channel
package com.ssaurel.chucknorrisfacts
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.text.Html
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*
import okhttp3.*
import org.json.JSONObject
import java.io.IOException
class MainActivity : AppCompatActivity() {
// we will use the ICNDB API for Chuck Norris Facts
val URL = "https://api.icndb.com/jokes/random"
var okHttpClient: OkHttpClient = OkHttpClient()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
nextBtn.setOnClickListener {
loadRandomFact()
}
}
private fun loadRandomFact() {
runOnUiThread {
progressBar.visibility = View.VISIBLE
}
val request: Request = Request.Builder().url(URL).build()
okHttpClient.newCall(request).enqueue(object: Callback {
override fun onFailure(call: Call?, e: IOException?) {
}
override fun onResponse(call: Call?, response: Response?) {
val json = response?.body()?.string()
// we get the joke from the Web Service
val txt = (JSONObject(json).getJSONObject("value").get("joke")).toString()
// we update the UI from the UI Thread
runOnUiThread {
progressBar.visibility = View.GONE
// we use Html class to decode html entities
factTv.text = Html.fromHtml(txt)
}
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment