Java 在不使用基本URL的情况下进行改装调用,获取@URL注释失败
我正在安卓系统中构建一个应用程序,希望通过改造打一些电话。 我有一个API类来进行调用,我有一个调用使用基本URL,然后构建在基本URL之上,但是第二个调用提供了一个自定义URL,所以我不希望使用基本URL。 我使用了@GET注释并用@Url声明了我的参数,但是当我查看输出时,它仍然使用基本Url,然后在它之后连接自定义Url 这是我的班级:Java 在不使用基本URL的情况下进行改装调用,获取@URL注释失败,java,android,retrofit2,Java,Android,Retrofit2,我正在安卓系统中构建一个应用程序,希望通过改造打一些电话。 我有一个API类来进行调用,我有一个调用使用基本URL,然后构建在基本URL之上,但是第二个调用提供了一个自定义URL,所以我不希望使用基本URL。 我使用了@GET注释并用@Url声明了我的参数,但是当我查看输出时,它仍然使用基本Url,然后在它之后连接自定义Url 这是我的班级: import com.alimuzaffar.example.dogs.model.AlbumImages; import com.alimuzaffar
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);
}
}
导入com.alimuzaffar.example.dogs.model.AlbumImages;
导入com.alimuzaffar.example.dogs.model.dogbrides;
导入com.google.gson.gson;
导入com.google.gson.GsonBuilder;
导入okhttp3.OkHttpClient;
进口okhttp3.0响应电子书;
导入okhttp3.logging.HttpLoggingInterceptor;
2.电话;;
进口改装2.改装;
进口改装2.converter.gson.GsonConverterFactory;
导入文件2.http.GET;
导入文件2.http.POST;
导入2.http.Path;
导入文件2.http.Url;
公共类Api{
专用静态接口api;
私有静态最终字符串BASE_URL=”https://rss.itunes.apple.com";
公共静态API接口getApi(){
如果(api==null){
HttpLoggingInterceptor logging=新的HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
OkHttpClient客户端=新建OkHttpClient.Builder()
.addInterceptor(日志记录)
.build();
Gson Gson=new GsonBuilder()
.registerTypeAdapter(
狗狗类,
新的JsonDogBreedsDeserializer()).setLenient()
.create();
改装改装=新改装.Builder()
.baseUrl(基本URL)
.客户(客户)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
api=改造.create(apinterface.class);
}
返回api;
}
公共接口
{
@获取(“api/v1/us/apple music/top albums/all/25/explicit.json/”)
调用getbrides();
@得到
调用fetchImage(@Url字符串Url);
}
}
多亏了一个人的评论,我创建了一个全新的改装实例,并将我的自定义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);
为此,您需要创建单独的改型实例。不能使用同一个实例。嘿,感谢您的评论,我尝试了,不幸的是,改型迫使我提供一个有效的,非空的基本URL。但是我的URL都是自定义的,甚至没有一个共同的基础。一旦我声明了新实例,我如何才能让它工作?