简体   繁体   English

如何使用改型返回String作为响应?

[英]How to get back String as a response using retrofit?

i have tried by calling 我已经尝试通过打电话

String s=findDate();

this findDate() is declared in retrofit service as 此findDate()在改造服务中声明为

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

Is this correct? 这个对吗?

But i am getting response as a string 214-11-24 when i tried using Async Callback. 但是当我尝试使用异步回调时,我得到的响应是字符串214-11-24。 Why it is so? 为什么会这样呢? The server is returning date as a string like 2014-11-24 服务器以字符串形式返回日期,例如2014-11-24

To use Retrofit asynchronoisly, you must declare the method in the interface with an extra Callback parameter as the last parameter. 若要异步使用Retrofit,必须在接口中使用额外的Callback参数作为最后一个参数声明该方法。

Retrofit will then operate asynchronously, and invoke your Callback when the data is available. 然后,改造将异步运行,并在数据可用时调用您的回调。

If you do not declare the method with a Callback, it operates synchronously. 如果未使用回调声明该方法,则该方法将同步运行。

I think must be public interface RetrofitService(){ @GET(/findDate) String findDate() } 我认为必须是public interface RetrofitService(){ @GET(/findDate) String findDate() }

and maybe you could share the RestAdapter object 也许您可以共享RestAdapter对象

It worked for me. 它为我工作。 I use retrofit:2.1.0. 我使用改造:2.1.0。

Api interface: api界面:

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

Api call: api通话:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM