Skip to content

3. Callback options

Andrii Konovalenko edited this page Apr 12, 2017 · 4 revisions

1. Main information

Request executing process occurs in BaseTask class, and all information exchange occurs through NTaskListener callback interface. But implemented manually NTaskListener interface not necessary, it is intended for internal logic. Therefore for convenience programmer, class EasyNet contains implementation of this interface in the form of heirs NBaseCallback class.

2. NCallbackGson

This is the most popular callback implementation, which returns ready-to-use model, witch created with Gson library. Example:

a) Include latest Gson version

compile 'com.google.code.gson:gson:2.8.0'

b) Create model class

public class SomeModel {
    @SerializedName("field")
    private Object field;

    public SomeModel() {
    }

    public SomeModel(Object field) {
        this.field = field;
    }

    public Object getField() {
        return field;
    }

    public void setField(Object field) {
        this.field = field;
    }
}

c) Execute request with a NCallbackGson instance

EasyNet.get("path").start(new NCallbackGson<SomeModel>(SomeModel.class) {
            @Override
            public void onSuccess(SomeModel model, NResponseModel responseModel) {
                Object field = model.getField(); // Your logic with the model in case of successful execution
            }
        });

3. NCallbackParse

You can use this callback if you want to make manually parsing.

a) Create model class with implement NBaseModel

class SomeModel extends NBaseModel {
        private Object field;

        public SomeModel() {
        }

        public Object getField() {
            return field;
        }

        public void setField(Object field) {
            this.field = field;
        }

        @Override
        public NBaseModel parse(NResponseModel responseModel, JSONObject jsonObject) {
            this.field = jsonObject.opt("field");
            return this;
        }
    }

b) Execute request with a NCallbackParse instance

EasyNet.get("path").start(new NCallbackParse<SomeModel>(SomeModel.class) {
            @Override
            public void onSuccess(SomeModel model, NResponseModel responseModel) {
                Object field = model.getField(); // Your logic with the model in case of successful execution
            }
        });

This method very comfort if you are faced with a non-trivial task. During parsing, you can add conditions, modify and bind data in any way. Execution of parsing takes place in a separate thread.


Note!
NCallbackGson and NCallbackParse implementations can return list of models. If you root JSON element - array, use next callback method instead onSuccess(Object, NResponseModel):

      @Override
      public void onSuccess(ArrayList<SomeModel> models, NResponseModel responseModel) {
      }

4. NCallback

The following callback method works without any parsing, just returns a response from the server as NResponseModel. Among other things, this model contains a String body and Map<String, List<String>> headers. You can use this method for processing arbitrary information.

Example:

EasyNet.get("path").start(new NCallback() {
            @Override
            public void onSuccess(NResponseModel responseModel) {
                 responseModel.getBody();
            }
        });

Clone this wiki locally