Android 登录获取错误代码=422,消息=Unprocessable
我正在使用改型,每当我点击登录按钮时,它就会响应.errorBody()。我只是想通过改造来导航。在Logcat中,我发现了这个错误。有人能帮我解决吗Response{protocol=http/1.1,code=422,message=Unprocessable Entity,url=https://xxxxxxxxxx.com/api/signin} 登录活动Android 登录获取错误代码=422,消息=Unprocessable,android,retrofit2,Android,Retrofit2,我正在使用改型,每当我点击登录按钮时,它就会响应.errorBody()。我只是想通过改造来导航。在Logcat中,我发现了这个错误。有人能帮我解决吗Response{protocol=http/1.1,code=422,message=Unprocessable Entity,url=https://xxxxxxxxxx.com/api/signin} 登录活动 private void signIn(){ String email = eName.getText().toStr
private void signIn(){
String email = eName.getText().toString().trim();
String password = ePassword.getText().toString().trim();
if(email.isEmpty()){
eName.setError("Email is required");
eName.requestFocus();
return;
}
if(!Patterns.EMAIL_ADDRESS.matcher(email).matches()){
eName.setError("Enter a Valid email");
eName.requestFocus();
return;
}
if(password.isEmpty()){
ePassword.setError("Password Required");
ePassword.requestFocus();
return;
}
if(password.length()<8){
ePassword.setError("Password should be atleast 8 character long");
ePassword.requestFocus();
return;
}
RetroInterface retroInterface = RetroClient.getClient().create(RetroInterface.class);
Call<LoginResponse> loginResponseCall =retroInterface.login(email,password);
loginResponseCall.enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
Log.d("Login Response",response.toString());
LoginResponse lResponse = response.body();
if(response.isSuccessful()){
Log.d("token",lResponse.getToken());
Log.d("email",lResponse.getLoginrequest().email);
Log.d("password",lResponse.getLoginrequest().name);
Toast.makeText(LoginActivity.this,"Login Success",Toast.LENGTH_SHORT).show();
Intent i = new Intent(LoginActivity.this, MainActivity.class);
startActivity(i);
finish();
}
else {
response.errorBody();
Toast.makeText(LoginActivity.this,"Response Error"+response.errorBody(),Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
Log.e("Error", t.getLocalizedMessage());
}
}尝试在API接口中的@POST之前添加@FormUrlEncoded,因为请求过程可能是Url编码的。此外,请确保您是否需要将电子邮件和密码作为字段或其他内容发送。尝试在API界面中的@POST之前添加@FormUrlEncoded,因为请求过程可能是Url编码的。此外,请确保是否需要将电子邮件和密码作为字段或其他内容发送。从何处传递基本url?从何处传递基本url?Monim Kaiser-i添加了@FormUrlEncoded,但仍会收到相同的错误。我需要同时传递Id和aur令牌吗?正因为如此,它才会发生?请尝试先从Postman测试api。我想你到时候会发现的。我在那里做得很好。我将分享截图。Monim Kaiser-i添加了@FormUrlEncoded,但仍然得到相同的错误。我需要同时传递Id和aur令牌吗?正因为如此,它才会发生?请尝试先从Postman测试api。我想你到时候会发现的。我在那里做得很好。我将分享截图。
@POST("api/signin")
Call<LoginResponse> login(@Field("email") String email,
@Field("password") String password);
public class RetroClient {
public static final String BASE_URL = "https://xxxxxxx.com/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}