B
B
backup_alisher2018-04-21 18:18:15
Android
backup_alisher, 2018-04-21 18:18:15

How to get authorized on the site using Retrofit 2?

Hello!
I'm trying to log in to the site to get a token. But I can’t get a response ((
As a response, I must accept data in JSON format

{
"data": {
    "token": "BxSGg2QK00-2PeKfG1Cw2RuBCleWjJ09"
},
"error": null,
"system": {
    "time": 0.405633
}}

Interface
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;

public interface API {
    @POST("user/login")
    Call<LoginResponse> login(@Body LoginRequest loginRequest);
}

LoginRequest
public class LoginRequest {
    public LoginRequest() {
        this.login = login;
        this.password = password;
    }

    @SerializedName("login")
    String login;

    @SerializedName("password")
    String password;
}

LoginResponse
public class LoginResponse {
    @Nullable
    @SerializedName("data")
    public Data data;

    public class Data {

        @Nullable
        @SerializedName("token")
        public String token;

    }

    @Nullable
    @SerializedName("error")
    public String error;

    @Nullable
    @SerializedName("system")
    public System system;

    public class System {

        @Nullable
        @SerializedName("time")
        public double time;

    }

MainActivity
Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://mysite.ru/api/authorize/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        API api = retrofit.create(API.class);

        final LoginRequest loginRequest = new LoginRequest();
        loginRequest.login = "admin";
        loginRequest.password = "admin";

        Call<LoginResponse> call = api.login(loginRequest);
        call.enqueue(new Callback<LoginResponse>() {
            @Override
            public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
                LoginResponse loginResponse = response.body();
                Log.d("authLog", loginResponse.data.token.toString());
            }

            @Override
            public void onFailure(Call<LoginResponse> call, Throwable t) {
                Log.d("authLog", "Error: " + t.getMessage());
            }
        });
    }

throws an error
04-21 18:09:03.324 3531-3531/examlpe.ru.authorizatioapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: examlpe.ru.authorizatioapp, PID: 3531
    java.lang.NullPointerException
        at examlpe.ru.authorizatioapp.MainActivity$1.onResponse(MainActivity.java:41)
        at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5019)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question