我是在安卓系统中使用改造的新手。我有一个get请求,它以良好的方式工作,但我希望在URL的端点中包含共享首选项的值。假设我的端点Url是:
public interface Data{
@GET("/myphone/extra/pull/Sharedpreferencevalue") //add shared preference value here
}我可以在改造中这样做吗?或者我必须用其他的方法?或者如何在改造中做到?
发布于 2017-05-24 10:42:58
您可以按以下方式动态添加参数:
@GET("/myphone/extra/pull/{Sharedpreferencevalue}")
Call<YourResponseClass> getData(@Path("Sharedpreferencevalue") String value);发布于 2017-05-24 10:42:54
您可以使用@Path注释以编程方式向端点添加值,并在您的retrofit服务接口中执行类似的操作:
@GET("/myphone/extra/pull/{sharedprefValue}")
Call<EntityName> getPref(@Path("sharedprefValue") String pref);发布于 2017-05-24 12:36:42
动态地对retrofit2使用urls,如下所示。
在你的界面上
@GET
public Call<ResponseBody> fetchMileage(@Url String url);用这种方式
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(ROOT_URL)
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.client( httpClient.build()).build();
MyInterface myInterface = retrofit.create(MyInterface.class);
Call<ResponseBody> result = myInterface.fetchMileage(endpointUrl);
result.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
String output ="";
if (response.isSuccessful()) {
try {
output = response.body().string();
}catch (IOException e)
{
e.printStackTrace();
}
}else{
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable throwable) {
//Toast.makeText(context,"Error "+throwable.getMessage(),Toast.LENGTH_LONG).show();
throwable.printStackTrace();
}
});https://stackoverflow.com/questions/44155923
复制相似问题