使用 Retrofit 将两个端点合二为一 Activity
Call two endpoint in one Activity using Retrofit
有没有办法在同一个 activity 中调用 2 个不同的端点并在 "Onrespond" 中找出结果?
我的意思是这样的:
在 MainActivity 中(例如)我称之为:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://robinchutaux.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
Call<JsonElement> call = WhosebugAPI.loadQuestions("/Blog/posts");
call.enqueue(this);
并获得调用 "onResponse" 方法的响应。
但是如果我也需要调用这个页面怎么办? (或者更像是 10 个不同的端点):
"/Blog/term"
我应该创建另一个像 Call2...?
如果是,如何在Onrespond中找到新来电的Onrespond?
你能帮我吗?
谢谢你。
PS:我知道我的问题来自于我缺乏信息,我是 Retrofit 的新手。
您可以检查example
其中的接口 class 代表每次调用。
public interface GitHub {
@GET("/repos/{owner}/{repo}/contributors")
Call<List<Contributor>> contributors(
@Path("owner") String owner,
@Path("repo") String repo);
}
以上 class 使您的通话同步。因为它没有回调对象作为参数。
public interface GitHub {
@GET("/repos/{owner}/{repo}/contributors")
Call<List<Contributor>> contributors(
@Path("owner") String owner,
@Path("repo") String repo),
Callback<List<Contributor>> callback;
}
如果您像这样添加回调,它将是异步调用。所以你不需要自己开帖,轻松得到回复。
还要确保你的改造对象是单实例或单例。
祝你好运。
有没有办法在同一个 activity 中调用 2 个不同的端点并在 "Onrespond" 中找出结果?
我的意思是这样的:
在 MainActivity 中(例如)我称之为:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://robinchutaux.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
Call<JsonElement> call = WhosebugAPI.loadQuestions("/Blog/posts");
call.enqueue(this);
并获得调用 "onResponse" 方法的响应。
但是如果我也需要调用这个页面怎么办? (或者更像是 10 个不同的端点):
"/Blog/term"
我应该创建另一个像 Call2...?
如果是,如何在Onrespond中找到新来电的Onrespond? 你能帮我吗? 谢谢你。 PS:我知道我的问题来自于我缺乏信息,我是 Retrofit 的新手。
您可以检查example
其中的接口 class 代表每次调用。
public interface GitHub {
@GET("/repos/{owner}/{repo}/contributors")
Call<List<Contributor>> contributors(
@Path("owner") String owner,
@Path("repo") String repo);
}
以上 class 使您的通话同步。因为它没有回调对象作为参数。
public interface GitHub {
@GET("/repos/{owner}/{repo}/contributors")
Call<List<Contributor>> contributors(
@Path("owner") String owner,
@Path("repo") String repo),
Callback<List<Contributor>> callback;
}
如果您像这样添加回调,它将是异步调用。所以你不需要自己开帖,轻松得到回复。
还要确保你的改造对象是单实例或单例。
祝你好运。