是否可以同时使用改装1.9.0和改装2.x?

时间:2017-07-21 14:28:05

标签: java android retrofit retrofit2

我有一个应用程序,其中包含许多现有的Retrofit 1.9接口。我想开始 逐步升级到Retrofit 2.x(目前一切都不可行) 获得对RxJava调用适配器的支持(因为不再开发1.9)。

让Retrofit1的RestAdapter共享一个OkHttp3客户端是相当微不足道的 将在Retrofit2接口中使用。版本1.9和2.x也有 不同的maven groupIds,所以这些类可以并存,没有任何问题。

但是,我在运行时遇到以下异常:

java.lang.IllegalAccessError: Method 'com.google.gson.stream.JsonWriter com.google.gson.Gson.newJsonWriter(java.io.Writer)' is inaccessible to class 'retrofit2.converter.gson.GsonRequestBodyConverter'

Retrofit 1对GSON 2.3.1有很强的依赖性,所讨论的方法在GSON 2.4中公开。我已经设置了我的Gradle依赖项,以便GSON依赖项解析为v2.7(我发布的最新版本):

build.gradle

compile('com.squareup.retrofit:retrofit:1.9.0') {
    exclude module: 'gson'
}
compile 'com.jakewharton.retrofit:retrofit1-okhttp3-client:1.1.0'

compile "com.squareup.retrofit2:retrofit:2.3.0"
compile "com.squareup.retrofit2:converter-gson:2.3.0"
compile "com.squareup.retrofit2:adapter-rxjava:2.3.0"
compile 'com.google.code.gson:gson:2.7'

运行./gradlew :app:dependencies表示正在解析GSON 2.7,但运行时行为是可疑的......

更新:我发现第三方硬件SDK将GSON 2.3.1捆绑在其内部的AAR中。我无法弄清楚如何删除它。

1 个答案:

答案 0 :(得分:1)

我最近与Retrofit 2.9.0一起实现了Retrofit 1.9,因为版本2能够更好地处理会话,并且我的API调用之一由于缺少处理响应cookie(会话)而失败。 / p>

我遇到了一个同样的问题,即此时无法将整个项目迁移到Retrofit 2。我可以确认它是否正常工作。

我将向您展示如何实现1.9和2.9.0的摘要。请参阅底部,以获取完整课程的链接。

对于两者: 创建一个类,您可以从中访问Retrofit对象并从以下位置调用接口:

public class ApiManager {

private static final String TAG = "API MANAGER";

private static final String API_URL = BuildConfig.API_URL;

private static Gson gson = new GsonBuilder()
    .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
    .setLenient()
    .create();

// The rest of the class to follow

,然后特定于1.9:

private static RequestInterceptor requestInterceptor = new RequestInterceptor() {
    @Override
    public void intercept(RequestInterceptor.RequestFacade request) {
        SessionManager sessionManager = new SessionManager(ContextHandler.getContext());
        HashMap session = sessionManager.getUserDetails();
        Object session_id = session.get("session_id");
        Object token = session.get("token");

        if (session_id != null && token != null) {
            request.addHeader("Cookie", "session_id=" + session_id + ";");
            request.addHeader("Cookie", "token=" + token + ";");
            Log.i("INTERCEPT", "Sent Cookies");
        }
        request.addHeader("Accept", "application/json");
    }
};

public static OkHttpClient getClient() {

    // init okhttp 3 logger
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);

    JavaNetCookieJar jncj = new JavaNetCookieJar(CookieHandler.getDefault());


    OkHttpClient client = new OkHttpClient();

    client.newBuilder()
            .addInterceptor(new AddCookiesInterceptor(ContextHandler.getContext()))
            .addInterceptor(new ReceivedCookiesInterceptor(ContextHandler.getContext()))
            .addNetworkInterceptor(logging)
            .cookieJar(jncj)
            .connectTimeout(10, TimeUnit.SECONDS)
            .writeTimeout(10, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.MINUTES);

    return client;
}

private static final RestAdapter REST_ADAPTER = new RestAdapter.Builder()
    .setEndpoint(API_URL) // On device
    .setRequestInterceptor(requestInterceptor)
    .setClient(new Ok3Client(getClient()))
    .setConverter(new GsonConverter(gson))
    .setLogLevel(RestAdapter.LogLevel.FULL) //log the request
    .build();


public interface AuthenticationInterface {
    @Headers("Content-type: application/json")
    @POST("/auth/getsession")
    void Authenticate(@Body Authentication Auth, Callback<SessionStore> response);

    @Headers("Content-type: application/json")
    @GET("/auth/logout")
    void logout(Callback<String> response);

    @Headers("Content-type: application/json")
    @GET("/auth/logout")
    String logout();
}


// Bind REST_ADAPTER to Interface
public static final AuthenticationInterface AUTHENTICATION_INTERFACE = REST_ADAPTER.create(AuthenticationInterface.class);
// Use this when you want to run the request.
public static AuthenticationInterface getAuthenticationService(){ return AUTHENTICATION_INTERFACE;  }

