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之外,但在尝试登录时位于第1行第1列路径$的STRING位置, 我是新手,请帮助我修复此错误,这会给我运行应用程序带来麻烦。我应该如何使用android改造向laravel Web api进行登录请求?
答案 0 :(得分:0)
在我将客户端ID设置为(int)并将客户端密钥设置为(String)并将真实的客户端ID和客户端密钥作为请求中的参数传递之后,此问题终于得到解决