0

I am working on an android application to which I have integrated 20 APIs using AsyncHttpClient, for every API call I have to implement these two below methods to get the Response whether it is success hit or failure.

client.post(context,url,null,AppConstant.APPLICATION_JSON_CONTENT,new AsyncHttpResponseHandler(){

@Override
public void onSuccess(int statusCode,cz.msebera.android.httpclient.Header[]headers,byte[]responseBody){

        }


@Override
public void onFailure(int statusCode,cz.msebera.android.httpclient.Header[]headers,byte[]responseBody,Throwable error){


        }
        });

I want to remove replication of these two methods from every activity on every API call.

Is it possible to create a class or interface to which I will pass on the URL, request parameters and header etc. required for API call and there I will define these two methods and this class/interface will be accessed for every API call. in return, it will provide me the response of the API

Santosh Yadav
  • 338
  • 2
  • 4
  • 15

1 Answers1

0

You can create your own instance of your own class which implements such interface. In order to store this instance you can use either singleton pattern or a value inside Application class (or instance)

By the way, you can use a custom parent class which implements wanted interface and hide some implementations.

Vyacheslav
  • 26,359
  • 19
  • 112
  • 194