Service methods cannot return void. retrofit

前端 未结 4 1703
执念已碎
执念已碎 2020-12-09 01:30

This is my method in Interface. I am calling this function but app crash with this exception:

Caused by: java.lang.IllegalArgumentException: Service m

4条回答
  •  一个人的身影
    2020-12-09 02:10

    There is difference in Asynchronous in Retrofit 1.9 and 2.0

    /* Synchronous in Retrofit 1.9 */

    public interface APIService {
    
    @POST("/list")
    Repo loadRepo();
    
    }
    

    /* Asynchronous in Retrofit 1.9 */

    public interface APIService {
    
    @POST("/list")
    void loadRepo(Callback cb);
    
    }
    

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

    // Synchronous Call in Retrofit 2.0

    Call call = service.loadRepo();
    Repo repo = call.execute();
    

    // Asynchronous Call in Retrofit 2.0

    Call call = service.loadRepo();
    call.enqueue(new Callback() {
    @Override
    public void onResponse(Response response) {
    
       Log.d("CallBack", " response is " + response);
    }
    
    @Override
    public void onFailure(Throwable t) {
    
      Log.d("CallBack", " Throwable is " +t);
    }
    });
    

提交回复
热议问题