简体   繁体   English

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

[英]The response from the onResponse method from Retrofit Library is always returning null

I'm using retrofit to get some data from the API for user login, but when I write all the code for the retrofit and check it's implementation on the emulator it always returns null response in onResponse method. 我正在使用翻新从API中获取一些数据以进行用户登录,但是当我编写所​​有翻新代码并在模拟器上检查其实现时,始终会在onResponse方法中返回空响应。

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
    );
}


The expected result is the no-error toast when the data from the API is correct and the error toast when the data is not present 如果API数据正确,则预期结果是无错误吐司,而当数据不存在时,则预期结果是错误吐司

Your code is working fine, I am getting proper output. 您的代码工作正常,我得到了正确的输出。

Try to print the output in logs, Below is my loginUser() function code: 尝试在日志中打印输出,以下是我的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) {

        }
    });
}

User.java User.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 + '\'' +
            '}';
}
}

Log details: 日志详细信息: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM