改造错误:服务方法无法返回void消息

时间:2016-12-31 06:38:41

标签: java android eclipse list retrofit2

祝大家新年快乐,我正在开发一个应用程序,它从JSON数组中检索对象列表并将其转换为POJO类。但是当我按照以下方式运行应用程序时,我在android + Eclipse中使用retrofit2接收此错误logcat:

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)

我的Api声明代码如下所示:

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);**
    }

这是我打电话的地方:

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

        });

Logat中显示的错误的来源是:

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

请问为什么我会收到此错误或有什么我做错了,请非常感谢任何解决此问题的指导。                                           欢呼声

1 个答案:

答案 0 :(得分:1)

您正在使用类似V1的语法和retrofit2。不再有回调作为参数,而是返回一个调用。 -

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

您还需要更新调用方式。对于异步调用,请在返回的调用上使用enqueue -

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

        });