改造-http 405响应?

时间:2017-01-07 04:58:53

标签: android django rx-java retrofit2 http-status-code-405

我正在尝试创建一个登录页面,我正在使用django作为后端。使用的身份验证是基于令牌的。我试图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,我正在使用基于令牌的身份验证。它完全适用于投掷。

request in hurl

response in hurl

这是木材记录。

retrofit2.adapter.rxjava.HttpException: HTTP 405 Method Not Allowed
                                                                        at retrofit2.adapter.rxjava.OperatorMapResponseToBodyOrError$1.onNext(OperatorMapResponseToBodyOrError.java:43)
                                                                        at retrofit2.adapter.rxjava.OperatorMapResponseToBodyOrError$1.onNext(OperatorMapResponseToBodyOrError.java:38)
                                                                        at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$RequestArbiter.request(RxJavaCallAdapterFactory.java:173)
                                                                        at rx.internal.operators.OperatorSubscribeOn$1$1$1.request(OperatorSubscribeOn.java:80)
                                                                        at rx.Subscriber.setProducer(Subscriber.java:211)
                                                                        at rx.internal.operators.OperatorSubscribeOn$1$1.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$1.call(OperatorSubscribeOn.java:94)
                                                                        at rx.internal.schedulers.CachedThreadScheduler$EventLoopWorker$1.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)

1 个答案:

答案 0 :(得分:0)

我在程序中发现了这个错误。实际上这是我提供给Restprovider类的Url的问题。它应该是 &#34; /api-token-auth/&#34; 只是一个斜线使所有的差异。感谢所有尝试过的人。