java - 如何在 Retrofit GET API 中传递查询参数?

标签 java android retrofit

我的buid.gradle就是这样。

implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'

implementation 'com.squareup.okhttp3:logging-interceptor:3.8.0'
implementation 'com.android.support:design:29.0.2'
implementation 'com.github.bumptech.glide:glide:3.7.0'

生成Api客户端

public class ApiClient {

    private static Retrofit retrofit = null;

    public static RestApiMethods getRestApiMethods() {
        return createRetrofit().create(RestApiMethods.class);
    }

    private static Retrofit createRetrofit() {
        if (retrofit == null) {
            OkHttpClient.Builder httpClient = getBuilder();
            httpClient.protocols(Arrays.asList(Protocol.HTTP_1_1));
            retrofit = new Retrofit.Builder()
                    .baseUrl(BuildConfig.BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(httpClient.build())
                    .build();
        }
        return retrofit;
    }

    @NonNull
    private static OkHttpClient.Builder getBuilder() {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        // set your desired log level
        if (BuildConfig.IS_DEBUG)
            logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        httpClient.connectTimeout((long)60 * 3, TimeUnit.SECONDS)
                .readTimeout((long)60 * 3, TimeUnit.SECONDS)
                .writeTimeout((long)60 * 3, TimeUnit.SECONDS);
        // add logging as last interceptor
        httpClient.addInterceptor(logging);
        return httpClient;
    }


}

打电话

@GET("URL")
Call<ResponseClass> getUser(@Path("id") int id);

调用API时,出现注释错误。 如何使 Url 类似于 URL/id?=1。

最佳答案

尝试这样

 @GET("URL")
Call<ResponseClass> getUser(@Query("id") int id);

关于java - 如何在 Retrofit GET API 中传递查询参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59299111/

相关文章:

java - 将 -javaagent 添加到 Tomcat 6 服务器,我应该将它放在哪里以及采用什么格式?

java - PipedInputStream.java 的设计中是否存在错误或者我误解了它的设计?

java - Linux 上的 Selenium NoSuchSession

java - 滑动回收站 View 无法正常工作

java - Android-使用 eclipse 安装到设备一次安装同一应用程序的两个实例-错误

android - 使用 SimpleXML 反序列化带有 HTML 转义部分的 XML

Android OAuth 改造访问 token 请求

java - 键盘隐藏 BottomSheetDialog

java - 什么是IndexOutOfBoundsException?我该如何解决?

php - 使用 Android 将字符串数组传递到 PHP 文件