因此,您可以按以下方式使用以上内容:

ApiManager.getAuthenticationService().Authenticate(auth, new Callback<SessionStore>() {

    @Override
    public void success(SessionStore sessionStore, Response response) {
        // Do somthing

    }

    @Override
    public void failure(RetrofitError error) {
        // Handle Error
    }
});

对于2.9.0:

public static OkHttpClient getHeader() {
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient okClient = new OkHttpClient.Builder()
            .addInterceptor(interceptor)
            .addInterceptor(new AddCookiesInterceptor(ContextHandler.getContext())) 
            .addInterceptor(new ReceivedCookiesInterceptor(ContextHandler.getContext())) 
            .cookieJar(cookieJar)
            .connectTimeout(10, TimeUnit.SECONDS)
            .writeTimeout(10, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.MINUTES)
            .addNetworkInterceptor(
                    new Interceptor() {
                        @Override
                        public Response intercept(Interceptor.Chain chain) throws IOException {
                            Request request = null;
                                Log.d("--Authorization-- ", "authorizationValue");

                            Request original = chain.request();
                            // Request customization: add request headers
                            Request.Builder requestBuilder = original.newBuilder();


                            SessionManager sessionManager = new SessionManager(ContextHandler.getContext());

                            HashMap session = sessionManager.getUserDetails();
                            Object session_id = session.get("session_id");
                            Object token = session.get("token");

                            if (session_id != null && token != null) {
                                requestBuilder.addHeader("Cookie", "session_id=" + session_id + ";");
                                requestBuilder.addHeader("Cookie", "token=" + token + ";");
                                Log.i("INTERCEPT", "Sent Cookies");
                            }
                            requestBuilder.addHeader("Accept", "application/json");

                                request = requestBuilder.build();

                            return chain.proceed(request);
                        }
                    })
            .build();
    return okClient;

}

private static final Retrofit REST_ADAPTER2 = new Retrofit.Builder()
    .baseUrl(API_URL) // On device
    .client(getHeader())
    .addConverterFactory(GsonConverterFactory.create(gson))
    .build();

public interface JasperReportsInterface {

    /**
     *
     * @param agent_id
     * @param report_id
     */
    @retrofit2.http.Headers("Content-type: application/json")
    @retrofit2.http.GET("/agents/{agent_id}/reports/{report_id}/")
    Call<Reports> GetAgentReportView(@retrofit2.http.Path("agent_id") String agent_id, @retrofit2.http.Path("report_id") String report_id);

    /**
     *
     * @param agent_id
     * @param report_id
     */
    @retrofit2.http.Headers("Content-type: application/json")
    @retrofit2.http.GET("/agents/{agent_id}/reports/{report_id}/jobs")
    Call<Jobs> PollAgentReportData(@retrofit2.http.Path("agent_id") String agent_id, @retrofit2.http.Path("report_id") String report_id);

    /**
     *
     * @param agent_id
     * @param report_id
     * @param jsonBody
     */
    @retrofit2.http.Headers("Content-type: application/json")
    @retrofit2.http.POST("/agents/{agent_id}/reports/{report_id}/jobs")
    Call<String> PostAgentReportData(@retrofit2.http.Path("agent_id") String agent_id, @retrofit2.http.Path("report_id") String report_id, @retrofit2.http.Body JsonObject jsonBody);

    /**
     *
     * @param agent_id
     * @param report_id
     * @param jsonBody
     */
    @retrofit2.http.Headers("Content-type: application/json")
    @retrofit2.http.POST("/agents/{agent_id}/reports/{report_id}/jobs")
    Call<String> DownloadAgentReportData(@retrofit2.http.Path("agent_id") String agent_id, @retrofit2.http.Path("report_id") String report_id, @retrofit2.http.Body JsonObject jsonBody);




}
// Bind REST_ADAPTER2 to Interface
public static final JasperReportsInterface JASPER_REPORTS_INTERFACE = REST_ADAPTER2.create(JasperReportsInterface.class);
// Use this when you want to run the request.
public static JasperReportsInterface getJasperReportsService(){  return JASPER_REPORTS_INTERFACE;  }

您将按以下方式使用以上内容:

Call<Reports> reportsCall = ApiManager.getJasperReportsService().GetAgentReportView(agentsID, reportTypeID);
reportsCall.enqueue(new retrofit2.Callback<Reports>() {
    @Override
    public void onResponse(Call<Reports> call, retrofit2.Response<Reports> response) {
        if(response.isSuccessful()) {
            report = response.body();

        } else {
            int statusCode = response.code();

            // handle request errors yourself
            ResponseBody errorBody = response.errorBody();
        }

    }

    @Override
    public void onFailure(Call<Reports> call, Throwable t) {

    }
});

您需要的依赖关系分别是1.9和2的基本需求。

有关完整课程,请参见here