答案 0 :(得分:1)
在您的项目中添加此类
public class ApiClient {
private static final String BASE_URL = "https://example.com";
private static Retrofit retrofit;
public static Retrofit getClient() {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(getHTTPClient().build())
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
private static OkHttpClient.Builder getHTTPClient() {
final OkHttpClient.Builder builder = new OkHttpClient.Builder();
return builder;
}
}
答案 1 :(得分:0)
您需要一个名为ApiClient
的课程,这个课程在您正在学习的教程中缺失
创建ApiClient的单例实例并定义基本URL
public class ApiClient {
public static final String BASE_URL = "http://api.themoviedb.org/3/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
如果您不熟悉改造,我建议您参考此link
答案 2 :(得分:0)
下面的代码用于制作改造对象..
public class ApiClient {
private final static String LOCAL_URL = "http://192.168.101.170:9057/";
private final static String BASE_URL = LOCAL_URL;
// private final static String BASE_URL = " http://ftd.php1.projectbeta.biz/";
//private final static String BASE_URL_SECURE = "PASTE_YOUR_BASE_HTTPS_URL_HERE_";
public static ApiClient apiClient;
private Retrofit retrofit = null;
public static ApiClient getInstance() {
if (apiClient == null) {
apiClient = new ApiClient();
}
return apiClient;
}
//private static Retrofit storeRetrofit = null;
public Retrofit getClient() {
return getClient(null);
}
private Retrofit getClient(final Context context) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder client = new OkHttpClient.Builder();
client.readTimeout(60, TimeUnit.SECONDS);
client.writeTimeout(60, TimeUnit.SECONDS);
client.connectTimeout(60, TimeUnit.SECONDS);
client.addInterceptor(interceptor);
client.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (context == null) {
request = request
.newBuilder()
.build();
} else {
request = request
.newBuilder()
.addHeader("Authorization", "Bearer " + AppSetting.getStringSharedPref(context, Constants.USER_KEY_TOKEN, ""))
.build();
}
return chain.proceed(request);
}
});
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client.build())
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;
}
}