Android使用改造来获取所有字段

时间:2015-10-13 10:13:07

标签: android json retrofit

我正在尝试从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;
    }
}

但它可以生成数百条记录,我需要获取它们并存储在数组中 我不知道怎么做.. 示例

[{"sname":"khfksjd","class":"djfhd","latitude":"73.6562320000","longitude":"72.2041200000"},{"sname":"ak","class":"hdhdh","latitude":"30.1394476766","longitude":"77.2564416164"}{"sname":"dffdf","class":"dfdfdff","latitude":"73.6562320000","longitude":"72.2041200000"},{"sname":"adfdfd","class":"hdxcxchdh","latitude":"30.1394476766","longitude":"77.2564416164"}]

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

只需让你的方法返回List< \ YourObjectHere>而不只是 YourObject

答案 1 :(得分:0)

  1. 创建另一个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;
        }
    }
    
  2. 我假设你的界面是这样的:

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

    就是这样。你很高兴!