In the below code Im syncing my data to the server but the app keeps crashing with this message Exception is: java.lang.IllegalStateException: Cannot read raw response body of a converted body. I am unable to fix it. What is causing the crash and what is the solution?
private void uploadToServer(String date, String userid, JSONArray attachment, String expense, String ad_user, String ad_pass, String versionCode, String apiVersion, String dcrSync, String urlParameters) {
RequestBody description = RequestBody.create(MediaType.parse("text/plain"), attachment.toString());
APIinterface apIinterface = RetrofitClient.getRetrofitClient(getApplicationContext()).create(APIinterface.class);
final Call<ResponseBody> call = apIinterface.uploadExpenseData(Datafields.tag_url.get("expense_sync").toString(), date, userid, attachment.toString(), expense, ad_user, ad_pass, versionCode, apiVersion, dcrSync);
Log.e("attachement", attachment.toString());
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Log.e("syncObjetc", String.valueOf(response));
if (response.isSuccessful()) {
ResponseBody responseBody = response.body();
String url = response.raw().request().url().toString();
ResponseBody body = response.raw().body();
try {
Toast.makeText(Sync.this, responseBody.string(), Toast.LENGTH_SHORT).show();
Toast.makeText(Sync.this, body.string(), Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
JSONObject jsonObject = new JSONObject(response.body().string());
JSONObject messageObj = jsonObject.getJSONObject("message");
JSONObject codeObject = jsonObject.getJSONObject("code");
JSONObject syncObjetc = jsonObject.getJSONObject("synchData");
String message = messageObj.getString("message");
int code = codeObject.getInt("code");
String date = syncObjetc.getString("date");
Log.e("String message ", message);
Log.e("syncObjetc", String.valueOf(syncObjetc));
Log.e("resbody ", responseBody.string());
altDialog.setMessage(message);
altDialog.setNegativeButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(Sync.this, Sync.class));
overridePendingTransition(0, 0);
}
});
altDialog.show();
if (code == 0) {
syncExpense(date);
}
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
progressDialog.dismiss();
Toast.makeText(Sync.this, "Sync Failed", Toast.LENGTH_LONG).show();
Log.e("Faliure", t.getMessage());
}
});
}
Below is the logcat:
java.lang.IllegalStateException: Cannot read raw response body of a converted body.
at retrofit2.OkHttpCall$NoContentResponseBody.source(OkHttpCall.java:274)
at okhttp3.ResponseBody.string(ResponseBody.java:173)
at com.anantfso.glenmark_fso_demo.Sync$19.onResponse(Sync.java:1898)
at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1$1.run(DefaultCallAdapterFactory.java:83)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
2023-04-24 12:58:48.459 9893-9893/com.anantfso.glenmark_fso_demo W/System.err: java.lang.IllegalStateException: Cannot read raw response body of a converted body.
2023-04-24 12:58:48.459 9893-9893/com.anantfso.glenmark_fso_demo W/System.err: at retrofit2.OkHttpCall$NoContentResponseBody.source(OkHttpCall.java:274)
2023-04-24 12:58:48.459 9893-9893/com.anantfso.glenmark_fso_demo W/System.err: at okhttp3.ResponseBody.string(ResponseBody.java:173)
2023-04-24 12:58:48.459 9893-9893/com.anantfso.glenmark_fso_demo W/System.err: at com.anantfso.glenmark_fso_demo.Sync$19.onResponse(Sync.java:1898)
2023-04-24 12:58:48.459 9893-9893/com.anantfso.glenmark_fso_demo W/System.err: at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1$1.run(DefaultCallAdapterFactory.java:83)
2023-04-24 12:58:48.460 9893-9893/com.anantfso.glenmark_fso_demo W/System.err: at android.os.Handler.handleCallback(Handler.java:883)
2023-04-24 12:58:48.460 9893-9893/com.anantfso.glenmark_fso_demo W/System.err: at android.os.Handler.dispatchMessage(Handler.java:100)
2023-04-24 12:58:48.460 9893-9893/com.anantfso.glenmark_fso_demo W/System.err: at android.os.Looper.loop(Looper.java:214)
2023-04-24 12:58:48.460 9893-9893/com.anantfso.glenmark_fso_demo W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7356)
2023-04-24 12:58:48.460 9893-9893/com.anantfso.glenmark_fso_demo W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2023-04-24 12:58:48.460 9893-9893/com.anantfso.glenmark_fso_demo W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
2023-04-24 12:58:48.460 9893-9893/com.anantfso.glenmark_fso_demo W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
You are attempting to read ResponseBody
multiple times and that's not allowed. as you can do this only once, so When you call responseBody.string()
and body.string()
, you read the response body twice, and the second one is causing the IllegalStateException
. If you need to access it more time then you must do a copy first and then access that copy instead of original response.