Created
September 14, 2018 14:40
-
-
Save CoderJava/76832f238cd7fe061e179dab4c80ad2a to your computer and use it in GitHub Desktop.
Network Stetho
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ... | |
| 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