###前提・実現したいこと
Android側からRailsAPIのJSONデータを取得したいです。
HTTP接続、GSONでJSONの変換を実装し、最後に取得したデータが意図していないデータでした。
何が取得されていて、なぜ希望のデータを取得できないかがわからないです。
###発生している問題・エラーメッセージ
取得したいデータ
ruby
1[ 2{"id":1, 3"en_title":"http://i.imgur.com/9h81ns6.jpg", 4"ja_title":"新宿駅 東口", 5"en_content":"FirstKitchen Mac Wi-fi", 6"ja_content":"ファーストキッチン マクドナルド ワイファイ", 7"url":{"url":null}, 8"created_at":"2017-05-28T06:43:53.150Z", 9"updated_at":"2017-05-30T04:54:06.455Z"}, 10 11{"id":2, 12"en_title":"kamkack;m", 13"ja_title":"mlcdm;kacm;k", 14"en_content":"mcsamk;casm;", 15"ja_content":";dc,lacsl;,", 16"url":{"url":null}, 17"created_at":"2017-05-30T05:31:13.344Z", 18"updated_at":"2017-05-30T05:31:13.344Z"}, 19 20{"id":3, 21"en_title":"lmascml;cal;", 22"ja_title":"mcmaclm", 23"en_content":"lm;dcmkdckm", 24"ja_content":"lcdm;cdlm;", 25"url":{"url":null}, 26"created_at":"2017-05-30T05:31:25.030Z", 27"updated_at":"2017-05-30T05:31:25.030Z"}, 28 29{"id":4, 30"en_title":"lm;ac;mdc;lm", 31"ja_title":"l;c;lmclm;", 32"en_content":"lddkm", 33"ja_content":"llm;dc;lm", 34"url":{"url":null}, 35"created_at":"2017-05-30T05:31:35.457Z", 36"updated_at":"2017-05-30T05:31:35.457Z"}, 37 38{"id":5, 39"en_title":"l;dcmk;dck;", 40"ja_title":"l;msckck;", 41"en_content":"dckmadkm", 42"ja_content":"klmckldc", 43"url":{"url":null}, 44"created_at":"2017-05-30T05:31:46.043Z", 45"updated_at":"2017-05-30T05:31:46.043Z"} 46]
取得したデータ(Logです)
Java
1D/debug3: station.around.aroundsatation.model.ListImage@7082e1 2D/debug3: station.around.aroundsatation.model.ListImage@2ce7806 3D/debug3: station.around.aroundsatation.model.ListImage@51f1ec7 4D/debug3: station.around.aroundsatation.model.ListImage@e8aecf4 5D/debug3: station.around.aroundsatation.model.ListImage@e116c1d
###該当のソースコード
MainActivity.java
Java
1package station.around.aroundsatation; 2 3import android.support.v7.app.AppCompatActivity; 4import android.os.Bundle; 5import android.widget.TextView; 6import station.around.aroundsatation.model.Image; 7import retrofit2.Call; 8import retrofit2.Callback; 9import retrofit2.Response; 10import retrofit2.Retrofit; 11import retrofit2.converter.gson.GsonConverterFactory; 12import station.around.aroundsatation.client.ImageClient; 13import station.around.aroundsatation.model.ListImage; 14 15import java.util.ArrayList; 16import java.util.List; 17import android.util.Log; 18 19public class MainActivity extends AppCompatActivity { 20 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 super.onCreate(savedInstanceState); 24 setContentView(R.layout.activity_main); 25 26 TextView textView = (TextView) findViewById(R.id.text_view); 27 28 //Imageインスタンス生成 29 Image image = new Image(); 30 image.en_title = "英語のタイトル"; 31 32 textView.setText(image.en_title); 33 showMember(); 34 } 35 36 private void showMember(){ 37 Retrofit retrofit = new Retrofit.Builder() 38 .baseUrl("http://candii.tk/") 39 .addConverterFactory(GsonConverterFactory.create()) 40 .build(); 41 ImageClient service = retrofit.create(ImageClient.class); 42 43 44 Call <List<ListImage>> call2 = service.listImages(); 45 call2.enqueue(new Callback<List<ListImage>>() { 46 @Override 47 public void onResponse(Call<List<ListImage>> call, Response<List<ListImage>> response) { 48 List<ListImage> listImage = response.body(); 49 int s = listImage.size(); 50 51 TextView textView; 52 textView = (TextView)findViewById(R.id.text_view2); 53 54 for(int i = 0; i < s; i++){ 55 Log.d("debug3", listImage.get(i).toString()); 56 textView.setText(listImage.get(i).toString()); 57 } 58 59 } 60 61 @Override 62 public void onFailure(Call<List<ListImage>> call, Throwable t) { 63 Log.d("debug4", t.getMessage()); 64 } 65 }); 66 67 } 68} 69
ImageClient.java
Java
1 2import retrofit2.Call; 3import retrofit2.http.GET; 4import retrofit2.http.Path; 5import station.around.aroundsatation.model.ListImage; 6import java.util.List; 7 8public interface ImageClient { 9 @GET("/api/v1/images") 10 Call <List<ListImage>> listImages(); 11}
ListImage.java
Java
1 2import java.util.List; 3 4public class ListImage { 5 public List<ListImage> image; 6 7 public List<ListImage> getListImages() { 8 return image; 9 } 10} 11
bundle.gradle
〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜 testCompile 'junit:junit:4.12' compile 'com.squareup.okhttp3:logging-interceptor:3.3.1' compile 'com.google.code.gson:gson:2.7' compile 'com.squareup.retrofit2:retrofit:2.1.0' compile 'com.squareup.retrofit2:converter-gson:2.1.0' 〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜
ご経験ある方、ご助言お願いします!
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/06/01 12:40