igor_rb
igor_rb

Reputation: 1891

How organize data from server on Android client

I develop Android client, that get orders list from server. For http requests I use Retrofit library, and wrap api service in class ApiClient

public class ApiClient {
    private ApiService apiService;

    public ApiClient() {
        Gson gson = new GsonBuilder()
                .setDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'")
                .create();
        RestAdapter restAdapter = new RestAdapter.Builder()
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setEndpoint(BuildConfig.API_SERVER)
                .setConverter(new GsonConverter(gson))
                .build();
        apiService = restAdapter.create(ApiService.class);

    }
    public ApiService getApiService() {
        return apiService;
    }
}

Now in MainActivity I call gerOrders of ApiService interface to get list of orders.

ApiService service = new ApiClient().getApiService();
service.getOrders(callback);

Callback callback = new Callback() {
    @Override
    public void success(Object o, Response response) {
        Log.d("TestObject", new Gson().toJson(o));
    }
    @Override
    public void failure(RetrofitError retrofitError) {
    }
};

But this orders list is may be used in other Activities, and I think, it's not good get data right inside Activity or Fragment. How I can organize this code well?

Upvotes: 0

Views: 44

Answers (1)

fab
fab

Reputation: 790

You can delegate this task to some helper, like DataHelper (you will find better name :) ), by this way activities, fragments, controllers can access it easily

Upvotes: 1

Related Questions