2012-03-09 12 views
2

私は現在、私のアンドロイドプロジェクトに次のコードを持って:春AndroidのRestTemplateに探してSpring Android RestTemplateでリストを返すことはできますか?

protected List<Facility> doInBackground(String... params) { 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpGet httpGet = new HttpGet(<SomeWebService>); 
      httpGet.addHeader("Accept", "application/json"); 
      ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
      try{ 
       Gson gson = new Gson(); 
       Type collectionType = new TypeToken<Collection<Facility>>(){}.getType(); 
       List<Facility> facilities= gson.fromJson(httpClient.execute(httpGet, responseHandler), collectionType); 
       return facilities; 
      }catch(Exception e){ 
       e.printStackTrace(); 
      } 
      return null; 
     } 

を、私はポイントをGoogleで検索すべて(この場合は施設に)リストにJSON配列をオンにする方法を参照してくださいません私はオブジェクトに。私は、Spring-Androidを使用すると上記のコードをよりシンプルに/クリーナーにすることができるかどうかを見ていましたが、私には何の示唆もしていません。どのように私は春のAndroidで上記のコードを書いて、具体的に私はテンプレートからリストを取得するでしょうか?

答えて

4

一般的な情報は、実行時にaviableではありませんので、あなたはこのトリックを必要とする:それは下盛

public static class FacilityList extends ArrayList<Facility>{ 
} 

... 

FacilityListfacilities= gson.fromJson(
    httpClient.execute(httpGet, responseHandler), 
    FacilityList.class); 
0

は別の解決策は、配列としてリストを取得し、それを変換することができ作るためにList<Facility>のサブクラスを作成します。

Type collectionType = new TypeToken<Facility[]>(){}.getType(); 
Facility[] facilities= gson.fromJson(httpClient.execute(httpGet, responseHandler), collectionType); 
return Arrays.asList(facilities); 
関連する問題