改造返回null

时间:2019-05-29 06:59:12

标签: android retrofit2

我想从API检索用户详细信息,但是当我将文本放在textview中时,它变成空白。

我的对象类

public class Obj {
/**
 * settings : {"success":"1","message":"You have successfully logged in.","fields":["user_id","email","status","profile_image","access_token","phone","user_name","plate_number","state_id","state","is_social","is_notification_enabled","search_report_count","remove_add"]}
 * data : [{"user_id":"108","email":"jk@grr.la","status":"Active","profile_image":"http://locateaplate.projectspreview.net/public/upload/profile_images/978307200_0-20190515152207782279.png","access_token":"144d44efa92327ed0c1b6d98bb866563a7cc95680c136603bbf0eca4832da635","phone":"","user_name":"Jack Kalsan","plate_number":"ABC1234","state_id":"2722","state":"Abakan","is_social":"","is_notification_enabled":"Yes","search_report_count":"69","remove_add":"0"}]
 */

private SettingsBean settings;
private List<DataBean> data;

public SettingsBean getSettings() {
    return settings;
}

public void setSettings(SettingsBean settings) {
    this.settings = settings;
}

public List<DataBean> getData() {
    return data;
}

public void setData(List<DataBean> data) {
    this.data = data;
}

public static class SettingsBean {
    /**
     * success : 1
     * message : You have successfully logged in.
     * fields : ["user_id","email","status","profile_image","access_token","phone","user_name","plate_number","state_id","state","is_social","is_notification_enabled","search_report_count","remove_add"]
     */

    private String success;
    private String message;
    private List<String> fields;

    public String getSuccess() {
        return success;
    }

    public void setSuccess(String success) {
        this.success = success;
    }

    public String getMessage() {
        return message;
    }

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

    public List<String> getFields() {
        return fields;
    }

    public void setFields(List<String> fields) {
        this.fields = fields;
    }
}

public static class DataBean {
    /**
     * user_id : 108
     * email : jk@grr.la
     * status : Active
     * profile_image : http://locateaplate.projectspreview.net/public/upload/profile_images/978307200_0-20190515152207782279.png
     * access_token : 144d44efa92327ed0c1b6d98bb866563a7cc95680c136603bbf0eca4832da635
     * phone :
     * user_name : Jack Kalsan
     * plate_number : ABC1234
     * state_id : 2722
     * state : Abakan
     * is_social :
     * is_notification_enabled : Yes
     * search_report_count : 69
     * remove_add : 0
     */

    private String user_id;
    private String email;
    private String status;
    private String profile_image;
    private String access_token;
    private String phone;
    private String user_name;
    private String plate_number;
    private String state_id;
    private String state;
    private String is_social;
    private String is_notification_enabled;
    private String search_report_count;
    private String remove_add;

    public String getUser_id() {
        return user_id;
    }

    public void setUser_id(String user_id) {
        this.user_id = user_id;
    }

    public String getEmail() {
        return email;
    }

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

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getProfile_image() {
        return profile_image;
    }

    public void setProfile_image(String profile_image) {
        this.profile_image = profile_image;
    }

    public String getAccess_token() {
        return access_token;
    }

