gpt4 book ai didi

android - Caused by : retrofit. RetrofitError: 方法POST必须有一个请求体

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:32:48 28 4
gpt4 key购买 nike

我正在使用改造来进行 post api 调用,在尝试到达端点时出现以下错误。

     Caused by: rx.exceptions.OnErrorNotImplementedException: method POST must have a request body.
at rx.Observable$30.onError(Observable.java:7334)
at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:154)
at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:111)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.pollQueue(OperatorObserveOn.java:197)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber$2.call(OperatorObserveOn.java:173)
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at
Caused by: java.lang.IllegalArgumentException: method POST must have a request body.
at com.squareup.okhttp.Request$Builder.method(Request.java:236)
at retrofit.client.OkClient.createRequest(OkClient.java:59)
at retrofit.client.OkClient.execute(OkClient.java:53)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:326)

尝试访问一个post api

 @POST("/service/v2/auth/ip-address")
rx.Observable<AuthState> verifyIP();

实际的api调用

LoginService service = CKRestClient.get().create(LoginService.class);
service.verifyIP().observeOn(AndroidSchedulers.mainThread()).subscribe(
new Action1<AuthState>() {
@Override
public void call(AuthState authState) {

}
});
});

最佳答案

看起来 Retrofit 希望 POST 请求具有负载。它已经存在问题:https://github.com/square/retrofit/issues/854

作为变通方法,您可以这样做:

@POST("/service/v2/auth/ip-address")
rx.Observable<AuthState> verifyIP(@Body Object dummy);

然后做:

LoginService service = CKRestClient.get().create(LoginService.class);

service.verifyIP(null).observeOn(AndroidSchedulers.mainThread()).subscribe(
new Action1<AuthState>() {
@Override
public void call(AuthState authState) {
// ...
}
});
});

或者,如果 service.verifyIP(null) 抛出 NPE,将其替换为 service.verifyIP("") 或类似的。

关于android - Caused by : retrofit. RetrofitError: 方法POST必须有一个请求体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30358545/

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