我正在使用改造进行后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)
Run Code Online (Sandbox Code Playgroud)
试图访问后api
@POST("/service/v2/auth/ip-address")
rx.Observable<AuthState> verifyIP();
Run Code Online (Sandbox Code Playgroud)
实际的api电话
LoginService service = CKRestClient.get().create(LoginService.class);
service.verifyIP().observeOn(AndroidSchedulers.mainThread()).subscribe(
new Action1<AuthState>() {
@Override
public void call(AuthState authState) {
}
});
});
Run Code Online (Sandbox Code Playgroud)
Bar*_*ers 17
看起来Retrofit希望POST请求具有有效负载.它已经存在问题:https://github.com/square/retrofit/issues/854
作为一种解决方法,您可以执行以下操作:
@POST("/service/v2/auth/ip-address")
rx.Observable<AuthState> verifyIP(@Body Object dummy);
Run Code Online (Sandbox Code Playgroud)
然后做:
LoginService service = CKRestClient.get().create(LoginService.class);
service.verifyIP(null).observeOn(AndroidSchedulers.mainThread()).subscribe(
new Action1<AuthState>() {
@Override
public void call(AuthState authState) {
// ...
}
});
});
Run Code Online (Sandbox Code Playgroud)
或者,如果service.verifyIP(null)抛出NPE,请更换service.verifyIP("")或类似.
Def*_*era 13
我通过将@Query替换为@Field解决了这个问题,方法如下:
不工作的代码:
@POST("/my/url/path")
Result postToServer(
@Query("user_name") String userName);
Run Code Online (Sandbox Code Playgroud)
工作范例:
@FormUrlEncoded
@POST("/my/url/path")
Result postToServer(
@Field("user_name") String userName);
Run Code Online (Sandbox Code Playgroud)
对于没有任何字段的方法,我必须添加如下所示的空字符串
Result postToServer(@Path("my_path") String myPath,
@Body String emptyString);
Run Code Online (Sandbox Code Playgroud)
称之为"":
restClient.postToServer(myPath, "");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15963 次 |
| 最近记录: |