user3015410
user3015410

Reputation: 29

Retrofit Error: Service methods cannot return void message

Happy new year to all,i'm working on an app that retrieves list of of objects from from an JSON array and convert it to POJO class. However i recieve this error logcat with retrofit2 in android+Eclipse when i run the app as follows:

12-31 06:59:32.405: E/test(31013): Exception
12-31 06:59:32.405: E/AndroidRuntime(31013): FATAL EXCEPTION: main
12-31 06:59:32.405: E/AndroidRuntime(31013): Process: com.nickSoft.unics_alpha, PID: 31013
12-31 06:59:32.405: E/AndroidRuntime(31013): java.lang.IllegalArgumentException: Service methods cannot return void.
12-31 06:59:32.405: E/AndroidRuntime(31013):     for method UnicsAgencyApi.getStreams
12-31 06:59:32.405: E/AndroidRuntime(31013):    at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:720)
12-31 06:59:32.405: E/AndroidRuntime(31013):    at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:711)
12-31 06:59:32.405: E/AndroidRuntime(31013):    at retrofit2.ServiceMethod$Builder.createCallAdapter(ServiceMethod.java:228)
12-31 06:59:32.405: E/AndroidRuntime(31013):    at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:160)
12-31 06:59:32.405: E/AndroidRuntime(31013):    at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:166)
12-31 06:59:32.405: E/AndroidRuntime(31013):    at retrofit2.Retrofit$1.invoke(Retrofit.java:145)
12-31 06:59:32.405: E/AndroidRuntime(31013):    at $Proxy1.getStreams(Native Method)
12-31 06:59:32.405: E/AndroidRuntime(31013):    at com.nickSoft.unics_alpha.Homepage.DownloadAgencyData(Homepage.java:582)
12-31 06:59:32.405: E/AndroidRuntime(31013):    at com.nickSoft.unics_alpha.Homepage.ShowAllUnicsAgencyInfo(Homepage.java:548)
12-31 06:59:32.405: E/AndroidRuntime(31013):    at com.nickSoft.unics_alpha.Homepage.onOptionsItemSelected(Homepage.java:394)
12-31 06:59:32.405: E/AndroidRuntime(31013):    at android.app.Activity.onMenuItemSelected(Activity.java:2600)
12-31 06:59:32.405: E/AndroidRuntime(31013):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1016)
12-31 06:59:32.405: E/AndroidRuntime(31013):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
12-31 06:59:32.405: E/AndroidRuntime(31013):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
12-31 06:59:32.405: E/AndroidRuntime(31013):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
12-31 06:59:32.405: E/AndroidRuntime(31013):    at com.android.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:177)
12-31 06:59:32.405: E/AndroidRuntime(31013):    at android.widget.AdapterView.performItemClick(AdapterView.java:299)
12-31 06:59:32.405: E/AndroidRuntime(31013):    at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
12-31 06:59:32.405: E/AndroidRuntime(31013):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
12-31 06:59:32.405: E/AndroidRuntime(31013):    at android.widget.AbsListView$3.run(AbsListView.java:3638)
12-31 06:59:32.405: E/AndroidRuntime(31013):    at android.os.Handler.handleCallback(Handler.java:733)
12-31 06:59:32.405: E/AndroidRuntime(31013):    at android.os.Handler.dispatchMessage(Handler.java:95)
12-31 06:59:32.405: E/AndroidRuntime(31013):    at android.os.Looper.loop(Looper.java:136)
12-31 06:59:32.405: E/AndroidRuntime(31013):    at android.app.ActivityThread.main(ActivityThread.java:5136)
12-31 06:59:32.405: E/AndroidRuntime(31013):    at java.lang.reflect.Method.invokeNative(Native Method)
12-31 06:59:32.405: E/AndroidRuntime(31013):    at java.lang.reflect.Method.invoke(Method.java:515)
12-31 06:59:32.405: E/AndroidRuntime(31013):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:819)
12-31 06:59:32.405: E/AndroidRuntime(31013):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)
12-31 06:59:32.405: E/AndroidRuntime(31013):    at dalvik.system.NativeStart.main(Native Method)

My Api declaration code looks like this:

public static UnicsAgencyApi getUnicsAgencyApi() {

        if (sUnicsAgencyApi == null) {
            retrofit = new Retrofit.Builder().baseUrl(ENDPOINT_URL).addConverterFactory(GsonConverterFactory.create())
                    .build();
            sUnicsAgencyApi = retrofit.create(UnicsAgencyApi.class);
        }
        return sUnicsAgencyApi;
    }

    public interface UnicsAgencyApi {

        @GET("api/uconnectservice/AllAgency")
        **void getStreams(Callback<List<AgencyModel>> callback);**
    }

Here is where i make the call:

RestApi.getUnicsAgencyApi().getStreams(new Callback<List<AgencyModel>>() {

            @Override
            public void onFailure(Call<List<AgencyModel>> arg0, Throwable arg1) {
                // TODO Auto-generated method stub

                Log.e("Error in parsing", arg0.toString());
            }

            @Override
            public void onResponse(Call<List<AgencyModel>> AgencyModelData, Response<List<AgencyModel>> response) {
                // TODO Auto-generated method stub

                // ADD TO List here!!!!!!!!
                mstreamData.addAll(response.body());
                Log.e("Response", response.body().toString());
            }

        });

The origin of the error show in Logat is:

**void getStreams(Callback<List<AgencyModel>> callback);**

Please why am i getting this error or is there something i'm doing wrong ,please any guidance to solve this problem is greatly appreciated. cheers

Upvotes: 0

Views: 1849

Answers (1)

iagreen
iagreen

Reputation: 32026

You are using V1-like syntax with retrofit2. There are no longer callbacks as parameters, you return a call instead. --

@GET("api/uconnectservice/AllAgency")
Call<List<AgencyModel>> getStreams();

You'll need to update how you call it, too. For asynchronous calls, use enqueue on the returned call --

RestApi.getUnicsAgencyApi().getStreams().enqueue(new Callback<List<AgencyModel>>() {

            @Override
            public void onFailure(Call<List<AgencyModel>> arg0, Throwable arg1) {
                // TODO Auto-generated method stub

                Log.e("Error in parsing", arg0.toString());
            }

            @Override
            public void onResponse(Call<List<AgencyModel>> AgencyModelData, Response<List<AgencyModel>> response) {
                // TODO Auto-generated method stub

                // ADD TO List here!!!!!!!!
                mstreamData.addAll(response.body());
                Log.e("Response", response.body().toString());
            }

        });

Upvotes: 1

Related Questions