我正在建立一个安卓的应用程序,并想与Retrofit进行一些电话。我有一个进行调用的API类,我有一个使用基本URL的调用,然后在基本URL的基础上构建,但第二个调用被赋予一个自定义的URL,所以我不希望使用基本URL。我使用了@GET注释并使用@Url声明了我的参数,但是当我查看输出时,它仍然使用基本URL,然后在它后面连接自定义URL。
下面是我的类:
import com.alimuzaffar.example.dogs.model.AlbumImages;
import com.alimuzaffar.example.dogs.model.DogBreeds;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Path;
import retrofit2.http.Url;
public class Api {
private static ApiInterface api;
private static final String BASE_URL = "https://rss.itunes.apple.com";
public static ApiInterface getApi() {
if (api == null) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(logging)
.build();
Gson gson = new GsonBuilder()
.registerTypeAdapter(
DogBreeds.class,
new JsonDogBreedsDeserializer()).setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
api = retrofit.create(ApiInterface.class);
}
return api;
}
public interface ApiInterface
{
@GET("api/v1/us/apple-music/top-albums/all/25/explicit.json/")
Call<DogBreeds> getBreeds();
@GET
Call<AlbumImages> fetchImage(@Url String url);
}
}发布于 2021-05-21 06:41:08
感谢一个人的评论,我创建了一个全新的翻新实例,并给出了我的自定义URL作为基本URL
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(logging)
.build();
Gson gson = new GsonBuilder()
.registerTypeAdapter(
DogBreeds.class,
new JsonDogBreedsDeserializer()).setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(this.getImageUrl())
.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
ApiInterfaceImage api = retrofit.create(ApiInterfaceImage.class);
api.fetchImage().enqueue(callback);https://stackoverflow.com/questions/67627307
复制相似问题