尝试运行我的Retrofit GET调用时出现以下错误,并且回调发生错误:
myClass
此模式适用于我的登录,但我是Retrofit的新手,似乎无法在此找到确切的问题。
我的IMyClient.Java代码:
E/roboguice: Throwable caught during background processing
java.lang.IllegalArgumentException: IMyClient.getUser: Must have return type or Callback as last argument, not both.
at retrofit.RestMethodInfo.methodError(RestMethodInfo.java:107)
at retrofit.RestMethodInfo.parseResponseType(RestMethodInfo.java:267)
at retrofit.RestMethodInfo.<init>(RestMethodInfo.java:97)
at retrofit.RestAdapter.getMethodInfo(RestAdapter.java:213)
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:236)
at java.lang.reflect.Proxy.invoke(Proxy.java:393)
at $Proxy15.getUser(Unknown Source)
at client.MyClient.getUser(MyClient.java:111)
at task.UserProfileTask.call(UserProfileTask.java:41)
at task.UserProfileTask.call(UserProfileTask.java:22)
at roboguice.util.SafeAsyncTask$SafeAsyncTaskAndroidCallable.doInBackground(SafeAsyncTask.java:163)
at roboguice.util.AndroidCallableWrapper.doDoInBackgroundThread(AndroidCallableWrapper.java:113)
at roboguice.util.AndroidCallableWrapper.run(AndroidCallableWrapper.java:41)
at roboguice.util.AndroidCallable.run(AndroidCallable.java:20)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:580)
at roboguice.util.AndroidCallable.<init>(AndroidCallable.java:12)
at roboguice.util.SafeAsyncTask$SafeAsyncTaskAndroidCallable.<init>(SafeAsyncTask.java:160)
at roboguice.util.SafeAsyncTask.newTask(SafeAsyncTask.java:156)
at roboguice.util.SafeAsyncTask.future(SafeAsyncTask.java:66)
at roboguice.util.SafeAsyncTask.execute(SafeAsyncTask.java:94)
at roboguice.util.SafeAsyncTask.execute(SafeAsyncTask.java:90)
at ui.fragment.AccountFragment.onViewCreated(AccountFragment.java:122)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1127)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:604)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
at roboguice.activity.RoboActionBarActivity.onStart(RoboActionBarActivity.java:106)
at ui.activity.BaseActivity.onStart(BaseActivity.java:270)
at ui.activity.AccountActivity.onStart(AccountActivity.java:60)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
at android.app.Activity.performStart(Activity.java:6253)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
在添加回调之前,这工作正常,但当然我需要能够访问响应来解析Json。
在我的MyClient中,我创建了一个IMyClient实例,它看起来像这样:
@GET("/persons")
List<UserProfile> getUser(@Header("Authorization") String token, Callback<List<UserProfile>> callback);
我的回调是针对我在Retrofit 1.9文档中找到的以及它如何使用我的登录信息而设置的:
public List<UserProfile> getUser(String token, Callback<List<UserProfile>> callback) {
return client.getUser(token, callback);
}
在这个Callback中,我希望能够访问响应并从中获取JSON。 UserProfile是我用来将所有JSON对象设置为的模型。
我不确定为什么它正在使用登录但不在这里工作,非常感谢任何帮助!
答案 0 :(得分:3)
阅读提供的stacktrace:
必须有返回类型或Callback作为最后一个参数,而不是两者。
如果您想要请求异步
,这意味着这一点@GET("/persons")
void getUser(@Header("Authorization") String token, Callback<List<UserProfile>> callback);
或者如果你想让它同步
@GET("/persons")
List<UserProfile> getUser(@Header("Authorization") String token);