###前提・実現したいこと
RailsAPIでJSONデータを吐き出し、Androidアプリで取得したいです。
吐き出したJSONデータの型とAndroid側の取得するための型が一致せずエラーになりました。
###発生している問題・エラーメッセージ
06-01 16:54:40.426 19350-19350/station.around.aroundsatation D/debug4: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
###該当のソースコード
APIが吐き出すJSONデータ
JSON
1[{"id":1,"en_title":"http://i.imgur.com/9h81ns6.jpg","ja_title":"新宿駅 東口","en_content":"FirstKitchen Mac Wi-fi","ja_content":"ファーストキッチン マクドナルド ワイファイ","url":{"url":null},"created_at":"2017-05-28T06:43:53.150Z","updated_at":"2017-05-30T04:54:06.455Z"},{"id":2,"en_title":"kamkack;m","ja_title":"mlcdm;kacm;k","en_content":"mcsamk;casm;","ja_content":";dc,lacsl;,","url":{"url":null},"created_at":"2017-05-30T05:31:13.344Z","updated_at":"2017-05-30T05:31:13.344Z"},{"id":3,"en_title":"lmascml;cal;","ja_title":"mcmaclm","en_content":"lm;dcmkdckm","ja_content":"lcdm;cdlm;","url":{"url":null},"created_at":"2017-05-30T05:31:25.030Z","updated_at":"2017-05-30T05:31:25.030Z"},{"id":4,"en_title":"lm;ac;mdc;lm","ja_title":"l;c;lmclm;","en_content":"lddkm","ja_content":"llm;dc;lm","url":{"url":null},"created_at":"2017-05-30T05:31:35.457Z","updated_at":"2017-05-30T05:31:35.457Z"},{"id":5,"en_title":"l;dcmk;dck;","ja_title":"l;msckck;","en_content":"dckmadkm","ja_content":"klmckldc","url":{"url":null},"created_at":"2017-05-30T05:31:46.043Z","updated_at":"2017-05-30T05:31:46.043Z"}]
ImageClient.java
Java
1import retrofit2.Call; 2import retrofit2.http.GET; 3import retrofit2.http.Path; 4import station.around.aroundsatation.model.ListImage; 5 6public interface ImageClient { 7 @GET("/api/v1/images") 8 Call<ListImage> listImages(); 9}
ListImage.java
Java
1import java.util.List; 2 3public class ListImage { 4 public List<Image> image; 5 6 public List<Image> getListImages() { 7 return image; 8 } 9}
MainActivity
Java
1 private void showMember(){ 2 Retrofit retrofit = new Retrofit.Builder() 3 .baseUrl("http://candii.tk/") 4 .addConverterFactory(GsonConverterFactory.create()) 5 .build(); 6 ImageClient service = retrofit.create(ImageClient.class); 7 8 9 Call<ListImage> call2 = service.listImages(); 10 call2.enqueue(new Callback<ListImage>() { 11 @Override 12 public void onResponse(Call<ListImage> call, Response<ListImage> response) { 13 List<Image> listImage = response.body().getListImages(); 14 int s = listImage.size(); 15 for(int i = 0; i < s; i++){ 16 Log.d("debug3", listImage.get(i).getEn_title()); 17 } 18 19 } 20 21 @Override 22 public void onFailure(Call<ListImage> call, Throwable t) { 23 Log.d("debug4", t.getMessage()); 24 } 25 }); 26 }
どのようにJSONデータ型に対応するか検討がつかず質問させていただきます。
ご経験ある方、ご指南お願いします!
追加
エラー内容
Error:(42, 51) エラー: 不適合な型: Call<List<ListImage>>をCall<ListImage>に変換できません: Error:Execution failed for task ':app:compileDebugJavaWithJavac'. > Compilation failed; see the compiler error output for details.
エラー箇所
MainActivity.java
public class MainActivity extends AppCompatActivity { 〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜 private void showMember(){ Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://candii.tk/") .addConverterFactory(GsonConverterFactory.create()) .build(); ImageClient service = retrofit.create(ImageClient.class); Call <ListImage> call2 = service.listImages(); ★ここです。★ call2.enqueue(new Callback<ListImage>() { @Override public void onResponse(Call<ListImage> call, Response<ListImage> response) { List<Image> listImage = response.body().getListImages(); int s = listImage.size(); for(int i = 0; i < s; i++){ Log.d("debug3", listImage.get(i).getEn_title()); } } 〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/06/01 08:41
2017/06/01 08:50 編集
2017/06/01 09:18
2017/06/01 09:22
2017/06/01 09:49
2017/06/01 10:44