Android: Retrofit beta2.0 ServiceClass with interface.无法理解为什么在不使用时使用接口
Android: Retrofit beta2.0 ServiceClass with interface. Unable to understand why use interface when it is not using
我不知道我的界面不工作。当我在界面中更改 URL
时,程序可以正常工作。但是,当我将 URL
从 REQUEST
更改为 URL
时,就会发生错误。什么原因。请帮我解决这个问题。
提前致谢
public class Service{
private static GitApiInterface gitApiInterface;
private static final String baseUrl = "https://api.vimeo.com/channels/";
public static GitApiInterface getClient() {
if (gitApiInterface == null) {
OkHttpClient okClient = new OkHttpClient();
okClient.interceptors().add(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = new Request.Builder()
.url(baseUrl + "staffpicks/videos")
.get()
.addHeader("Accept", "application/json")
.addHeader("Authorization", "Bearer " + accessToken)
.addHeader("cache-control", "no-cache")
.build();
Log.e("Request ", "" + request.url());
Response response = chain.proceed(request);
return response;
}
});
Retrofit client = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(okClient)
.addConverterFactory(new ToStringConverter())
.build();
gitApiInterface = client.create(GitApiInterface.class);
}
return gitApiInterface;
}
public interface GitApiInterface {
@GET("staffpicks/videos")
Call<String> getVimeoVideoData();
}
}
您的请求是 url 正在“https://api.vimeo.com/channels/staffpicks/videosstaffpicks/videos”。因为您要添加 "staffpicks/videos" 两次。
Retrofit 自动添加给定的注解,表示接口中的方法到基 url 的末尾。
Request request = new Request.Builder()
.url(baseUrl + "staffpicks/videos")
.get()
.addHeader("Accept", "application/json")
.addHeader("Authorization", "Bearer " + accessToken)
.addHeader("cache-control", "no-cache")
.build();
在本节中,“.url()”应该用于基础 urls。
我不知道我的界面不工作。当我在界面中更改 URL
时,程序可以正常工作。但是,当我将 URL
从 REQUEST
更改为 URL
时,就会发生错误。什么原因。请帮我解决这个问题。
提前致谢
public class Service{
private static GitApiInterface gitApiInterface;
private static final String baseUrl = "https://api.vimeo.com/channels/";
public static GitApiInterface getClient() {
if (gitApiInterface == null) {
OkHttpClient okClient = new OkHttpClient();
okClient.interceptors().add(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = new Request.Builder()
.url(baseUrl + "staffpicks/videos")
.get()
.addHeader("Accept", "application/json")
.addHeader("Authorization", "Bearer " + accessToken)
.addHeader("cache-control", "no-cache")
.build();
Log.e("Request ", "" + request.url());
Response response = chain.proceed(request);
return response;
}
});
Retrofit client = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(okClient)
.addConverterFactory(new ToStringConverter())
.build();
gitApiInterface = client.create(GitApiInterface.class);
}
return gitApiInterface;
}
public interface GitApiInterface {
@GET("staffpicks/videos")
Call<String> getVimeoVideoData();
}
}
您的请求是 url 正在“https://api.vimeo.com/channels/staffpicks/videosstaffpicks/videos”。因为您要添加 "staffpicks/videos" 两次。
Retrofit 自动添加给定的注解,表示接口中的方法到基 url 的末尾。
Request request = new Request.Builder()
.url(baseUrl + "staffpicks/videos")
.get()
.addHeader("Accept", "application/json")
.addHeader("Authorization", "Bearer " + accessToken)
.addHeader("cache-control", "no-cache")
.build();
在本节中,“.url()”应该用于基础 urls。