Zulqurnain Jutt
Zulqurnain Jutt

Reputation: 1093

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

Upvotes: 0

Views: 1212

Answers (2)

Mikhail
Mikhail

Reputation: 3305

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.

Upvotes: 0

Jake Wharton
Jake Wharton

Reputation: 76075

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.

Upvotes: 4

Related Questions