###前提・実現したいこと
RailsAPIからJSONデータをカラムごとにAndroid側で取得したいです。
取得するデータは以下です。(en_titleカラムの値を取得したいです。)
[{"id":1,"en_title":"Shinjuku East Exit","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-28T06:43:53.150Z"}]
###発生している問題・エラーメッセージ
HTTP通信でテキストデータで取得できるのですが、
JSONデータだと表示できません。
①HTTP通信で流れてきた情報を文字列型で取得
②①をJSONオブジェクトに変換
③特定のカラム(en_title)で取得
の流れを想定しています。
Java
1public class HttpApi extends Activity implements View.OnClickListener { 2 private final static int WC = LinearLayout.LayoutParams.WRAP_CONTENT; 3 private final static int MP = LinearLayout.LayoutParams.MATCH_PARENT; 4 private final static String TAG_READ = "read"; 5 private EditText editText; 6 private String text; 7 private Handler handler = new Handler(); 8 private JSONObject json; 9 private String jsonObject; 10 11 //テキストファイルのURLの指定(1) 12 private final static String URL = 13 "http://candii.tk/api/v1/images"; 14 15 //アクティビティ起動時に呼ばれる 16 @Override 17 public void onCreate(Bundle bundle) { 18 super.onCreate(bundle); 19 requestWindowFeature(Window.FEATURE_NO_TITLE); 20 21 //レイアウトの生成 22 LinearLayout layout = new LinearLayout(this); 23 layout.setBackgroundColor(Color.WHITE); 24 layout.setOrientation(LinearLayout.VERTICAL); 25 setContentView(layout); 26 27 //エディットテキストの生成 28 editText = new EditText(this); 29 editText.setText(""); 30 editText.setLayoutParams(new LinearLayout.LayoutParams(MP, WC)); 31 layout.addView(editText); 32 33 //ボタンの生成 34 layout.addView(makeButton("HTTP通信", TAG_READ)); 35 } 36 37 //ボタンの生成 38 private Button makeButton(String text, String tag) { 39 Button button = new Button(this); 40 button.setText(text); 41 button.setTag(tag); 42 button.setOnClickListener(this); 43 button.setLayoutParams(new LinearLayout.LayoutParams(WC, WC)); 44 return button; 45 } 46 47 //ボタンクリック時に呼ばれる 48 public void onClick(View v) { 49 String tag = (String)v.getTag(); 50 if (TAG_READ.equals(tag)) { 51 //スレッドの生成(4) 52 Thread thread = new Thread(new Runnable() {public void run() { 53 //HTTP通信 54 try { 55 text = new String(http2data(URL)); 56 } catch (Exception e) { 57 text = null; 58 } 59 //ハンドラの生成(5) 60 handler.post(new Runnable() {public void run() { 61 if (text != null) { 62 editText.setText(text); 63 } 64 }}); 65 }}); 66 thread.start(); 67 } 68 } 69 70 //HTTP通信 71 public static String http2data(String path) throws Exception { 72 byte[] w=new byte[1024]; 73 HttpURLConnection c = null; 74 InputStream in = null; 75 ByteArrayOutputStream out = null; 76 try { 77 //HTTP接続のオープン(2) 78 URL url = new URL(path); 79 c = (HttpURLConnection)url.openConnection(); 80 c.setRequestMethod("GET"); 81 c.connect(); 82 in = c.getInputStream(); 83 String readSt = readInputStream(in); ---① 84 JSONObject jsonData = new JSONObject(readSt); ---② 85 String st = jsonData.getString("en_title"); ---③ 86 87 88 89 //HTTP接続のクローズ(3) 90 in.close(); 91 c.disconnect(); 92 return st; 93 94 } catch (Exception e) { 95 try { 96 if (c != null) c.disconnect(); 97 if (in != null) in.close(); 98 if (out != null) out.close(); 99 } catch (Exception e2) { 100 } 101 throw e; 102 } 103 } 104 105 public static String readInputStream(InputStream in) throws IOException, UnsupportedEncodingException { 106 StringBuffer sb = new StringBuffer(); 107 String st = ""; 108 109 BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8")); 110 while((st = br.readLine()) != null) 111 { 112 sb.append(st); 113 } 114 try 115 { 116 in.close(); 117 } 118 catch(Exception e) 119 { 120 e.printStackTrace(); 121 } 122 123 return sb.toString(); 124 } 125}
①までの結果
取得できたもの
[{"id":1,"en_title":"Shinjuku East Exit","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-28T06:43:53.150Z"}]
③までの結果
こちらは何も取得できませんでした。。。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。