gpt4 book ai didi

java - Retrofit 库的 onResponse 方法的响应始终返回 null

转载 作者:行者123 更新时间:2023-12-02 02:24:51 25 4
gpt4 key购买 nike

我使用改造从 API 获取一些数据以供用户登录,但是当我编写改造的所有代码并检查其在模拟器上的实现时,它总是在 onResponse 方法中返回 null 响应。

package com.madhulata.shriresume;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;


import com.android.volley.RequestQueue;


import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class MainActivity extends AppCompatActivity {
EditText emailLogin, passwordLogin;
Button loginBtn;
RequestQueue mQueue;
ProgressBar progressBar;
String email,password;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

emailLogin = findViewById(R.id.emailLogin);
passwordLogin = findViewById(R.id.passwordLogin);
loginBtn = findViewById(R.id.loginBtn);
mQueue = VolleySingleton.getnstance(this).getRequestQueue();

progressBar = findViewById(R.id.progressBar);
email = emailLogin.getText().toString().trim();
password = passwordLogin.getText().toString().trim();
// Login button click listener

loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
loginUser();

}
});

}


// Login user by fetching the data from the Api

public void loginUser(){
Call<User> call = RetrofitClient.getInstance().getApi().userLogin(email,password);
call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
User user = response.body();

if(user.getId() != 0){
Toast.makeText(MainActivity.this, "Everthing is okay " , Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "Everthing went wrong " , Toast.LENGTH_SHORT).show();
}
}

@Override
public void onFailure(Call<User> call, Throwable t) {

}
});
}








}

///////////////////////////////////////
User.java
package com.madhulata.shriresume;

public class User {
private int id;
private String email;
private String country;
private String authentication_token;

public User(int id, String email, String country,String authentication_token) {
this.id = id;
this.email = email;
this.country = country;
this.authentication_token = authentication_token;

}

public int getId() {
return id;
}

public String getEmail() {
return email;
}

public String getCountry() {
return country;
}

public String getAuthentication_token(){
return authentication_token;
}
}
//////////////////////////////////////////////////////////////////////
RetroClient.java
package com.madhulata.shriresume;

import retrofit2.Retrofit;

import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitClient {
private static final String BASE_URL = "https://shriresume.com/api/v1/";
private static RetrofitClient mInstance;
private Retrofit retrofit;

private RetrofitClient(){
retrofit = new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
}

public static synchronized RetrofitClient getInstance(){
if(mInstance == null){
mInstance = new RetrofitClient();
}
return mInstance;
}

public Api getApi(){
return retrofit.create(Api.class);
}


}
//////////////////////////////////////////////////////////////////////
Api.java
package com.madhulata.shriresume;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;


public interface Api {



@FormUrlEncoded
@POST("login")
Call<User> userLogin(
@Field("email") String email,
@Field("password") String password
);
}


当来自 API 的数据正确时,预期结果是无错误 toast;当数据不存在时,预期结果是错误 toast

最佳答案

您的代码工作正常,我得到了正确的输出。

尝试在日志中打印输出,下面是我的loginUser()函数代码:

public void loginUser(String email, String pass){
Call<User> call = RetrofitClient.getInstance().getApi().userLogin(email,pass);
call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
User user = response.body();

Log.d("userOutput","user->"+user.toString());
if(user.getId() != 0){
Toast.makeText(TestAct.this, "Everthing is okay " , Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(TestAct.this, "Everthing went wrong " , Toast.LENGTH_SHORT).show();
}
}

@Override
public void onFailure(Call<User> call, Throwable t) {

}
});
}

用户.java

public class User {
private int id;
private String email;
private String country;
private String authentication_token;

public User(int id, String email, String country,String authentication_token) {
this.id = id;
this.email = email;
this.country = country;
this.authentication_token = authentication_token;

}

public int getId() {
return id;
}

public String getEmail() {
return email;
}

public String getCountry() {
return country;
}

public String getAuthentication_token(){
return authentication_token;
}

@Override
public String toString() {
return "User{" +
"id=" + id +
", email='" + email + '\'' +
", country='" + country + '\'' +
", authentication_token='" + authentication_token + '\'' +
'}';
}
}

日志详细信息: enter image description here

关于java - Retrofit 库的 onResponse 方法的响应始终返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57259239/

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