18

My api prints a simple json object like this:

{"status":1}

How to say that retrofit (v2) pass it to gson and return an JSONObject contains this key-value pair in response?

I tried following:

Call<JSONObject> result();

But when prints response.body() in onResponse (Response<JSONObject> response) method, it returns {} which means it's empty.

mrdaliri
  • 7,148
  • 22
  • 73
  • 107

2 Answers2

44

After lots of R&D I got answer. Please find it below

Use JsonObject from package com.google.gson instead of JSONObject from package org.json

After that call Call<JsonObject> result() and in onResponse (Response<JsonObject> response) method used to call response.body() or response.body().toString(); it wil print correct Json from apiwhatever you want

Amandeep Rohila
  • 3,558
  • 2
  • 28
  • 34
1

To get org.json.JSONArray or org.json.JSONObject just create custom ConverterFactory like here: https://github.com/marcinOz/Retrofit2JSONConverterFactory

oziem
  • 470
  • 6
  • 15