如何从Android中的演示者访问令牌?

时间:2017-02-02 10:59:14

标签: java android

我发布了一封电子邮件和密码,以便我收到回复。

@FormUrlEncoded
    @POST("api-token-auth/")
    Observable<AccessToken> getAccessToken(@Field("username") String email, @Field("password") String password);

然后我在演示者中运行代码。

 subscription = getAccessTokenUseCase.execute()
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<AccessToken>() {
                    @Override
                    public void onCompleted() {

                        Log.v("token",accessToken.getAccessToken());
                        sharedPrefsWrapper.putString("token",accessToken.getAccessToken());
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(AccessToken accessToken) {

                    }
                });

由于access.getAccessToken(),我得到了一个nullpointerexception。

Caused by: java.lang.NullPointerException
                                                                   at com.wyat.wyat.accounts.presenters.LoginPresenter$1.onCompleted(LoginPresenter.java:93)
                                                                   at rx.observers.SafeSubscriber.onCompleted(SafeSubscriber.java:84)
                                                                   at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.checkTerminated(OperatorObserveOn.java:272) 
                                                                   at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:207) 
                                                                   at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55) 
                                                                   at android.os.Handler.handleCallback(Handler.java:605) 
                                                                   at android.os.Handler.dispatchMessage(Handler.java:92) 
                                                                   at android.os.Looper.loop(Looper.java:137) 
                                                                   at android.app.ActivityThread.main(ActivityThread.java:4517) 
                                                                   at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                   at java.lang.reflect.Method.invoke(Method.java:511

然而,日志显示已收到令牌。

    02-02 13:51:29.497 26886-28414/com.wyat.wyat D/OkHttp: <-- 200 OK http://zacmwa.pythonanywhere.com/api-token-auth/ (1734ms)
02-02 13:51:29.507 26886-28414/com.wyat.wyat D/OkHttp: Server: openresty/1.9.15.1
02-02 13:51:29.507 26886-28414/com.wyat.wyat D/OkHttp: Date: Thu, 02 Feb 2017 10:51:29 GMT
02-02 13:51:29.507 26886-28414/com.wyat.wyat D/OkHttp: Content-Type: application/json
02-02 13:51:29.507 26886-28414/com.wyat.wyat D/OkHttp: Transfer-Encoding: chunked
02-02 13:51:29.507 26886-28414/com.wyat.wyat D/OkHttp: Connection: keep-alive
02-02 13:51:29.507 26886-28414/com.wyat.wyat D/OkHttp: Vary: Accept-Encoding
02-02 13:51:29.507 26886-28414/com.wyat.wyat D/OkHttp: X-Frame-Options: SAMEORIGIN
02-02 13:51:29.507 26886-28414/com.wyat.wyat D/OkHttp: Allow: POST, OPTIONS
02-02 13:51:29.507 26886-28414/com.wyat.wyat D/OkHttp: X-Clacks-Overhead: GNU Terry Pratchett
02-02 13:51:29.507 26886-28414/com.wyat.wyat D/OkHttp: {"token":"19d384d3abe94024a9bbbfa85b883b18413d615e"}
02-02 13:51:29.507 26886-28414/com.wyat.wyat D/OkHttp: <-- END HTTP (52-byte body)

如何保存令牌? 编辑:

public class AccessToken {
    @SerializedName("venue")
    @Expose
    private String AccessToken;

    public String getAccessToken() {
        return AccessToken;
    }
}

1 个答案:

答案 0 :(得分:0)

从日志看起来,令牌的关键是&#34;令牌&#34;而不是&#34;场地&#34;。因此,将AccessToken模型修改为此

public class AccessToken {
    @SerializedName("token")
    @Expose
    private String accessToken;

    public String getAccessToken() {
        return accessToken;
    }
}