запрос на вход в систему от Android-до - PullRequest
1 голос
/ 24 июня 2019
at first I did user register it worked fine but now I am trying to send 
login request to laaravel web api from android using Retrofit and it is 
giving me  the error : java.lang.illegalStateException: Excepted 
BEGAIN_OBJECT but was  STRING at line 1 column 1 path $

I have no Idea how to solve this problem, The registration part had 
worked 
fine but this is giving me error,

/ файл 1: ApiInterface JsonPlaceHolderApi.java

public interface JsonPlaceHolderApi {

//this is register method which works fine
@Headers("Content-Type: application/json")
@POST("register")
Call<UserRegisterModel> registerUser(@Body UserRegisterModel user);

// this is login method which is giving me error
@Headers("Content-Type: application/json")
@POST("login")
Call<LoginResponseModel> loginRequest(@Body UserLoginModel user);
}


 // Userlogin model based on laravel api
file 2:  UserLoginModel.java

public class UserLoginModel {

@SerializedName("email")
private String u_email;

@SerializedName("password")
private String u_password;

@SerializedName("client_id")
private int client_id;

@SerializedName("client_secret")
private int client_secret;

public UserLoginModel(String u_email, String u_password,int client_id, 
int client_secret) {
    this.client_id = client_id;
    this.client_secret = client_secret;
    this.u_email = u_email;
    this.u_password = u_password;
}

public int getClient_id() {
    return client_id;
}

public void setClient_id(int client_id) {
    this.client_id = client_id;
}

public int getClient_secret() {
    return client_secret;
}

public void setClient_secret(int client_secret) {
    this.client_secret = client_secret;
}

public String getU_email() {
    return u_email;
}

public void setU_email(String u_email) {
    this.u_email = u_email;
}

public String getU_password() {
    return u_password;
}

 public void setU_password(String u_password) {
    this.u_password = u_password;
 }
 }

file 3: loginResponseModel.java

public class LoginResponseModel {

@SerializedName("message")
String msg;

@SerializedName("errors")
String error;

public LoginResponseModel(String msg, String error) {
    this.msg = msg;
    this.error = error;
}

public String getMsg() {
    return msg;
}

public void setMsg(String msg) {
    this.msg = msg;
}

public String getError() {
    return error;
}

public void setError(String error) {
    this.error = error;
}
}


file 4: retrofit login request code

@Override
public void onClick(View v) { // onclick event listener

    if (v.getId() == R.id.btnSignInNow) { // if signin button is clicked

        String email = etEmail.getText().toString();
        String password = etPassword.getText().toString();
        Gson gson = new GsonBuilder()
                .setLenient()
                .create();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.eticketnepal.com/api/auth/")
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
        JsonPlaceHolderApi jsonPlaceHolderApi = 
retrofit.create(JsonPlaceHolderApi.class);

        UserLoginModel userLogin = new UserLoginModel(email, 
password,34543501,34543501);

        Call<LoginResponseModel> call = 
jsonPlaceHolderApi.loginRequest(userLogin);
        call.enqueue(new Callback<LoginResponseModel>() {
            @Override
            public void onResponse(Call<LoginResponseModel> call, 
Response<LoginResponseModel> response) {
                if (!response.isSuccessful()) {
                    tvDisplayText.setText("Code: " + response.code());
                    return;
                }
                LoginResponseModel userLoginResponse = response.body();
                String content = "";
                content += "Code: " + response.code() + "\n";
                content += "Response " + response.message() + "\n\n";
                // this textview displays the response of the request
                // method which is giving me error now
                tvDisplayText.append(content);
            }
            @Override
             public void 
             onFailure(Call<LoginResponseModel>call,Throwablet) {
                tvDisplayText.setText(t.getMessage());
            }
        });
    }

приведенный выше код дает мне java.lang.illegalStateException: исключено BEGAIN_OBJECT, но при попытке войти в него строка STRING находилась в строке 1 столбца 1 пути $, Я новичок в модернизации, помогите мне исправить эту ошибку, из-за которой у меня возникают проблемы с запуском приложения. Что я должен использовать для запроса входа в систему для веб-API Laravel от Android модификации?

1 Ответ

0 голосов
/ 26 июня 2019

Это наконец исправлено после того, как я установил идентификатор клиента как (int) и секрет клиента как (String) и передал реальный идентификатор клиента и секрет клиента в качестве параметра в запросе

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...