【问题标题】:The response from the onResponse method from Retrofit Library is always returning null来自 Retrofit Library 的 onResponse 方法的响应总是返回 null
【发布时间】:2019-07-30 12:15:01
【问题描述】:

我正在使用改造从 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
    );
}


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

【问题讨论】:

  • 请创建一个更好的标题。使用一个有意义的
  • 对不起那个朋友,你能帮帮我吗
  • 我觉得这次还可以
  • 请帮忙。
  • 显示错误文本

标签: java android retrofit


【解决方案1】:

您的代码运行良好,我得到了正确的输出。

尝试在日志中打印输出, 下面是我的 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 + '\'' +
            '}';
}
}

日志详情:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    • 2020-08-16
    • 1970-01-01
    • 2020-02-10
    • 2020-06-03
    • 1970-01-01
    相关资源
    最近更新 更多