Reputation: 93
There are couple of questions already available about "JSON document was not fully consumed" in Stackoverflow, but there is no accepted answer. Link is given below.
Android Retrofit 2.0 JSON document was not fully consumed
Why is JSON document not fully consumed?
In my app I'm taking an email from user. And using my API, I'm checking given email is exist or not in the database. If exist, I'm sending a code to that email. I am using localhost as Server and have used Slim for API. I think my API is Ok. I checked it via Postman. The Json response that I shared in my post is from Postman. Actually I am learning Retrofit. Help please. Thanks in advance.
RetrofitClient.java
package com.example.royta.retrofitdemo.APIs;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitClient {
private static final String BASE_URL = "http://aa561878.ngrok.io/MyApi/public/";
private static RetrofitClient retrofitClient;
private Retrofit retrofit;
private static Gson gson = new GsonBuilder()
.setLenient()
.create();
private RetrofitClient() {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
public static synchronized RetrofitClient getInstance() {
if(retrofitClient == null) {
retrofitClient = new RetrofitClient();
}
return retrofitClient;
}
public Api getApi() {
return retrofit.create(Api.class);
}
}
Json
{
"isSend": true,
"code": 117482,
"email": true
}
UserExist.java (Pojo)
package com.example.royta.retrofitdemo.ModelClass;
import com.google.gson.annotations.SerializedName;
public class UserExist {
@SerializedName("email")
private boolean email;
@SerializedName("isSend")
private boolean isSend;
@SerializedName("code")
private int code;
public UserExist(boolean email, boolean isSend, int code) {
this.email = email;
this.isSend = isSend;
this.code = code;
}
public boolean isSend() {
return isSend;
}
public boolean isEmailExist() {
return email;
}
public int getCode() {
return code;
}
}
Api.java
package com.example.royta.retrofitdemo.APIs;
import com.example.royta.retrofitdemo.ModelClass.UserExist;
import retrofit2.Call;
import retrofit2.http.GET;
public interface Api {
@GET("finduser")
Call<UserExist> isUserExist(
@Query("email") String email
);
}
Here is the call 👇
Call <UserExist> call = RetrofitClient.getInstance().getApi().isUserExist(email);
call.enqueue(new Callback<UserExist>() {
@Override
public void onResponse(Call<UserExist> call, Response<UserExist> response) {
if(response.body().isEmailExist()) {
String resetCode = String.valueOf(response.body().getCode());
Log.d("ROYs", "Reset Code: "+resetCode);
Intent i = new Intent(FindAccount.this, EnterSecurityCode.class);
i.putExtra("email", email);
i.putExtra("code", resetCode);
startActivity(i);
}
else {
Toast.makeText(FindAccount.this, "You are not registered", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<UserExist> call, Throwable t) {
String stacktrace = Log.getStackTraceString(t);
Log.d("ROYs", "onFailure Method: "+stacktrace);
}
});
Logcat
2019-01-18 19:44:23.129 29583-29583/com.example.royta.retrofitdemo D/ROYs: onFailure Method: com.google.gson.JsonIOException: JSON document was not fully consumed.
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:41)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:27)
at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:223)
at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:121)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:206)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Sometime I'm getting this following Exception in the same code
2019-01-18 21:55:39.610 1424-1424/com.example.royta.retrofitdemo D/ROYs: onFailure Method: java.net.SocketTimeoutException: timeout
at okio.Okio$4.newTimeoutException(Okio.java:232)
at okio.AsyncTimeout.exit(AsyncTimeout.java:285)
at okio.AsyncTimeout$2.read(AsyncTimeout.java:241)
at okio.RealBufferedSource.indexOf(RealBufferedSource.java:354)
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:226)
at okhttp3.internal.http1.Http1Codec.readHeaderLine(Http1Codec.java:215)
at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189)
at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:254)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:200)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.net.SocketException: Socket closed
at java.net.SocketInputStream.read(SocketInputStream.java:203)
at java.net.SocketInputStream.read(SocketInputStream.java:139)
at okio.Okio$2.read(Okio.java:140)
at okio.AsyncTimeout$2.read(AsyncTimeout.java:237)
at okio.RealBufferedSource.indexOf(RealBufferedSource.java:354)Â
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:226)Â
at okhttp3.internal.http1.Http1Codec.readHeaderLine(Http1Codec.java:215)Â
at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:189)Â
at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)Â
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)Â
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)Â
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)Â
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)Â
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)Â
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)Â
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)Â
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)Â
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)Â
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)Â
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)Â
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)Â
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:254)Â
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:200)Â
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)Â
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)Â
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)Â
at java.lang.Thread.run(Thread.java:764)Â
Upvotes: 2
Views: 7532
Reputation: 1
If there are any null values for the keys then it throws com.google.gson.JsonIOException:
Eg : { "request": null, "body": "{"status": "passed","message": "[]"}" "exception": null }
Assign valid values for the keys
Upvotes: 0
Reputation: 63
The problem I had when I received this error message "JSON document was not fully consumed" was simply that there was extraneous text at the end of the JSON String.
In my case I manually added text to the end of the valid JSON String to "test" error handling and retry logic on a Friday and then Monday forgot that I added this temporary code!
Upvotes: 2
Reputation: 55
You should put fields in model class in same order as in json
@SerializedName("isSend")
private boolean isSend;
@SerializedName("code")
private int code;
@SerializedName("email")
private boolean email;
Upvotes: 0