Mr.Z Mr.Z - 7 months ago 50
JSON Question

Get Retrofit Response as String

I have an api that gives result in something like:

if okay :

OK ID:1234567

not okay:
2 <br>(Please Type something)

Now i want to receive this in my android app using retrofit , one way i can think of is getting response in
and manually checking about error or not error scenario , But i cannot find a better way to do this :

These SO question are not explaining the solution enough to replicate:
Q1 , Q2 , Q3


There are two ways:

  • Out of the box Retrofit supports using OkHttp's ResponseBody type. ResponseBody is basically an abstraction on "just bytes" and has a method called string() which will consume the body as a String.

  • You can add the converters-scalars artifact and add the ScalarsConverterFactory to your instance which will allow using String as your response type.