Retrofit-http 405响应?

Retrofit-http 405 response?

我正在尝试创建一个登录页面,我正在使用 django 作为后端。使用的身份验证是 token-based。我已经尝试 POST 用户名和密码,这给我一个 405 http 代码错误,这不是我预期的情况。而且它还说它不期待我会做的 GET 方法。它在 https://www.hurl.it/ 中以正确的方式工作。我已经在此处发布了图像和代码。请帮忙!

LoginPresenter.java

subscription = RxUtil.io(restProvider.authenticate(userId, password))
                .subscribe(new Subscriber<JsonObject>() {
                    @Override
                    public void onCompleted() {
                        getMvpView().showProgress(false);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Timber.e(e);
                        getMvpView().showProgress(false);
                    }

                    @Override
                    public void onNext(JsonObject jsonObject) {
                        Timber.d(jsonObject.toString());
                    }
                });

RestProvider.java

public interface RestProvider {

    @FormUrlEncoded
    @POST("/api-token-auth")
    Observable<JsonObject> authenticate(@Field("username") String user, @Field("password") String pass);

}

拦截器日志是

POST http://mylink.com/mypath http/1.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 52
username=myusername&password=mypassword
--> END POST (52-byte body)
<-- 405 Method Not Allowed http://mylink.com/mypath (84ms)
Date: Sat, 07 Jan 2017 04:36:48 GMT
Server: Apache/2.4.18 (Ubuntu)
Vary: Cookie
X-Frame-Options: SAMEORIGIN
Allow: POST, OPTIONS
Keep-Alive: timeout=5, max=99
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: application/json
{"detail":"Method \"GET\" not allowed."}
<-- END HTTP (40-byte body)

后端是 Django,我使用的是基于令牌的身份验证。它在 hurl.it 中完美运行。

这是原木。

retrofit2.adapter.rxjava.HttpException: HTTP 405 Method Not Allowed
                                                                        at retrofit2.adapter.rxjava.OperatorMapResponseToBodyOrError.onNext(OperatorMapResponseToBodyOrError.java:43)
                                                                        at retrofit2.adapter.rxjava.OperatorMapResponseToBodyOrError.onNext(OperatorMapResponseToBodyOrError.java:38)
                                                                        at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$RequestArbiter.request(RxJavaCallAdapterFactory.java:173)
                                                                        at rx.internal.operators.OperatorSubscribeOn.request(OperatorSubscribeOn.java:80)
                                                                        at rx.Subscriber.setProducer(Subscriber.java:211)
                                                                        at rx.internal.operators.OperatorSubscribeOn.setProducer(OperatorSubscribeOn.java:76)
                                                                        at rx.Subscriber.setProducer(Subscriber.java:205)
                                                                        at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:152)
                                                                        at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:138)
                                                                        at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
                                                                        at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
                                                                        at rx.Observable.unsafeSubscribe(Observable.java:10142)
                                                                        at rx.internal.operators.OperatorSubscribeOn.call(OperatorSubscribeOn.java:94)
                                                                        at rx.internal.schedulers.CachedThreadScheduler$EventLoopWorker.call(CachedThreadScheduler.java:230)
                                                                        at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
                                                                        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
                                                                        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:272)
                                                                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                                                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                                                        at java.lang.Thread.run(Thread.java:761)

我在我的程序中发现了错误。实际上这是我的 Url 提供给 Restprovider class 的问题。本来应该是 “/api-token-auth/” 只是一个斜线让一切变得不同。感谢所有尝试过的人。