2015-10-13 69 views
-2

我想從JSON獲取一些字段。問題是,如果只產生一個那麼我就可以使用下面的類Android獲取使用改進的所有字段

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":"Akshay","class":"MCA","latitude":"73.6562320000","longitude":"72.2041200000"}] 

得到它很容易,但它可以產生數記錄,我需要他們獲取和存儲陣列中的 我不知道該怎麼做.. 例如

[{"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"}] 

誰能幫助我嗎?

回答

0

只是讓你方法的返回列表< \ YourObjectHere>,而不是僅僅YourObject

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

我假設你的界面是這樣的:

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

完蛋了。你已準備好出發!

+0

@Downvoter。如果你提到它爲什麼被低估,那將會很棒。 –

相關問題