    public void setAccess_token(String access_token) {
        this.access_token = access_token;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getUser_name() {
        return user_name;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public String getPlate_number() {
        return plate_number;
    }

    public void setPlate_number(String plate_number) {
        this.plate_number = plate_number;
    }

    public String getState_id() {
        return state_id;
    }

    public void setState_id(String state_id) {
        this.state_id = state_id;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getIs_social() {
        return is_social;
    }

    public void setIs_social(String is_social) {
        this.is_social = is_social;
    }

    public String getIs_notification_enabled() {
        return is_notification_enabled;
    }

    public void setIs_notification_enabled(String is_notification_enabled) {
        this.is_notification_enabled = is_notification_enabled;
    }

    public String getSearch_report_count() {
        return search_report_count;
    }

    public void setSearch_report_count(String search_report_count) {
        this.search_report_count = search_report_count;
    }

    public String getRemove_add() {
        return remove_add;
    }

    public void setRemove_add(String remove_add) {
        this.remove_add = remove_add;
    }
}}

我的MainActivity.java

public class MainActivity extends AppCompatActivity {

List emList=new ArrayList();

List dataList=new ArrayList();

List userDets=new ArrayList();

ImageView proImg;
TextView userName;
TextView userID;
TextView userEmail;
TextView userStatus;
TextView userPlateNum;
TextView userStateName;
TextView userStateID;
TextView userSearchReportCount;

String img;
String name;
String idUser;
String emailID;
String status;
String plateNum;
String stateName;
String stateID;
String searchReportCount;

String email;

Login login;

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

    emList.add(email);

    proImg=(ImageView)findViewById(R.id.userProfilePic);
    userName=findViewById(R.id.userName);

    email=getIntent().getStringExtra("email");

     login=ApiUtils.getLoginClass();

    getUserData();
}

private void getUserData(){
    Call<Obj.DataBean> call=login.getUserDetails();

    call.enqueue(new Callback<Obj.DataBean>() {
        @Override
        public void onResponse(Call<Obj.DataBean> call, Response<Obj.DataBean> response) {
            if(response.isSuccessful()) {

                Obj.DataBean obj=response.body();

                name=obj.getUser_name();
                userName.setText(name);

            }
        }

        @Override
        public void onFailure(Call<Obj.DataBean> call, Throwable t) {
            Toast.makeText(getApplicationContext(),t.getMessage(),Toast.LENGTH_LONG).show();
        }
    });
}}

我的界面

interface Login {

     @GET("user_login_v1")
   Call<Obj.DataBean> getUserDetails();}

我的布局

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:background="#e9edf6"
android:scrollbars="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

<com.mikhaellopez.circularimageview.CircularImageView
    android:id="@+id/userProfilePic"
    android:layout_gravity="center"
    android:layout_marginTop="30dp"
    android:layout_width="150dp"
    android:layout_height="150dp"
    />

    <TextView
        android:id="@+id/userName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/fnavyblue"
        android:textSize="20sp"
        android:text="DummyNameText"
        android:layout_marginTop="50dp"

        />

    <TextView
        android:id="@+id/userID"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/fnavyblue"
        android:textSize="20sp"
        android:text="DummyUserIDText"
        android:layout_marginTop="20dp"
        />

    <TextView
        android:id="@+id/emailID"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/fnavyblue"
        android:textSize="20sp"
        android:text="DummyEmailText"
        android:layout_marginTop="20dp"
        />

    <TextView
        android:id="@+id/userStatus"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/fnavyblue"
        android:textSize="20sp"
        android:text="DummyStatusText"
        android:layout_marginTop="20dp"
        />

    <TextView
        android:id="@+id/userPlateNum"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/fnavyblue"
        android:textSize="20sp"
        android:text="DummyPlateNumText"
        android:layout_marginTop="20dp"
        />

    <TextView
        android:id="@+id/userStateName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/fnavyblue"
        android:textSize="20sp"
        android:text="DummyStateNameText"
        android:layout_marginTop="20dp"
        />

    <TextView
        android:id="@+id/userStateID"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/fnavyblue"
        android:textSize="20sp"
        android:text="DummyStateIDText"
        android:layout_marginTop="20dp"
        />

    <TextView
        android:id="@+id/userSearchReportCount"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/fnavyblue"
        android:textSize="20sp"
        android:text="DummySearchReportCountText"
        android:layout_marginTop="20dp"
        />
    </LinearLayout>
</ScrollView>

我想在不同的文本视图中显示详细信息(例如名称,id,车牌号等)。但是它返回一个空字符串,即null。

Here's my API in JSON format

有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

尝试这种接口方法。

   @GET("user_login_v1")
   Call<Obj> getUserDetails(@Query("email") String email,
                                             @Query("password") String password);

和MainActivity中的Call方法是这样的。

 Call<Obj> call=login.getUserDetails(email,password);//email and password which user enter when he login in your device.

随时问更多问题。