no news no news - 2 months ago 9
Android Question

How to get response from other thread using Retrofit 2.0

I use the retrofit 2 to get JSON

Array
and I want to create new
ManagerListAdapter
(It's adapter for
RecyclerView
) and pass
ArrayList
(which I got from JSON) in the constructor.

At the moment I successfully got this List in
onResponse
.
But it's other thread.

What's the best way to transfer it into the main thread?
Maybe I need to use RxJava and RxAndroid?

private void initRecyclerView() {
RecyclerView managersRecyclerView = (RecyclerView) view.findViewById(R.id.managersRecyclerView);
managersRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
managersRecyclerView.setAdapter(new ManagerListAdapter(getManagers()));




// getRemoteManagers();


}

private void getRemoteManagers() {

ApiObject client = ServiceGenerator.createService(ApiObject.class);
Call<List<ManagerDTO>> call = client.getManagers();


call.enqueue(new Callback<List<ManagerDTO>>() {
@Override
public void onResponse(Call<List<ManagerDTO>> call, Response<List<ManagerDTO>> response) {


if (response.isSuccessful()) {

List<ManagerDTO> managers = response.body(); //I want to add this List in RecyclerAdapter
sortManagers(managers);

}

}

@Override
public void onFailure(Call<List<ManagerDTO>> call, Throwable t) {

}
});


}


private List<ManagerDTO> getManagers() {

//It's for test.

List<ManagerDTO> managers = new ArrayList<>();

managers.add(new ManagerDTO("Alex Pavlov", 100));
managers.add(new ManagerDTO("Ivan Pavlov", 95));
managers.add(new ManagerDTO("Petr Pavlov", 99));
managers.add(new ManagerDTO("Stepan Pavlov", 100));
managers.add(new ManagerDTO("Azik Pavlov", 100));
managers.add(new ManagerDTO("Azik Myand", 100));
managers.add(new ManagerDTO("Azik Specca", 50));
managers.add(new ManagerDTO("Azik Kane", 0));
managers.add(new ManagerDTO("Azik McDonald", 99));
managers.add(new ManagerDTO("Azik Jagr", 1));

sortManagers(managers);

return managers;


}

Answer

We can use RxJava and RxAndroid with Retrifit2.

ServiceGenerator:

private static Retrofit.Builder builder =
            new Retrofit.Builder()
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io()))
                    .baseUrl(Const.URL.UrlService)
                    .addConverterFactory(JacksonConverterFactory.create());


   public static <S> S createService(Class<S> serviceClass) {

        Retrofit retrofit = builder.build();
        return retrofit.create(serviceClass);
    }
}

Request:

ApiObject service = ServiceGenerator.createService(ApiObject.class);
        Observable<List<ManagerDTO>> observManagers = service.getManagers();
        observManagers
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<List<ManagerDTO>>() {
                    @Override
                    public void onCompleted() {
                        //do nothing
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("OnError", e.getMessage());
                        Toast.makeText(context, R.string.toast_connection_false, Toast.LENGTH_SHORT).show();
                        initRecyclerView(Cache.getManagers());
                        managersSwipe.setRefreshing(false);
                    }

                    @Override
                    public void onNext(List<ManagerDTO> managers) {
                        Log.e("OnNext", "Connect is OK");
                        sortManagers(managers);
                        Cache.setManagerDTO(managers);
                        initRecyclerView(managers);
                        managersSwipe.setRefreshing(false);

                        //Copy to db
                        RealmHelper helper = new RealmHelper(context);
                        helper.copyToRealmOrUpdate(managers);
                        Log.e("RealmCopy", "it's ok");
                    }
                });
    }
Comments