gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:27:12 25 4
gpt4 key购买 nike

我的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/

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