0

I'm facing to such problem: get data from server and load into TextView. Of course, it's quite simple. But I want to implement it with some new for me approach. I started digging in RxJava, but found that it isn't used usually for such simple tasks and applied for operating more complex flows of data. Am I right? What are best practices for my task?

So I implemented it with Retrofit, but also I've seen that there is a tight relation between Rx and Retrofit. Second one used for interaction with Net and Rx let's operate data asynchronously in general?

Can you explain me please difference between these frameworks and how there are usually used?

Thanks everyone for answers in advance!

Ening
  • 455
  • 3
  • 19
  • I halfway into writing an example but I thought there must be a better answer on SO here. So here it is: https://stackoverflow.com/questions/21890338/when-should-one-use-rxjava-observable-and-when-simple-callback-on-android/29918329 – denvercoder9 May 16 '18 at 18:58
  • Possible duplicate of [When should one use RxJava Observable and when simple Callback on Android?](https://stackoverflow.com/questions/21890338/when-should-one-use-rxjava-observable-and-when-simple-callback-on-android) – denvercoder9 May 16 '18 at 18:58

1 Answers1

0

Retrofit is basically an abstraction to the Android's own object. HttpURLConnection

One is not dependent on the other. I would recommend you learning Reactive programming (RxJava), besides being almost a standard at this point, makes your life easier once you get the hang of it.

And a basic implementation would be:

public interface GitHubService {
  @GET("users/{user}/repos")
  Call<List<Repo>> listRepos(@Path("user") String user);
}

// The Retrofit class generates an implementation of the GitHubService interface.

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("https://api.github.com/")
    .build();

GitHubService service = retrofit.create(GitHubService.class);
Each Call from the created GitHubService can make a synchronous or asynchronous HTTP request to the remote webserver.

Call<List<Repo>> repos = service.listRepos("octocat");

If you want to include/combine RxJava with Retrofit just simple include the necessary dependencies and instead of returning a Call<T> you would return an Observable<T>, of course calling and handling the responses would be the Rx way.

You can find some nice Rx examples here:

Joaquim Ley
  • 4,038
  • 2
  • 24
  • 42