Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to resolve java.lang.NoSuchMethodError with GSON library

I was trying to build a sample Retrofit Java program taken from;

https://github.com/square/retrofit/blob/master/samples/src/main/java/com/example/retrofit/SimpleService.java

I included required dependent jars (retrofit-2.0.0.jar,converter-gson-2.0.0.jar,okhttp-3.0.0-RC1.jar,okio-1.6.0.jar and gson-2.0.jar) to the build path.

I encounter the following exception while trying to run the application.

Exception in thread "main" java.lang.NoSuchMethodError: com.google.gson.Gson.getAdapter(Lcom/google/gson/reflect/TypeToken;)Lcom/google/gson/TypeAdapter;
at retrofit2.converter.gson.GsonConverterFactory.responseBodyConverter(GsonConverterFactory.java:63)
at retrofit2.Retrofit.nextResponseBodyConverter(Retrofit.java:325)
at retrofit2.Retrofit.responseBodyConverter(Retrofit.java:308)
at retrofit2.ServiceMethod$Builder.createResponseConverter(ServiceMethod.java:651)
at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:166)
at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:166)
at retrofit2.Retrofit$1.invoke(Retrofit.java:145)
at com.sun.proxy.$Proxy0.contributors(Unknown Source)
at SimpleService.main(SimpleService.java:40)

Am I missing any dependent jar?

I am trying to use Retrofit in standalone Java application and was testing samples to get used to it. It will be great if someone could point me to a simple sample program that demonstrates the working of Retrofit, even a sample of earlier version would be good.

like image 806
moooni moon Avatar asked Mar 24 '16 23:03

moooni moon


2 Answers

try to add this in gradle:

compile 'com.google.code.gson:gson:2.6.2'

it works well for me :)

like image 108
Yakami Avatar answered Sep 21 '22 15:09

Yakami


Here are some dependencies that might help you.

GSON and Retrofit

compile 'com.google.code.gson:gson:2.7'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.retrofit2:retrofit:2.1.0'

HTTP

compile 'org.apache.commons:commons-lang3:3.4'
compile 'org.apache.httpcomponents:httpcore:4.4.5'

Try to clean your project and sync with gradle. Or try using the same version jar files in your project.

Or have you replaced this line with correct value.

@GET("/repos/{owner}/{repo}/contributors")

What is the response after calling the API. plz check the URL in the browser.

let me know if issue still persists.

like image 35
DearDhruv Avatar answered Sep 18 '22 15:09

DearDhruv