改装获得字符串响应 [英] Retrofit get String response

查看:93
本文介绍了改装获得字符串响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用Retrofit库仅接收String响应?我遇到一种情况,需要在链接上添加查询,以便链接看起来像: 本地主机//注册?handle = SomeID

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是整数,当我这样做时,我将从服务器接收包含20个字符的字符串格式的响应.我如何获得该回复?改造甚至可以处理非Json格式的响应吗?

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?

我也应该如何创建它:

@GET("/api/UserMainInformations") 呼叫getUserMainInfo();

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

这是来自其他一些调用的示例,但是现在我没有任何模型可以发送它,因为我只在Query上添加了它.我应该在Call<>;

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

推荐答案

您可以从api获取响应并将其转换为这样的字符串:

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();
    }
});

这篇关于改装获得字符串响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