改造2:没有虚拟方法newJsonReader(Ljava/io/Reader;)NoSuchMethodException [英] Retrofit 2: No virtual method newJsonReader(Ljava/io/Reader;) NoSuchMethodException
本文介绍了改造2:没有虚拟方法newJsonReader(Ljava/io/Reader;)NoSuchMethodException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在整个互联网上搜索此错误,但是,只有一个stackoverflow条目,没有答案或评论.我正在尝试使用Retrofit2.这是我第一次使用它.这是我的依赖项:
I am searching for this error whole internet, but yet, only one stackoverflow entry with no answer or comment. I am trying to use Retrofit 2. It is my first time using it. Here are my dependencies:
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.google.code.gson:gson:2.6.2'
由于Retrofit已在使用OkHttp库,因此我将其排除.
I exclued any OkHttp libraries as Retrofit already uses it.
这是我的请求界面:
public interface LoginService {
@POST(HTTPService.AUTHENTICATIO_URL)
Call<User> login();
}
下一步:我的服务生成器:
Next: my Service generator:
public class ServiceGenerator {
public static final String API_BASE_URL = HTTPService.BASE_URL;
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create());
public static <S> S createService(Class<S> serviceClass, String username, String password, String roleId) {
if (username != null && password != null) {
String credentials = username+":"+password+":"+roleId;
final String basic =
"Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
httpClient.addInterceptor(chain -> {
Request original = chain.request();
Request.Builder requestBuilder = original.newBuilder()
.header("Authorization", basic)
.header("Accept", "application/json")
.method(original.method(), original.body());
Request request = requestBuilder.build();
return chain.proceed(request);
});
}
OkHttpClient client = httpClient.build();
Retrofit retrofit = builder.client(client).build();
return retrofit.create(serviceClass);
}
}
接下来:我在哪里提出请求:
And next: where I make the request:
@Override
public void loadData(DataSource.LoadJsonCallback loadJsonCallback) {
String login = mUser.getLogin();
String password = mUser.getPassword();
LoginService loginService =
ServiceGenerator.createService(LoginService.class, login, password, "");
Call<User> call = loginService.login();
String a = call.request().url().toString();
call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
try {
loadJsonCallback.onTasksLoaded(response.body());
User a = response.body();
mUser = getDataFromJson("");
if (mUser != null) {
mUser.setPassword(password);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<User> call, Throwable t) {
loadJsonCallback.onDataNotAvailable(t.getMessage());
}
});
}
因此,我遇到了一个例外,我找不到任何地方:
So, I get this exception, that I cannot find anywhere:
java.lang.NoSuchMethodError: No virtual method newJsonReader(Ljava/io/Reader;)Lcom/google/gson/stream/JsonReader; in class Lcom/google/gson/Gson; or its super classes (declaration of 'com.google.gson.Gson' appears in /data/app/org.ucomplex.ucomplex-2/base.apk)
谢谢您的帮助.
推荐答案
我开始使用较新的版本:
I started using the newer versions:
compile 'com.google.code.gson:gson:2.7'
compile 'com.squareup.retrofit2:retrofit:2.2.0'
compile 'com.squareup.retrofit2:converter-gson:2.2.0'
错误消失了.
这篇关于改造2:没有虚拟方法newJsonReader(Ljava/io/Reader;)NoSuchMethodException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文