mil
mil

Reputation: 137

Call two endpoint in one Activity using Retrofit

Is there a way to call 2 different endpoint in a same activity and find out the result in the "Onrespond"?

I mean something like this:

In the MainActivity (for example) I call this:

Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://robinchutaux.com")
.addConverterFactory(GsonConverterFactory.create())
.build();

Call<JsonElement> call = stackOverflowAPI.loadQuestions("/Blog/posts");
call.enqueue(this);

and get Response of call on "onResponse" method.

But what if i need to call this page too? (or even more like 10 different endpoint):

"/Blog/term"

I should create another one like Call2... ?

if yes, how can I find out the Onrespond of new call in the Onrespond? can u help me? Thank you. PS: I know my question came from the lack of information from me, I am new in Retrofit.

Upvotes: 0

Views: 1739

Answers (1)

Emre Akt&#252;rk
Emre Akt&#252;rk

Reputation: 3346

You can check the example

Interfaces in that class represent each call.

public interface GitHub {
    @GET("/repos/{owner}/{repo}/contributors")
    Call<List<Contributor>> contributors(
        @Path("owner") String owner,
        @Path("repo") String repo);
  }

The above class is making your call to be sync. Because it dosent have callback object as a parameter.

public interface GitHub {
        @GET("/repos/{owner}/{repo}/contributors")
        Call<List<Contributor>> contributors(
            @Path("owner") String owner,
            @Path("repo") String repo),
            Callback<List<Contributor>> callback;
      }

If you add your callback like this, it will be async call. So you dont need to open your own thread and get the response easily.

Also make sure your Retrofit object be single instance or singleton.

Good luck there.

Upvotes: 1

Related Questions