0

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 String 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

Community
  • 1
  • 1
Zulqurnain Jutt
  • 1,083
  • 3
  • 15
  • 41

2 Answers2

4

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.

Jake Wharton
  • 75,598
  • 23
  • 223
  • 230
0

This is an example to the first way. Perhaps it will be helpful for some.

Api interface:

public interface APIService {
@GET("api/get_info")
    Call<ResponseBody> getInfo();//import okhttp3.ResponseBody;
}

Api call:

// Retrofit service creation code skipped here
String json = retrofitService().getInfo().execute().body().string();

It worked for me. I use retrofit:2.1.0.

Mikhail
  • 2,612
  • 3
  • 22
  • 37