当尝试使用Retrofit时,com.google.gson.Gson.newJsonWriter(Ljava/io/Writer;)Lcom/google/gson/stream/JsonWriter;试图从类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
);
}
我哪里出错了?请帮帮忙
发布于 2020-07-21 01:29:38
你能试试这句话吗?
.addConverterFactory(GsonConverterFactory.create())
而不是
.addConverterFactory(GsonConverterFactory.create(getGson()))
在创建翻新对象时。
编辑:在GSON的依赖周期中
configurations.all {
resolutionStrategy.force 'com.google.code.gson:gson:2.8.6'
}
还可以使用
./gradlew app:dependencies --configuration implementation
https://stackoverflow.com/questions/63009560
复制相似问题