What's in this blog? Hide
    1. Quick Setup
  1. How to use

Android HTTP Library named EasyOKHttp is a fast and easy RESTful API request for android. The library can parse response by GSON and uses: OkHttp and RxJava-RxAndroid and Gson.

Need some career advice or prepping for an Android developer interview? Hit me up on Topmate.io, and let's chat!

Android HTTP Library: EasyOKHttp
Android HTTP Library: EasyOKHttp
EasyOKHttp

Quick Setup

Add Android HTTP Library: EasyOkHttp to dependencies :

implementation 'github.mehrdadf7:okhttp:1.0.7'

(optional) add RxJava-RxAndroid to dependencies :

implementation 'io.reactivex.rxjava2:rxandroid:2.1.0' 
implementation 'io.reactivex.rxjava2:rxjava:2.2.0'

(optional) If you need @SerializedName(“key_name”) add Gson :

implementation 'com.google.code.gson:gson:2.8.5'

Don’t forget Connect your phone to internet 🙂 and notice jcenter() is added :

repositories {        
jcenter()
}

How to use

AndroidManifest.xml :

<uses-permission android:name="android.permission.INTERNET" />

activity or fragment :

OkHttpInjector.getHttpClient().makeRequest(
      HttpRequest.Method method, //GET, POST
      String url, 
      String requestBody, 
      Class<T> responseType //ClassModel.class
  ).send().observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.newThread())
                .subscribe(new Observer<T>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        // pre-request
                    }

                    @Override
                    public void onNext(T t) {
                      //response(t)
                    }

                    @Override
                    public void onError(Throwable e) {
                      //handle error
                    }

                    @Override
                    public void onComplete() {
                      //complete-request
                    }
                });
                
/*************** OR ******************/    

OkHttpInjector.getHttpClient().makeRequest(
      HttpRequest.Method method, //GET, POST
      String url, 
      String requestBody, 
      Class<T> responseType //ClassModel.class
  ).send(activity, new OnResultCallback<T>() {
            @Override
            public void onReceived(T t) {
                  //response(t)
            }
            @Override
            public void onError() {
              //handle error
              Toast.makeText(this, "check internet", Toast.LENGTH_SHORT).show();
            }
        });  

JAVA

// Observer<T> and Observable<T> in 'io.reactivex' package name
public void requestName(Observer<ClassModel> observer) {
                OkHttpInjector.getHttpClient().makeRequest(
                        HttpRequest.Method.GET, //HttpRequest.Method.POST
                        url,
                        "", //jsonObject.toString()
                        ClassModel.class
                ).send().observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.newThread())
                .subscribe(observer);
    }

KOTLIN

fun requestName(observer: Observer<ClassModel>) {
        OkHttpInjector.getHttpClient()?.makeRequest(
            HttpRequest.Method.GET, //HttpRequest.Method.POST
            url,
            "", //jsonObject.toString()
            ClassModel::class.java
        )?.send()?.observeOn(AndroidSchedulers.mainThread())?.subscribeOn(Schedulers.newThread())
            ?.subscribe(observer)
    }

Read about Android OAuth2 Library : Simple O Auth

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

You May Also Like