@backup_alisher

Как правильно пройти авторизацию на сайте с помощью Retrofit 2?

Здравствуйте!
Пытаюсь авторизоваться на сайте, для получения token. Но не могу получить ответ ((

В качестве ответа должен принять данные в формате JSON
{
"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());
            }
        });
    }


Выкидывает ошибку
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)
  • Вопрос задан
  • 344 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы