问题
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'
I exclued any OkHttp libraries as Retrofit already uses it.
This is my request interface:
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)
Thank you for any help.
回答1:
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'
And the error disappeared.
来源:https://stackoverflow.com/questions/41258878/retrofit-2-no-virtual-method-newjsonreaderljava-io-reader-nosuchmethodexcept