そのJSON http://rutube.ru/api/video/editors/?page=1&format=jsonを解析するにはどうすればよいですか? 空の表示があります。http://www.androidhive.info/2012/01/android-json-parsing-tutorial/から例を挙げます
JSONPARSER.java
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
int httpStatus = httpResponse.getStatusLine().getStatusCode();
if (httpStatus != HttpStatus.SC_OK) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
}
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return jObj;
}
}
MAINACTIVITY.java
public class MainActivity extends ListActivity {
private static String URL_VIDEO = "http";
private static final String TAG_RESULTS = "results";
private static final String VIDEO_OBJECT = "video";
private static final String VIDEO_ID = "id";
private static final String VIDEO_TITLE = "title";
private static final String VIDEO_CREATED_TS = "created_ts";
JSONArray results = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<HashMap<String, String>> resultList = new ArrayList<HashMap<String,String>>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(URL_VIDEO);
try {
results = json.getJSONArray(TAG_RESULTS);
for (int i = 0; i < results.length(); i++) {
JSONObject c = results.getJSONObject(i);
JSONObject video = c.getJSONObject(VIDEO_OBJECT);
String id = video.getString(VIDEO_ID);
String title = video.getString(VIDEO_TITLE);
String created_ts = video.getString(VIDEO_CREATED_TS);
HashMap<String, String> map = new HashMap<String, String>();
map.put(VIDEO_ID, id);
map.put(VIDEO_TITLE, title);
map.put(VIDEO_CREATED_TS, created_ts);
resultList.add(map);
}
} catch(JSONException e) {
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(this, resultList,
R.layout.list_video_item,
new String[] { VIDEO_OBJECT, VIDEO_TITLE, VIDEO_CREATED_TS }, new int[] { R.id.object, R.id.title, R.id.created_ts });
setListAdapter(adapter);
ListView lv = getListView();
}
}