服务方法不能返回 void.改造 [英] Service methods cannot return void. retrofit
问题描述
这是我在接口中的方法.我正在调用此函数,但应用程序因此异常而崩溃:
This is my method in Interface. I am calling this function but app crash with this exception:
Caused by: java.lang.IllegalArgumentException: Service Methods cannot返回无效.对于方法 RestInterface.getOtp
Caused by: java.lang.IllegalArgumentException: Service methods cannot return void. for method RestInterface.getOtp
//post method to get otp for login
@FormUrlEncoded
@POST("/store_login")
void getOtp(@Header("YOUR_APIKEY") String apikey, @Header("YOUR_VERSION") String appversion,
@Header("YOUR_VERSION") String confiver, @Field("mobile") String number, Callback<Model> cb);
这是我调用这个函数的代码
And this is the code where I am calling this function
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_URL)
.build();
RestInterface restApi = retrofit.create(RestInterface.class);
restApi.getOtp("andapikey", "1.0", "1.0", "45545845454", new Callback<Model>() {
@Override
public void onResponse(Response<Model> response) {
}
@Override
public void onFailure(Throwable t) {
}
});
推荐答案
Retrofit 1.9 和 2.0 中异步有区别
There is difference in Asynchronous in Retrofit 1.9 and 2.0
/* 在 Retrofit 1.9 中同步 */
/* Synchronous in Retrofit 1.9 */
public interface APIService {
@POST("/list")
Repo loadRepo();
}
/* Retrofit 1.9 中的异步 */
/* Asynchronous in Retrofit 1.9 */
public interface APIService {
@POST("/list")
void loadRepo(Callback<Repo> cb);
}
但是在 Retrofit 2.0 上,它要简单得多,因为您只需使用一个模式即可声明
But on Retrofit 2.0, it is far more simple since you can declare with only just a single pattern
/* Retrofit 2.0 */
public interface APIService {
@POST("/list")
Call<Repo> loadRepo();
}
//Retrofit 2.0 中的同步调用
// Synchronous Call in Retrofit 2.0
Call<Repo> call = service.loadRepo();
Repo repo = call.execute();
//Retrofit 2.0 中的异步调用
// Asynchronous Call in Retrofit 2.0
Call<Repo> call = service.loadRepo();
call.enqueue(new Callback<Repo>() {
@Override
public void onResponse(Response<Repo> response) {
Log.d("CallBack", " response is " + response);
}
@Override
public void onFailure(Throwable t) {
Log.d("CallBack", " Throwable is " +t);
}
});
这篇关于服务方法不能返回 void.改造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!