Skip to content

ankitgusai/SimpleNetworkHelper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 

Repository files navigation

SimpleNetworkHelper

This is simple light-wight http network call solution. it is build on top of couple of well known libraries(mainly okhttp and RxAndroid).

The API is Glide like and simple to use. The core feature is the convenience over response handling. the response handlers are completely decoupled from the networking and threading module. practically you can create and plug any kind of handler you want.

lets check the example,

 Subscription subscription =
              NetworkHelper
                      .getHelper()
                      .createRequest()
                      .get("http://www.bing.com/az/hprichbg/rb/Halloween2016_EN-US7682362704_1920x1080.jpg")
                      .with(ImageHandler.Builder.create(200, 200))
                      .subscribe(bitmap -> {
                          ((ImageView) findViewById(R.id.imageView)).setImageBitmap(bitmap);
                      });

the with method accepts a HandlerBuilder creates a the Handler on the fly and omits a Observable.

public <Z, T extends Handler<Z>> Observable<Z> with(final HandlerBuilder<T> t)

Json response sample,

  Subscription subscription = NetworkHelper
              .getHelper()
              .createRequest()
              .get(BING_TEST_URL)
              .with(JsonHandler.Builder.createDefault(BingDummyModel.class))
              .subscribeOn(Schedulers.io())
              .observeOn(AndroidSchedulers.mainThread())
              .subscribe(bingDummyModel -> {
                  Utils.logd("response " + bingDummyModel.images.get(0).url);
              });

Building a custom handler is pretty straight forward. just extend the Handler<T> definition and write down your own logic to handle the response. create the HandlerBuilder by extending HandlerBuilder<T>.

The other part is that this mechanism will cache the service response in memory(RAM). it was just what i had to implement for a particular case. note that it isnt using okhttp caching mechanism which is way better approach if you have more control over server response. it is also more standard practice that is followed. but in memory mechanism is certainly useful is some case where you want to have more control over what is cached.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors