无法解析符号' ApiClient' - 改造

时间:2018-04-09 03:37:37

标签: android retrofit2

我是Retrofit的新用户,我尝试从我的数据库中获取数据并将其显示在我应用的文本视图中。但是,我收到一条错误消息。

enter image description here

3 个答案:

答案 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;
}

}