简体   繁体   English

如何获取accessToken(来自服务器的响应)

[英]How to get accessToken(response coming from the server)

Hello stuck on 'Retrofit2' thing. 您好停留在“ Retrofit2”上。 Can somebody please help me how could I possibly get the accessToken. 有人可以帮帮我,我怎么可能获得accessToken。 This 'accessToken' will only be created from the server if the user logged in. 仅当用户登录时,才从服务器创建此“ accessToken”。 在此处输入图片说明

Step 1: 第1步:
Make Object from JSON. 从JSON生成对象。 You may use jsonToPojo android studio plugin. 您可以使用jsonToPojo android studio插件。

Step 2: 第2步:
Process result from onResponse: 来自onResponse的处理结果:

ApiCall apiService = ApiClient.getClient().create(ApiCall.class);
        Call<Api> callForLogin = apiService.getLoginResult("user_name","Password");
        callForDrawList.enqueue(new Callback<LoginResultObject>() {
            @Override
            public void onResponse(Call<LoginResultObject> call, Response<LoginResultObject> response) {
                String accessToken=response.body().getAccessToken());
            }

            @Override
            public void onFailure(Call<DrawResultsApi> call, Throwable t) {

            }
        });

I think it may help you... Thanks 我认为这可能对您有帮助...谢谢

I assume you know how to use retrofit. 我假设您知道如何使用改造。 Something you need is just a simple request like below: 您需要的只是一个简单的请求,如下所示:

call.enqueue(new Callback<AuthenticationResponse>() {             @Override             public void onResponse(Call<AuthenticationResponse> call, Response<AuthenticationResponse> response) {                    if (response.code() == 200)                     mAuthenticateView.showPhoneNumberSentSuccessfully(response.body().getToken(), response.body().getExpiresIn());                 else                     mAuthenticateView.showMessage(response.message());             }             @Override             public void onFailure(Call<AuthenticationResponse> call, Throwable t) {     }         });

For more information see this tutorial: Using Retrofit 2.x as REST client 有关更多信息,请参见本教程: 将Retrofit 2.x用作REST客户端

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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