私はいくつかの答えを調べましたが、なぜ私のものが正確に失敗しているのかわかりません...
コードは次のようになります
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
String json = EntityUtils.toString(httpEntity);
//Convert to JsonArray
JSONArray jsonArray = new JSONArray(json);
Log.i(DEBUG_TAG, Integer.toString(jsonArray.length()));
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Log.i(DEBUG_TAG, jsonObject.getString(KEY_ID));
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(KEY_ID, jsonObject.getString(KEY_ID));
map.put(KEY_TITLE, jsonObject.getString(KEY_TITLE));
map.put(KEY_ARTIST, jsonObject.getString(KEY_ARTIST));
map.put(KEY_DURATION, jsonObject.getString(KEY_DURATION));
map.put(KEY_VOTECOUNT, jsonObject.getString(KEY_VOTECOUNT));
map.put(KEY_THUMB_URL, jsonObject.getString(KEY_THUMB_URL));
map.put(KEY_GENRE, jsonObject.getString(KEY_GENRE));
//Adding map to ArrayList
if (Integer.parseInt(jsonObject.getString(KEY_VOTECOUNT)) == -1){
//If VoteCount is -1 then add to header
headerList.add(map);
}else {
songsList.add(map);
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String jsonでlogcatを実行すると、次のような正しい情報が表示されるようです...
{
"userdata": [
{
"id": "8",
"title": "Baby One More Time",
"artist": "Britney Spears",
"duration": "03:24:00",
"votes": "0",
"thumb_url": "http://api.androidhive.info/music/images/dido.png",
"genre": null
},
{
"id": "2",
"title": "As Long As You Love Me",
"artist": "Justin Bieber",
"duration": "05:26:00",
"votes": "0",
"thumb_url": "http://api.androidhive.info/music/images/enrique.png",
"genre": "Rock"
}
]
}
とlogcat
JSONArray jsonArray = new JSONArray(json);
jsonArray.length()
10-31 22:57:28.433:W / CustomizedListView(26945):エラー!無効なインデックス0、サイズは0です
私にお知らせください
ありがとうございました、