Skip to content

This is a custom network client helper infrastructure for Android apps.

License

Notifications You must be signed in to change notification settings

echsylon/blocks-network

Repository files navigation

CircleCI Coverage Status JitPack Snapshot Download

Network

This is a simple callback infrastructure. It will allow you to define "tasks" and attach three different types of optional callback listeners; SuccessListener, ErrorListener and FinishListener. The library also offers a CallbackManager impelementation to manage the attached listeners for you and, when the time is right, deliver the result of the task through them.

Include

Add below gradle dependencies in your module build script like so:

dependencies {
    compile 'com.echsylon.blocks:network:{version}'
}

The JsonNetworkRequest implementation: Seperation of concerns

This library will enable the below asynchronous network request:

public class MyActivity extends AppCompatActivity {

    @Override
    public void onResume() {
        super.onResume();

        showProgressDialog();
        Product.get(123L)
            .withFinishListener(this::hideProgressDialog)
            .withErrorListener(this::showError)
            .withSuccessListener(this::parseJson);
    }

    private void hideProgressDialog() {
        // Hide the progress dialog
    }

    private void showError(Throwable cause) {
        // Show an error snackbar
    }

    private void parseJson(String json) {
        // Parse the JSON and populate UI
    }
}

For this to work you'll have to implement your Product domain. It could look something like this:

public class Product {

    public static class DTO {
        public final long id;
        public final float price;
        public final String name;
        
        public DTO(int id, float price, String name) {
            this.id = id;
            this.price = price;
            this.name = name;
        }
    }
    
    
    public Request<Product.DTO> get(long id) {
        return JsonNetworkRequest.enqueue(
                DefaultNetworkClient.getInstance(),
                "http://api.project.com/path/to/resource?id=123",
                "GET",
                null, // headers
                null, // payload
                new DefaultJsonParser(),
                Product.DTO.class);
    }
}

The DefaultNetworkClien

The core of this library is the default NetworkClient implementation. Internally it relies on OkHttp. The choice of a singleton design pattern is open for debate, but that's how the OkHttp client seems to work optimally.

You can, of course, provide your own NetworkClient implementation if the default one doesn't fit your needs.

About

This is a custom network client helper infrastructure for Android apps.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages