gpt4 book ai didi

java - 改造响应正文为空但状态代码为 200

转载 作者:行者123 更新时间:2023-12-01 16:22:05 25 4
gpt4 key购买 nike

我想做一个登录功能,我想做一个登录功能,最初我看到响应代码显示200,但是当我看到响应正文为空时。我仍然可以登录,但登录的用户数据未保存。我使用 2 db MySql 和 SQLite,我使用改造和共享首选项。这是我的代码

API接口(interface)

public interface Api {
@FormUrlEncoded
@POST(Config.API_LOGIN_USER)
Call<UserOrtu> loginUser(
@Field("email") String email,
@Field("password") String password
);

PrefUtil.java

public class PrefUtil {
public static final String USER_SESSION = "user_session";
public static final String USER_STORAGE = "user_storage";

public static SharedPreferences getSharedPreferences(Context ctx){
return PreferenceManager.getDefaultSharedPreferences(ctx);
}

public static void putUser(Context ctx, String key, UserOrtu user){
Gson gson = new Gson();
String json = gson.toJson(user);
putString(ctx, key, json);
}

public static UserOrtu getUser(Context ctx, String key){
Gson gson = new Gson();
String json = getString(ctx, key);
UserOrtu user = gson.fromJson(json, UserOrtu.class);
return user;
}

public static void putString(Context ctx, String key, String value){
getSharedPreferences(ctx).edit().putString(key, value).apply();
}

public static String getString(Context ctx, String key){
return getSharedPreferences(ctx).getString(key, null);
}

public static void clear(Context ctx) {
getSharedPreferences(ctx).edit().clear().apply();
}
}

型号UserOrtu.java

public class UserOrtu {
@SerializedName("id_user")
@Expose
private int idUser;
@SerializedName("nama")
@Expose
private String nama;
@SerializedName("email")
@Expose
private String email;
@SerializedName("password")
@Expose
private String password;
@SerializedName("error")
@Expose
private Boolean error;
@SerializedName("message")
@Expose
private String message;

public int getIdUser() {
return idUser;
}

public void setIdUser(int idUser) {
this.idUser = idUser;
}

public String getNama() {
return nama;
}

public void setNama(String nama) {
this.nama = nama;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public Boolean getError() {
return error;
}

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

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}

SignInActivity.java

公共(public)类 SignInActivity 扩展了 AppCompatActivity {

    @BindView(R.id.input_email_signin)
TextInputEditText etEmail;
@BindView(R.id.text_register)
TextView tvRegister;
@BindView(R.id.input_password_signin)
TextInputEditText etPassword;

EmailValidator emailValidator;
PasswordValidator passwordValidator;
Context context;

private String email;
private String password;
private Api mApi;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isSessionLogin()){
startActivity(new Intent(this, MainActivity.class));
this.finish();
}

setContentView(R.layout.activity_sign_in);
ButterKnife.bind(this);
AndroidThreeTen.init(this);
mApi = RetrofitBuilder.builder(this).create(Api.class);

}

boolean isEmail(EditText text){
emailValidator = new EmailValidator();
String email = text.getText().toString();
return emailValidator.isValid(email);
}

boolean isPassword(EditText text){
passwordValidator = new PasswordValidator();
String pass = text.getText().toString();
return passwordValidator.isValid(pass);
}

@OnClick(R.id.text_register) void toRegister(){
Intent intent = new Intent(this, SignUpActivity.class);
startActivity (intent);
}

@OnClick(R.id.btn_signin) void onLogin(){
if(isEempty(etEmail)){
etEmail.setError("Email harus diisi");
}else if(isEempty(etPassword)){
etPassword.setError("Password harus diisi");
}else if(!isEmail(etEmail)){
etEmail.setError("Email tidak valid");
}else if(!isPassword(etPassword)){
String str = passwordValidator.getString();
Toast.makeText(getApplicationContext(),str, Toast.LENGTH_SHORT).show();
}else {
loginAct();
}
}
void loginAct(){
email = etEmail.getText().toString();
password = etPassword.getText().toString();
final MaterialDialog dialog = DialogBuilder.showLoadingDialog(SignInActivity.this, "Updating Data", "Please wait..", false);
mApi.loginUser(email, password).enqueue(new Callback<UserOrtu>() {
@Override
public void onResponse(Call<UserOrtu> call, Response<UserOrtu> response) {
UserOrtu user = response.body();
Log.i("USER_LOGIN", response.message());
if (user != null){
//Masih error disini
//if (!user.getError()){
PrefUtil.putUser(getApplicationContext(), PrefUtil.USER_SESSION, user);
Intent intent = new Intent(SignInActivity.this, MainActivity.class);
startActivity(intent);
//this.finish();

//}
Toast.makeText(getApplicationContext(), user.getMessage(), Toast.LENGTH_SHORT).show();
}
if (response.code() == 403){
etPassword.requestFocus();
etPassword.setError(getString(R.string.error_password));
}
if (response.code() == 404){
etEmail.requestFocus();
etEmail.setError(getString(R.string.error_login));
}
dialog.dismiss();
}

@Override
public void onFailure(Call<UserOrtu> call, Throwable t) {
//Toast.makeText(getActivity(), t.getMessage(), Toast.LENGTH_SHORT).show();
dialog.dismiss();
Log.i("USER_LOGIN", t.getMessage());
DialogBuilder.showErrorDialog(SignInActivity.this, "Gagal Login");
}
});
}

// this method to check is user logged in ?
boolean isSessionLogin(){
return PrefUtil.getUser(getApplicationContext(), PrefUtil.USER_SESSION) != null;
}

}

这是我的结果 enter image description here

这是我的 JSON 请求 enter image description here

最佳答案

您的模型所表示的数据与 JSON 中的数据不同。对于 UserOrtu 类,只有当您的响应具有如下结构时,它才会起作用:

{
"id":1,
"nama": "Name",
"email":"email",
"message":"msg"
}

但是正如您所看到的,前 3 个字段位于另一个对象 data 内。所以你的模型应该看起来更像:

class LoginResponse{
@SerializedName("data")
@Expose
private UserOrtu userData;
@SerializedName("message")
@Expose
private String message;
}

关于java - 改造响应正文为空但状态代码为 200,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62242603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com