我正在使用Square的Retrofit库进行短期网络调用.我@Query在每个请求中都包含了一些数据作为参数.像这样:
@GET("/thingOne.php")
void thingOne(
@Query("app_version") String appVersion,
@Query("device_type") String deviceType,
Callback<Map<String,Object>> callback
);
@GET("/thingTwo.php")
void thingTwo(
@Query("app_version") String appVersion,
@Query("device_type") String deviceType,
Callback<Map<String,Object>> callback
);
Run Code Online (Sandbox Code Playgroud)
必须定义appVersion和deviceType为每个端点中概述的每个端点都很麻烦Interface.有没有办法设置每个请求应包含的基本参数集?类似于我们如何设置共同的东西Authorization Header?
RestAdapter restAdapter = new RestAdapter.Builder()
.setServer("...")
.setRequestHeaders(new RequestHeaders() {
@Override
public List<Header> get() {
List<Header> headers = new ArrayList<Header>();
Header authHeader = new Header(
"Authorization", "Bearer " + token);
headers.add(authHeader);
}
return headers;
}
})
.build();
this.service = restAdapter.create(ClientInterface.class);
Run Code Online (Sandbox Code Playgroud) 我正在开发一个新的Android项目,而Retrofit2对我来说效果很好.但是,我需要在我发送的数据之上使用两个字符串中的一个来命中一个网址.
现在它看起来像这样:
@FormUrlEncoded
@POST("token")
Call<AccessResponse> requestAccess(@Field("grant_type") String type, @Field("code") String authCode, @Field("client_id") String ApiKey);
Run Code Online (Sandbox Code Playgroud)
授权类型只是两件事之一,我想把它抽象成静态网址,如下所示:
@FormUrlEncoded
@POST("token")
@Field("grant_type","type1")
Call<AccessResponse> requestAccess( @Field("code") String authCode, @Field("client_id") String ApiKey);
@FormUrlEncoded
@POST("token")
@Field("grant_type","type2")
Call<AccessResponse> refreshAccess( @Field("code") String authCode, @Field("client_id") String ApiKey);
Run Code Online (Sandbox Code Playgroud)
有没有办法实现这个目标?我2天的google-fu没有为我工作,也没有浏览API文档和代码.我只是不想在我的代码中的各个地方跟踪正确的字符串.