Service methods cannot return a void. modify

This is my interface method. I call this function, but the application crashes with this exception:

Called: java.lang.IllegalArgumentException: service methods cannot return void. for the RestInterface.getOtp method

//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 call 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) { } }); 
+38
retrofit
Aug 31 '15 at 12:40
source share
4 answers

Asynchronous versions 1.9 and 2.0 have a difference.

/ * Synchronous in version 1.9 * /

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

/ * Asynchronous in version 1.9 * /

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

But on Retrofit 2.0, it's a lot easier, since you can only declare with one template.

 /* Retrofit 2.0 */ public interface APIService { @POST("/list") Call<Repo> loadRepo(); } 

// Synchronous call when retrofitting 2.0

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

// Asynchronous call when retrofitting 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); } }); 
+75
Sep 16 '15 at 16:34
source share

You can always do:

 @POST("/endpoint") Call<Void> postSomething(); 

EDIT:

If you use RxJava starting from 1.1.1 you can use the Completable class.

+18
Dec 17 '15 at 17:54
source share

https://github.com/square/retrofit/issues/297

Please follow this link.

"All interface declarations will be required to return an object through which all interaction will take place. The behavior of this object will be similar to the future and will be typical typed (T) for the type of success response."

 @GET("/foo") Call<Foo> getFoo(); 

Based on the new version of Retrofit 2.0.0, you cannot specify the return type as void to make it asynchronous

according to the code inside retrofit ( https://github.com/square/retrofit/blob/master/retrofit/src/main/java/retrofit/MethodHandler.java ) it will show an exception if you try the previous implementation using 2.0 .0 beta

 if (returnType == void.class) { throw Utils.methodError(method, "Service methods cannot return void."); } 
+4
03 Sep '15 at 8:45
source share

Based on your classes, it looks like you are using Retrofit 2.0.0, which is currently in beta. I think using voids in your service method is no longer allowed. Instead, return a Call , which you can queue to make a network call asynchronously.

Alternatively, drop your library to Retrofit 1.9.0 and replace the Retrofit class with RestAdapter.

+2
02 Sep '15 at 3:06 on
source share



All Articles