【问题标题】:Using RxJava and Resuable Retrofit to create rest client - Observable使用 RxJava 和 Resuable Retrofit 创建 REST 客户端 - Observable
【发布时间】:2020-11-10 12:59:24
【问题描述】:

收到此错误试图访问方法 com.google.gson.Gson.newJsonWriter(Ljava/io/Writer;)Lcom/google/gson/stream/JsonWriter;尝试使用 Retrofit 时来自类 retrofit2.converter.gson.GsonRequestBodyConverter

compile('com.google.code.gson:gson:2.8.2')
    compile('com.squareup.okhttp3:logging-interceptor:3.14.1')
    compile('com.squareup.okhttp3:okhttp:3.14.1')
    compile('com.squareup.retrofit2:converter-gson:2.5.0')
    compile('com.squareup.retrofit2:retrofit:2.5.0')
    compile('com.squareup.retrofit2:adapter-rxjava2:2.5.0')

我的类实现如下

public class RetrofitService {

    private static OkHttpClient getClient() {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .readTimeout(60, TimeUnit.SECONDS)
                .connectTimeout(60, TimeUnit.SECONDS)
                .addInterceptor(loggingInterceptor)
                .addInterceptor(chain -> {
                    Request request = chain.request();
                    return chain.proceed(request
                            .newBuilder()
                            .header("Content-Type", "application/json")
                            .method(request.method(), request.body()).build());
                });
        return builder.build();
    }

    private static Gson getGson() {
        return new GsonBuilder()
                .create();
    }

    private static Retrofit getRestAdapter(String baseUrl) {
        return new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create(getGson()))
                .client(getClient())
                .build();
    }

    static RepositoryInterface getService(String baseUrl) {
        return getRestAdapter(baseUrl).create(RepositoryInterface.class);
    }
}

接口实现

public interface RepositoryInterface {
    
    @POST("user")
    Observable<Response<Void>> createUser(
            @Header("S-Key") String sKey,
            @Header("X-Id") String xId,
            @Body HashMap<String, String> body
    );

    @GET("user/{X-Id}")
    Observable<Response<ApiUser>> getUser(
            @Header("S-Key") String sKey,
            @Path("X-Id") String xId
    );
}

我哪里出错了?请帮忙

【问题讨论】:

    标签: gson observable retrofit2 rx-java okhttp


    【解决方案1】:

    你可以试试这条线

    .addConverterFactory(GsonConverterFactory.create())
    

    而不是

    .addConverterFactory(GsonConverterFactory.create(getGson()))

    在创建改造对象时。

    编辑:如果 GSON sdk 存在依赖循环

    configurations.all { 
        resolutionStrategy.force 'com.google.code.gson:gson:2.8.6'
    }
    

    同时使用检查依赖树

    ./gradlew app:dependencies --configuration implementation
    

    【讨论】:

    • 仍然给我同样的错误failed; nested exception is java.lang.IllegalAccessError: tried to access method com.google.gson.Gson.newJsonWriter(Ljava/io/Writer;)Lcom/google/gson/stream/JsonWriter; from class retrofit2.converter.gson.GsonRequestBodyConverter] with root cause java.lang.IllegalAccessError: tried to access method com.google.gson.Gson.newJsonWriter(Ljava/io/Writer;)Lcom/google/gson/stream/JsonWriter; from class retrofit2.converter.gson.GsonRequestBodyConverter at retrofit2.converter.gson.GsonRequestBodyConverter.convert(GsonRequestBodyConverter.java:46)
    • 嗯,似乎 gson 在其他一些库中也被复制了不同的版本。您可以强制 gson 库使用单一版本。
    • 添加了对该问题的编辑看看它是否解决了
    • 它没有用。请检查我完整的build.gradle 文件和建议justpaste.it/edit/37308595/n17loglww6o17rap
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-06
    • 2010-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    相关资源
    最近更新 更多