Retrofit get String response

前端 未结 2 402
猫巷女王i
猫巷女王i 2020-12-11 01:09

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

相关标签:
2条回答
  • 2020-12-11 01:19

    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();
        }
    });
    
    0 讨论(0)
  • 2020-12-11 01:38

    Try this:

    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.

    0 讨论(0)
提交回复
热议问题