org.json.JSONException: End of input at character 0 of
For some weird reason if you use .string()
>1 time, response turns out to be empty. Try to use it only once when parsing Retrofit response.
Someone, who knows why it happens like this, please improve my answer.
Answer : Response object should of Generic type - ResponseBody
.
See below Correct code for reference.
Now response.body()
method will return object ResponseBody
i.e.
ResponseBody rb = response.body();
rb.string();
Here ResponseBody have method string()
which returns String
object but internally string()
method calls Util.closeQuietly(source);
which makes response empty once method string()
gets called.
Just remove Log.d(TAG, response.body().string());
and follow below code.
Reference - okhttp3.ResponseBody.java
error : org.json.JSONException: End of input at character 0
Correct code :
@Override
public void onResponse(Call call, Response<ResponseBody> response) throws IOException {
if (response.isSuccessful()) {
String remoteResponse=response.body().string();
Log.d(TAG, remoteResponse);
try {
JSONObject forecast = new JSONObject(remoteResponse);
} catch (JSONException e) {
e.printStackTrace();
}
}
}