Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get simple json object using retrofit 2

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.

like image 865
mrdaliri Avatar asked Sep 10 '15 22:09

mrdaliri


People also ask

What is retrofit JSON?

Retrofit is a REST Client for Java and Android allowing to retrieve and upload JSON (or other structured data) via a REST based You can configure which converters are used for the data serialization, example GSON for JSON.


1 Answers

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

like image 110
Amandeep Rohila Avatar answered Sep 21 '22 10:09

Amandeep Rohila