3 つのフラグメントを持つアクティビティがあり、毎日更新するサーバーから JSON ファイルを取得しようとしています。
私の JSON ファイルはここにあります: http://pagesbyz.com/test.json
フラグメントがあるため、MainActivity
クラスで次のコードを使用しました。
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://pagesbyz.com/test.json");
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
Toast.makeText(getApplicationContext(), result, 2000).show();
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
本当に必要なのはTYPE
、JSON ファイルのフィールドを取得し、それを 3 つのタブに分けて、次のListView
ように配置することです。
上のコードを使用してJSONファイルを読んだら、どのように進めればよいか知りたいです...助けてくれてありがとう:)
各フラグメントに対してクエリを実行してから、TYPE フィールドを探す必要がありますか? その方が簡単でしょうか?
トーストが実行されなかった理由について知りたい...これが私の主なアクティビティのペーストビンです: http://pastebin.com/gKTCeH79