Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

should i put my retrofit calls inside and Android Service class?

should i put my retrofit calls inside and Android Service class? for my application iam calling retrofit inside in some classes for example like below

 Call<ArrayList<CantItem>> mycall = retrofitcalls.getCanteenItems("url.php", urldatamap);
        mycall.enqueue(new Callback<ArrayList<CantItem>>() {
            @Override
            public void onResponse(Response<ArrayList<CantItem>> response, Retrofit retrofit) {

                int code = response.code();
                Log.d("code ", String.valueOf(code));

                if (code == 200 || code == 201) {
                    ArrayList<CantItem> cantitems = response.body();
                    Log.d("retrieved", "returned items");

                    savedToSharedPrefs(createString(cantitems));
                    cantmap = createMap(cantitems);
                    presenter.updateView(cantmap);
                }
            }

            @Override
            public void onFailure(Throwable t) {
                Log.d("couldnt retrieve", "failure");
            }
        });

I was wondering should i put this call inside an Android Service class? as my retrofit call runs asynchronously anyway? Every tutorial ive seen seems to run it in either classes or activities. I haven't seen anyone using a service. Im not 100% sure what the best approach is at present. thanks

like image 380
filthy_wizard Avatar asked Dec 05 '25 10:12

filthy_wizard


1 Answers

It really depends on your requirements. Do you need the result of the webservice call even if your application killed?

If yes, then put it into a service.

If not then just starts something from your Activity.

like image 102
jbarat Avatar answered Dec 08 '25 00:12

jbarat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!