Retrofit get String response

user6038288 picture user6038288 · Jun 2, 2016 · Viewed 26.1k times · Source

Is it possible to recieve only String response using Retrofit library? I have a situation where I need to add Query on my link so that link is looking like : localhost//Register?handle=SomeID

SomeID is integer and when I do that I will receive response from server in string format that consist of 20 chars. How can I get that response? Can Retrofit even handle response that is not in Json format?

Also how should I create this :

@GET("/api/UserMainInformations") Call getUserMainInfo();

That's example from some other call but now I won't have any model to send it cuz I only add it on Query. What should i put in Call<> ;

Answer

SaravInfern picture SaravInfern · Jun 2, 2016

You can get the response from api and convert it to string like this:

 public interface RetrofitService{
        @GET("/users")
        Call<ResponseBody> listRepos();//function to call api
    }

    RetrofitService service = retrofit.create(RetrofitService.class);
    Call<ResponseBody> result = service.listRepos(username);
    result.enqueue(new Callback<ResponseBody>() {

    @Override
    public void onResponse(Response<ResponseBody> response) {
        try {
            System.out.println(response.body().string());//convert reponse to string
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onFailure(Throwable t) {
        e.printStackTrace();
    }
});