Mithu
Mithu

Reputation: 655

Retrofit Service methods cannot return void

I have below method in my Api Interface.I am trying to post and return data from the same page

   public interface ApiInterface {

   @FormUrlEncoded // annotation used in POST type requests

   @POST("/pagee1.php")     // API's endpoints

   public void registration(@Field("param1") String param1,
                         @Field("param2") String param2,
                         @Field("param3") String param3,
                         @Field("param4") String param4,
                         @Field("param5") String param5,
                         Callback<UserListResponse> callback);

@GET("/pagee1.php")
public void getUsersList
Callback<List<UserListResponse>> callback);

}

And this is my post_Data method using above ApiInterface registration

 private void Post_Data() {

    Api.getClient().registration("value1", "value2", "value3", "value4", "value5", new Callback<UserListResponse>() {
        @Override
        public void onResponse(Call<UserListResponse> call, retrofit2.Response<UserListResponse> response) {
            progressDialog.dismiss(); //dismiss progress dialog
            getUserListData();
        }

        @Override
        public void onFailure(Call<UserListResponse> call, Throwable t) {
            Toast.makeText(getContext(), t.toString(), Toast.LENGTH_LONG).show();
            Log.e("Retrofit_callback Error", t.toString());

        }
    });

}

But it is giving Error on Api.getClient().registration position and below is the error code

java.lang.IllegalArgumentException: Service methods cannot return void.
    for method ApiInterface.getUsersList

what's wrong I am doing

Upvotes: 0

Views: 832

Answers (1)

Biscuit
Biscuit

Reputation: 5247

You need to change to this :

public interface ApiInterface {

    @FormUrlEncoded // annotation used in POST type requests
    @POST("/pagee1.php")     // API's endpoints
    public Call<UserListResponse> registration(@Field("param1") String param1,
                         @Field("param2") String param2,
                             @Field("param3") String param3,
                             @Field("param4") String param4,
                             @Field("param5") String param5);

    @GET("/pagee1.php")
    public Call<List<UserListResponse>> getUsersList();

}

Your calls a returning something which is Call<YourEntity>. Have a look at this tutorial for a better understanding of Retrofit.

Upvotes: 1

Related Questions