这是我在Interface中的方法.我正在调用此函数,但app崩溃时出现此异常:
引起:java.lang.IllegalArgumentException:服务方法不能返回void.方法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);
Run Code Online (Sandbox Code Playgroud)
这是我调用此函数的代码
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) {
}
});
Run Code Online (Sandbox Code Playgroud)
Meh*_*aji 87
在Retrofit 1.9和2.0中,Asynchronous有所不同
/*改造中的同步1.9*/
public interface APIService {
@POST("/list")
Repo loadRepo();
}
Run Code Online (Sandbox Code Playgroud)
/*Retrofit中的异步1.9*/
public interface APIService {
@POST("/list")
void loadRepo(Callback<Repo> cb);
}
Run Code Online (Sandbox Code Playgroud)
但是在Retrofit 2.0上,它更简单,因为你只能用一个模式声明
/* Retrofit 2.0 */
public interface APIService {
@POST("/list")
Call<Repo> loadRepo();
}
Run Code Online (Sandbox Code Playgroud)
// Retrofit 2.0中的同步调用
Call<Repo> call = service.loadRepo();
Repo repo = call.execute();
Run Code Online (Sandbox Code Playgroud)
// 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);
}
});
Run Code Online (Sandbox Code Playgroud)
raf*_*kob 21
你可以随时做:
@POST("/endpoint")
Call<Void> postSomething();
Run Code Online (Sandbox Code Playgroud)
编辑:
如果您使用的是RxJava,那么从1.1.1开始就可以使用Completable类.
https://github.com/square/retrofit/issues/297
请浏览此链接。
“所有接口声明都需要返回一个对象,所有交互都将通过该对象发生。该对象的行为将类似于 Future,并且对于成功响应类型将是泛型类型 (T)。 ”
@GET("/foo")
Call<Foo> getFoo();
Run Code Online (Sandbox Code Playgroud)
基于新的Retrofit 2.0.0 beta 不能将返回类型指定为void以使其异步
根据retrofit(https://github.com/square/retrofit/blob/master/retrofit/src/main/java/retrofit/MethodHandler.java)中的代码,当您尝试使用2.0之前的实现时,它将显示异常。 0 贝塔
if (returnType == void.class) {
throw Utils.methodError(method, "Service methods cannot return void.");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17319 次 |
| 最近记录: |