I was integrating Api ( Webservice) in android app I came across following error.

com.google.gson.stream.MalformedJsonException: JSON forbids NaN and infinities: NaN at line 1 column 123 path

I was using Retrofit with GsonConverter. Section 2.4 of JSON specification disallows special double values (NaN, Infinity, -Infinity).

so how to solve this problem?

we can use Following GsonBuilder properties

serializeSpecialFloatingPointValues()
or
serializeNulls()
or
setLenient()

your final Retrofit client should look like this

public static Retrofit getClient() {

Gson gson =
new GsonBuilder()
.serializeNulls()
.serializeSpecialFloatingPointValues()
.setLenient()
.create();


String baseurl = BuildConfig.BASE_URL;


retrofit = new Retrofit.Builder()
.baseUrl(baseurl)
.client(getHttpClient())
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();

return retrofit;
}

Hope it helps.

akshay shahane

Researcher by fate developer by choice

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store