我有一个 URL 例如:
https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVinValues/KMHDC8AEXAU084769?format=JSON
这里我想动态改变KMHDC8AEXAU084769?format=JSON
这部分
如何使用 Retrofit2 实现。
我试过:
@FormUrlEncoded
@GET("{input}")
Call<Result> getVin(@Path("input") String input, @Field("format") String format);
但是 @FormUrlEncoded
只支持 POST,不支持 GET。
我是这样调用它的:
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
Call<Result> call = apiService.getVin(vin, "JSON");
call.enqueue(new Callback<Result>() {
@Override
public void onResponse(Call<Result> call, Response<Result> response) {
Result result = response.body();
Log.e("Result: ", "" + response.body());
Gson gson = new Gson();
String json = gson.toJson(result);
responseTV.setText("" + json);
}
@Override
public void onFailure(Call<Result> call, Throwable t) {
// Log error here since request failed
Log.e("MainActivity", t.toString());
Toast.makeText(MainActivity.this, "Try later", Toast.LENGTH_SHORT).show();
}
});
最佳答案
试试这个:
@GET("/api/vehicles/DecodeVinValues/{input}")
Call<Result> getVin(@Path("input") String input, @Query("format") String format);
@FormUrlEncoded 和@Field 注释用于 POST 请求。
@GET 注解参数的当前值可能会根据您使用的 baseUrl 值而有所不同。
关于android - 改造 - 动态传递 URL 值 - GET 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45820998/