android - 改造 - 动态传递 URL 值 - GET 方法

标签 android retrofit2

我有一个 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/

相关文章:

Android 改造 GET @Query ArrayList

android - 带有搜索过滤器的Flutter应用 GridView ?

java - Android 上的模拟 API 响应

Android 将特定 API 调用限制为 3 秒内一次

java - 使用 JSONObject 调用改造。

android - 拆分 apk 与 null 崩溃,不拆分工作正常

android - <使用权限android :name ="android.permission.SEND_SMS"/>

java - 如何将sharedpref添加到Retrofit界面

android - Firebase Cloud Messaging 文档中的 "app server in my own environment"是什么意思?

java - 使用 Gson 和改造的 Java 的 Json 错误 [预期为 BEGIN_ARRAY,但在第 1 行第 70 列路径 $.Data 处为 BEGIN_OBJECT]