gpt4 book ai didi

android - 使用相同的 url 前缀改造注释?

转载 作者:行者123 更新时间:2023-11-29 02:19:47 28 4
gpt4 key购买 nike

在我的 android 应用程序中,我像这样使用 Retrofit:

import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;
public interface TangoRestClient {

@POST("/myproject/1/user/login")
Call<JsonElement> login(@Body JsonElement body);

@POST("/myproject/1/user/register")
Call<JsonElement> register(@Body JsonElement body);

@POST("/myproject/1/user/dictionary")
Call<JsonElement> getDictionary(@Body JsonElement body);

}

如您所见,所有方法都具有相同的 url 前缀:

/myproject/1/user

可以提取这个常量并像这样执行 smt :

String prefix = " /myproject/1/user";

@POST("{prefix}/login")
Call<JsonElement> login(@Body JsonElement body);

@POST("{prefix}/register")
Call<JsonElement> register(@Body JsonElement body);

@POST("{prefix}/dictionary")
Call<JsonElement> getDictionary(@Body JsonElement body);

最佳答案

我这样做的方法是创建一个接口(interface)和一个管理类,它看起来像这样:

public class CatalogManager extends NetworkingManager {
private String id;

public CatalogManager(String baseUrl) {
super(baseUrl);
}

public CatalogManager(String baseUrl, String id) {
super(baseUrl);
this.id = id;
}

public interface CatalogInterface {
@GET("endpoint1")
Call<Data> getCatalogGenres();

@GET("endpoint2")
Call<Data> getGenreMovies(@Query("query_param") String id);
}

public void getCatalog(CatalogCallback callback){
CatalogInterface ci = retrofit.create(CatalogInterface.class);
Call call = ci.getEndpoint1();
call.enqueue(callback);
}

public void getCatalogCatagories(CatalogGenreCallback callback){
CatalogInterface ci = retrofit.create(CatalogInterface.class);
Call call = ci.getEndpoint2(id);
call.enqueue(callback);
}

@Override
protected OkHttpClient getHttpClient() {
return super.getHttpClient();
}

@Override
protected Gson getGson() {
return super.getGson();
}
}

然后当我调用该方法时,我会:

CatalogManager cm = new CatalogManager(Constants.BASE_URL , getId());
cm.getEndpoint1(new CatalogGenreCallback() {

@Override
public void onResponse(Call<Data> call, Response<SliderData> response) {

Data data = response.body();
if (data != null) {
adapter.updateAdapterInfo(data);
}
}

@Override
public void onFailure(Call<Data> call, Throwable t) {
Log.e(TAG, "Error: " + t.toString());
}

});

我有 Constant.BASE_URL,您可以传入任何您喜欢的 URL。在您的情况下,您可以创建一个常量,其中包含您的基础加上您的公共(public)路径:

    CatalogManager cm = new CatalogManager(Constant.BASE_URL + Constant.COMMONT_PATH , getId()); 

关于android - 使用相同的 url 前缀改造注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57043040/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com