Skip to content

Instantly share code, notes, and snippets.

@CoderJava
Created September 14, 2018 14:40
Show Gist options
  • Select an option

  • Save CoderJava/76832f238cd7fe061e179dab4c80ad2a to your computer and use it in GitHub Desktop.

Select an option

Save CoderJava/76832f238cd7fe061e179dab4c80ad2a to your computer and use it in GitHub Desktop.
Network Stetho
// ...
val progressDialog = ProgressDialog(this)
progressDialog.setCancelable(false)
progressDialog.setMessage("Please wait")
button_network.setOnClickListener { _ ->
progressDialog.show()
Observable
.create<Boolean> { emitter ->
val request = Request.Builder()
.url("https://jsonplaceholder.typicode.com/posts")
.addHeader("Accept", "application/json")
.addHeader("Content-Type", "application/json")
.build()
OkHttpClient.Builder()
.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
.addNetworkInterceptor(StethoInterceptor())
.build()
.newCall(request)
.enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
emitter.onError(e)
}
override fun onResponse(call: Call, response: Response) {
emitter.onNext(true)
emitter.onComplete()
}
})
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{
progressDialog.dismiss()
},
{
progressDialog.dismiss()
it.printStackTrace()
},
{
/* nothing to do in here */
}
)
}
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment