Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.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 安卓通过改造获得所有字段_Android_Json_Retrofit - Fatal编程技术网

Android 安卓通过改造获得所有字段

Android 安卓通过改造获得所有字段,android,json,retrofit,Android,Json,Retrofit,我正在尝试从JSON获取一些字段。问题是,如果它只生成一个,那么我可以通过使用下面的 [{"sname":"Akshay","class":"MCA","latitude":"73.6562320000","longitude":"72.2041200000"}] 通过使用以下类 public class Example { @SerializedName("sname") @Expose private String sname; @SerializedN

我正在尝试从JSON获取一些字段。问题是,如果它只生成一个,那么我可以通过使用下面的

[{"sname":"Akshay","class":"MCA","latitude":"73.6562320000","longitude":"72.2041200000"}]
通过使用以下类

public class Example {

    @SerializedName("sname")
    @Expose
    private String sname;

    @SerializedName("class")
    @Expose
    private String _class;

    @SerializedName("latitude")
    @Expose
    private String latitude;

    @SerializedName("longitude")
    @Expose
    private String longitude;

    /**
    * 
    * @return
    * The sname
    */
    public String getSname() {
        return sname;
    }

    /**
    * 
    * @param sname
    * The sname
    */
    public void setSname(String sname) {
        this.sname = sname;
    }

    /**
    * 
    * @return
    * The _class
    */
    public String getClass_() {
        return _class;
    }

    /**
    * 
    * @param _class
    * The class
    */
    public void setClass_(String _class) {
        this._class = _class;
    }

    /**
    * 
    * @return
    * The latitude
    */
    public String getLatitude() {
        return latitude;
    }

    /**
    * 
    * @param latitude
    * The latitude
    */
    public void setLatitude(String latitude) {
        this.latitude = latitude;
    }

    /**
    * 
    * @return
    * The longitude
    */
    public String getLongitude() {
        return longitude;
    }

    /**
    * 
    * @param longitude
    * The longitude
    */
    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }
}
但它可以生成数百条记录,我需要获取它们并存储在一个数组中 我不知道怎么做。。 范例


有人能帮我吗?

只列出方法返回列表,而不只是对象

  • 创建另一个POJO类,如下所示

    import java.util.ArrayList;
    
    public class DataExample {
    
        private ArrayList<Example> arrayList = new ArrayList<Example>();
    
        public ArrayList<Example> getArrayList() {
            return arrayList;
        }
    
        public void setArrayList(ArrayList<Example> arrayList) {
            this.arrayList = arrayList;
        }
    }
    
    在您的活动中,请执行以下操作:

     class GetExampleData extends AsyncTask<Void, Void, Void> {
    
            @Override
            protected Void doInBackground(Void... params) {
    
                try {
                    APIRequests request = Utils.getRestAdapter().create(
                            APIRequests.class);
                    DataExample mResponseData = request.requestExampleData("userId");
                    // Just for understanding purpose.
                    //you can get your list by accessing the mResponseData.
    
                    Yourlist = mResponseData.getArrayList();
    
                    // Yourlist now have all the values you need.
    
                } catch (Exception ex) {
    
                }
                return null;
            }
        }
    
    类GetExampleData扩展异步任务{
    @凌驾
    受保护的Void doInBackground(Void…参数){
    试一试{
    APIRequests request=Utils.getRestAdapter().create(
    (1.类别),;
    DataExample mResponseData=request.requestExampleData(“用户ID”);
    //只是为了理解目的。
    //您可以通过访问mResponseData来获取您的列表。
    Yourlist=mResponseData.getArrayList();
    //您的列表现在包含了您需要的所有值。
    }捕获(例外情况除外){
    }
    返回null;
    }
    }
    

    就这样。你可以走了

    @down投票人。如果你能提到为什么被否决,那就太好了?
            @FormUrlEncoded
            @POST("/getExampleData")
            DataExample requestExampleData( @Field("userId") String userId);
    
     class GetExampleData extends AsyncTask<Void, Void, Void> {
    
            @Override
            protected Void doInBackground(Void... params) {
    
                try {
                    APIRequests request = Utils.getRestAdapter().create(
                            APIRequests.class);
                    DataExample mResponseData = request.requestExampleData("userId");
                    // Just for understanding purpose.
                    //you can get your list by accessing the mResponseData.
    
                    Yourlist = mResponseData.getArrayList();
    
                    // Yourlist now have all the values you need.
    
                } catch (Exception ex) {
    
                }
                return null;
            }
        }