gpt4 book ai didi

java - Retrofit 1.9 回调错误

转载 作者:行者123 更新时间:2023-12-01 21:08:57 25 4
gpt4 key购买 nike

尝试运行 Retrofit GET 调用时出现以下错误,并且该错误发生在回调上:

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)

这种模式在我的登录中工作得很好,但我是 Retrofit 的新手,似乎无法在这里找到确切的问题。

我的 IMyClient.Java 代码:

    @GET("/persons")
List<UserProfile> getUser(@Header("Authorization") String token, Callback<List<UserProfile>> callback);

在添加回调之前,这工作得很好,但当然我需要能够访问响应来解析 Json。

在我的 MyClient 中,我创建了一个 IMyClient 实例,如下所示:

public List<UserProfile> getUser(String token, Callback<List<UserProfile>> callback) {
return client.getUser(token, callback);
}

我已经按照我在 Retrofit 1.9 文档中找到的回调进行了布局,以及它如何与我的登录一起工作:

@Override
public List<UserProfile> call() throws Exception {
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
String response = "";
client.getUser(token, new Callback<List<UserProfile>>(){

@Override
public void success(List<UserProfile> profile, Response response) {
Object data = profile;
Object data1 = response;

}

@Override
public void failure(RetrofitError error) {
OttoBusSingleton.getInstance().post(error);
}
} );

return new ArrayList<UserProfile>();
}

在此回调中,我希望能够访问响应并从中获取 JSON。 UserProfile 是我用来设置所有 JSON 对象的模型。

我不确定为什么它可以使用登录名但不能在这里工作,非常感谢任何帮助!

最佳答案

读取提供的堆栈跟踪:

Must have return type or Callback as last argument, not both.

如果你想让请求异步,这意味着这个

@GET("/persons")
void getUser(@Header("Authorization") String token, Callback<List<UserProfile>> callback);

或者如果你想让它同步

@GET("/persons")
List<UserProfile> getUser(@Header("Authorization") String token);

关于java - Retrofit 1.9 回调错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41622132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com