1

There are only following questions asked for this issue 1, 2 but they don't solve my question.

Question:

Suppose my api at http://www.example.com/getSomething will give {"Status":"False"}.

// desired
public Apiinter{
  @GET("http://www.example.com/getSomething")
  Observable<Boolean> getSomething();
}

i want something like Observale<Boolean> as returned , without making any extra POJO on the way, how to achieve that

My Try:

i can use a POJO/model (but i don't want to) , i can return JSONObject or JSONElement , but that will make not make Observable<Boolean> as returned value

Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
Zulqurnain Jutt
  • 1,077
  • 1
  • 9
  • 21

1 Answers1

1

Truth be told, I'd still rather go with 20+ POJOs than this but you might consider adding your custom JsonDeserializer (assuming you will use GSON for parsing your API results). The deserializer i can think of looks something along these lines:

   private static class BooleanDeserializer implements JsonDeserializer<Boolean> {
        @Override
        public Boolean deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            JsonObject obj = json.getAsJsonObject();
            Set<Map.Entry<String, JsonElement>> entrySet = obj.entrySet();
            return ((JsonElement)entrySet.toArray()[0]).getAsBoolean();
        }
    }

And setting up your Gson like this:

Gson gson = new GsonBuilder()
                .registerTypeAdapter(Boolean.class, new BooleanDeserializer()).create();

And your RestAdapter:

final RestAdapter.Builder builder = new RestAdapter.Builder()
                ...
                .setConverter(gson);

You's probably want to mess around with it a little bit and of course, this saves you work only to a degree. I don't know how your APIs differ but this should be useful for cases where you may have different API structures but only one useful value.

Bjelis
  • 116
  • 7
  • you're welcome! i forgot to mention that you probably want to return Observable instead of just Boolean (I didn't want to dig into that since I'm not really proficient in rx-java) but I'm sure you'll manage. – Bjelis Oct 10 '17 at 10:00