【问题标题】:How to get back String as a response using retrofit?如何使用改造取回字符串作为响应?
【发布时间】:2014-11-24 05:44:42
【问题描述】:

我尝试过调用

String s=findDate();

这个 findDate() 在改造服务中被声明为

public RetrofitService{
@GET(/findDate)
String findDate()
}

这对吗?

但是当我尝试使用异步回调时,我得到了字符串 214-11-24 的响应。为什么会这样?服务器将日期作为字符串返回,例如 2014-11-24

【问题讨论】:

  • 请澄清您的问题。“我得到了”什么?请展示您的异步改造界面。
  • GreyBeardedGeek,我更新了我的问题。

标签: java json gson retrofit


【解决方案1】:

要异步使用 Retrofit,您必须在接口中声明方法,并使用额外的回调参数作为最后一个参数。

Retrofit 然后将异步操作,并在数据可用时调用您的回调。

如果您不使用回调声明该方法,它会同步运行。

【讨论】:

  • 是的,我尝试过使用回调,它正在工作。但我想不使用回调。
  • 您可以将它与回调异步一起使用,也可以不使用回调同步,这似乎对您有用。您还在寻找什么?
  • 字符串 s=findDate();它给了我改造错误。但是 findDate(new Callback){};正在工作。
【解决方案2】:

我觉得应该是 public interface RetrofitService(){ @GET(/findDate) String findDate() }

也许你可以分享RestAdapter 对象

【讨论】:

    【解决方案3】:

    它对我有用。我使用改造:2.1.0。

    API接口:

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

    API 调用:

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

    【讨论】:

      猜你喜欢
      • 2016-10-02
      • 1970-01-01
      • 1970-01-01
      • 2017-04-05
      • 1970-01-01
      • 2019-12-20
      • 1970-01-01
      • 2015-10-22
      • 2021-08-29
      相关资源
      最近更新 更多