简体   繁体   中英

How do I access a token from the presenter in Android?

I am posting an email and password so that I can receive a response.

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

Then I run the code in the presenter.

 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) {

                    }
                });

I get a nullpointerexception because of access.getAccessToken().

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

However the logs show that a token was received.

    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)

How do I save the token? EDIT :

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

    public String getAccessToken() {
        return AccessToken;
    }
}

From the logs it looks like the key for the token is "token" and not "venue". So modify your AccessToken model to this

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

    public String getAccessToken() {
        return accessToken;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM