【发布时间】: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