Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用改型从API获取数据_Android_Retrofit2 - Fatal编程技术网

Android 使用改型从API获取数据

Android 使用改型从API获取数据,android,retrofit2,Android,Retrofit2,我需要从API获取详细信息,并在文本视图中显示它们 以下是我的JSON格式API: 我需要获得像用户名,用户图像,用户电话号码等字符串数据,并显示在文本视图。 如何请求所有字段并在不同的文本视图中显示列表 这是我的登录界面 package com.example.hb.loginapi; import java.util.List; import retrofit2.Call; import retrofit2.http.GET; import retrofit2.http

我需要从API获取详细信息,并在文本视图中显示它们

以下是我的JSON格式API:

我需要获得像用户名,用户图像,用户电话号码等字符串数据,并显示在文本视图。 如何请求所有字段并在不同的文本视图中显示列表

这是我的登录界面

package com.example.hb.loginapi;

import java.util.List;

   import retrofit2.Call;
   import retrofit2.http.GET;

  import retrofit2.http.POST;

   import retrofit2.http.Query;

    interface Login {

   @POST("user_login_v1")
   Call<ResObj>  loginInfo(@Query("password") String password, 
   @Query("email") String email);


   @GET("user_login_v1")
   Call<List> getUserDetails();


}
package com.example.hb.loginapi;
导入java.util.List;
2.电话;;
导入文件2.http.GET;
导入文件2.http.POST;
导入2.http.Query;
界面登录{
@POST(“用户登录版”)
调用loginInfo(@Query(“password”)字符串密码,
@查询(“电子邮件”)字符串(电子邮件);
@获取(“用户登录名”)
调用getUserDetails();
}
这是我的ResObj课程

package com.example.hb.loginapi;

import com.google.gson.annotations.SerializedName;
import java.util.List;

 public class ResObj {



@SerializedName("settings")
private Settings settings;

@SerializedName("data")
private List<DataItem> data;

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

public Settings getSettings(){
    return settings;
}

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

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

public class Settings {

    @SerializedName("success")
    private String success;

    @SerializedName("message")
    private String message;

    @SerializedName("fields")
    private List<String> fields;

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

    public String getSuccess() {
        return success;
    }

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

    public String getMessage() {
        return message;
    }

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

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

public class DataItem{

    @SerializedName("user_name")
    private String userName;

    @SerializedName("search_report_count")
    private String searchReportCount;

    @SerializedName("access_token")
    private String accessToken;

    @SerializedName("profile_image")
    private String profileImage;

    @SerializedName("is_social")
    private String isSocial;

    @SerializedName("is_notification_enabled")
    private String isNotificationEnabled;

    @SerializedName("user_id")
    private String userId;

    @SerializedName("phone")
    private String phone;

    @SerializedName("plate_number")
    private String plateNumber;

    @SerializedName("state_id")
    private String stateId;

    @SerializedName("state")
    private String state;

    @SerializedName("email")
    private String email;

    @SerializedName("status")
    private String status;

    public void setUserName(String userName){
        this.userName = userName;
    }

    public String getUserName(){
        return userName;
    }

    public void setSearchReportCount(String searchReportCount){
        this.searchReportCount = searchReportCount;
    }

    public String getSearchReportCount(){
        return searchReportCount;
    }

    public void setAccessToken(String accessToken){
        this.accessToken = accessToken;
    }

    public String getAccessToken(){
        return accessToken;
    }

    public void setProfileImage(String profileImage){
        this.profileImage = profileImage;
    }

    public String getProfileImage(){
        return profileImage;
    }

    public void setIsSocial(String isSocial){
        this.isSocial = isSocial;
    }

    public String getIsSocial(){
        return isSocial;
    }

    public void setIsNotificationEnabled(String isNotificationEnabled){
        this.isNotificationEnabled = isNotificationEnabled;
    }

    public String getIsNotificationEnabled(){
        return isNotificationEnabled;
    }

    public void setUserId(String userId){
        this.userId = userId;
    }

    public String getUserId(){
        return userId;
    }

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

    public String getPhone(){
        return phone;
    }

    public void setPlateNumber(String plateNumber){
        this.plateNumber = plateNumber;
    }

    public String getPlateNumber(){
        return plateNumber;
    }

    public void setStateId(String stateId){
        this.stateId = stateId;
    }

    public String getStateId(){
        return stateId;
    }

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

    public String getState(){
        return state;
    }

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

    public String getEmail(){
        return email;
    }

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

    public String getStatus(){
        return status;
    }
}

}
package com.example.hb.loginapi;
导入com.google.gson.annotations.SerializedName;
导入java.util.List;
公共类资源{
@SerializedName(“设置”)
私人设置;
@SerializedName(“数据”)
私人名单数据;
公共无效设置(设置){
this.settings=设置;
}
公共设置getSettings(){
返回设置;
}
公共无效设置数据(列表数据){
这个数据=数据;
}
公共列表getData(){
返回数据;
}
公共类设置{
@序列化名称(“成功”)
私人字符串成功;
@SerializedName(“消息”)
私有字符串消息;
@SerializedName(“字段”)
私有列表字段;
public void setSuccess(字符串成功){
成功=成功;
}
公共字符串getSuccess(){
回归成功;
}
公共无效设置消息(字符串消息){
this.message=消息;
}
公共字符串getMessage(){
返回消息;
}
公共无效设置字段(列表字段){
this.fields=字段;
}
公共列表getFields(){
返回字段;
}
}
公共类数据项{
@SerializedName(“用户名”)
私有字符串用户名;
@SerializedName(“搜索报告计数”)
私有字符串searchReportCount;
@SerializedName(“访问令牌”)
私有字符串访问令牌;
@序列化名称(“配置文件\图像”)
私有字符串轮廓图像;
@SerializedName(“is_social”)
私有字符串是社会的;
@SerializedName(“是否已启用通知”)
私有字符串isNotificationEnabled;
@SerializedName(“用户id”)
私有字符串用户标识;
@序列化名称(“电话”)
私人电话;
@序列化名称(“车牌号”)
私有字符串编号;
@SerializedName(“状态id”)
私有字符串stateId;
@序列化名称(“状态”)
私有字符串状态;
@序列化名称(“电子邮件”)
私人字符串电子邮件;
@序列化名称(“状态”)
私有字符串状态;
public void setUserName(字符串用户名){
this.userName=用户名;
}
公共字符串getUserName(){
返回用户名;
}
public void setSearchReportCount(字符串searchReportCount){
this.searchReportCount=searchReportCount;
}
公共字符串getSearchReportCount(){
返回搜索报告计数;
}
公共无效setAccessToken(字符串accessToken){
this.accessToken=accessToken;
}
公共字符串getAccessToken(){
返回accessToken;
}
public void setProfileImage(字符串profileImage){
this.profileImage=profileImage;
}
公共字符串getProfileImage(){
返回图像;
}
public void setIsSocial(字符串isSocial){
this.isSocial=isSocial;
}
公共字符串getIsSocial(){
回归社会;
}
public void setIsNotificationEnabled(字符串isNotificationEnabled){
this.isNotificationEnabled=isNotificationEnabled;
}
公共字符串getIsNotificationEnabled(){
返回已启用通知;
}
public void setUserId(字符串userId){
this.userId=userId;
}
公共字符串getUserId(){
返回用户标识;
}
公用无效设置电话(字符串电话){
this.phone=电话;
}
公共字符串getPhone(){
回电话;
}
公共作废setPlateNumber(字符串plateNumber){
这个.plateNumber=plateNumber;
}
公共字符串getPlateNumber(){
返回车牌号;
}
public void setStateId(字符串stateId){
this.stateId=stateId;
}
公共字符串getStateId(){
返回stateId;
}
公共无效设置状态(字符串状态){
this.state=状态;
}
公共字符串getState(){
返回状态;
}
公用电子邮件(字符串电子邮件){
this.email=电子邮件;
}
公共字符串getEmail(){
回复邮件;
}
公共无效设置状态(字符串状态){
这个状态=状态;
}
公共字符串getStatus(){
返回状态;
}
}
}
我的布局

<?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>

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

    userName=findViewById(R.id.userName);


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

     login=ApiUtils.getLoginClass();

    getUserData();
}


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

    call.enqueue(new Callback() {
        @Override
        public void onResponse(Call call, Response response) {
            if(response.isSuccessful()){
                List resObj=(List)response.body();


                for(int i=0;i<resObj.size();i++){
                    Log.e("data",resObj.get(i).toString());
                }
 }
        }


        @Override
        public void onFailure(Call call, Throwable t) {
            Toast.makeText(getApplicationContext(),t.getMessage(),Toast.LENGTH_LONG).show();
        }
    });
}
public类MainActivity扩展了AppCompatActivity{
List emList=new ArrayList();
List dataList=new ArrayList();
List userDets=new ArrayList();
图像视图proImg;
文本视图用户名;
TextView用户ID;
TextView用户电子邮件;
TextView用户状态;
TextView用户模板;
TextView用户名;
TextView用户状态ID;
TextView用户搜索报告计数;
字符串img;
字符串名;
字符串idUser;
字符串emailID;
字符串状态;
弦板;
字符串stateName;
字符串stateID;
字符串搜索报告计数;
字符串电子邮件;
登录;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
emList.add(电子邮件);
userName=findviewbyd(R.id.userName);
email=getIntent().getStringExtra(“电子邮件”);
login=ApiUtils.getLoginClass();
getUserData();
}
私有void getUserData(){
Call Call=login.getUserDetails();
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
如果(响应)
 private void callApi() {
    Log.e(TAG, "callApi: inside apiCall");
    Call<List> call = 
 login.getUserDetails();

 call.enqueue(new Callback<List>() {
        @Override
        public void onResponse(Call<List> call, @NonNull Response<List>response) {
            List items = response.body();

            items = list.data;
            String user_id = items.getUserID();
        }

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

        }
List<DataItem>  items = resObj.getData();
    for(int i=0;i<items.size();i++){
        Log.e("data",items.get(i).getUserName()); // like this you can access other values in the items list
    }
@GET("user_login_v1")
Call<ResObj> getUserDetails();
private void getUserData(){
    Call<ResObj> call=login.getUserDetails();

    call.enqueue(new Callback() {
        @Override
        public void onResponse(Call call, Response response) {
            if(response.isSuccessful()){
                ResObj resObj=(ResObj)response.body();
                DataItem user=reObj.getData().get(0);
                //set value on yout text views
                userName.setText(user.getUserName())
                userID.setText(user.getUserId())
                //....
               }
        }


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