I am trying to parse data from JSON but my JSON data is an array without a string name. Here is an example:
[
{
"$id": "1",
"ClubVideoId": 1027,
"ClubId": 1,
"Title": "Brian Interview",
"ThumbURL": "url",
"VideoURL": "urll",
"DateAdded": "2014-03-25 00:00"
},
{
"$id": "2",
"ClubVideoId": 1028,
"ClubId": 1,
"Title": "Chase Interview",
"ThumbURL": "url",
"VideoURL": "urll",
"DateAdded": "2014-03-25 00:00"
},
I can seem to pass an Array without a string. Here is my code:
public void handleBlogResponse() {
mProgressBar.setVisibility(View.INVISIBLE);
if (mVideoData == null) {
updateDisplayForError();
}
else {
try {
JSONArray jsonPosts = mVideoData.getJSONArray("");
ArrayList<HashMap<String, String>> clubVideos =
new ArrayList<HashMap<String, String>>();
for (int i = 0; i < jsonPosts.length(); i++) {
JSONObject post = jsonPosts.getJSONObject(i);
String thumburl = post.getString(KEY_THUMBURL);
thumburl = Html.fromHtml(thumburl).toString();
String dateurl = post.getString(KEY_DATEADDED);
dateurl = Html.fromHtml(dateurl).toString();
HashMap<String, String> blogPost = new HashMap<String, String>();
blogPost.put(KEY_THUMBURL, thumburl);
blogPost.put(KEY_DATEADDED, dateurl);
clubVideos.add(blogPost);
}
String[] keys = {KEY_THUMBURL, KEY_DATEADDED};
int[] ids = { android.R.id.text1, android.R.id.text2 };
SimpleAdapter adapter = new SimpleAdapter(this, clubVideos,
android.R.layout.simple_list_item_2, keys, ids);
setListAdapter(adapter);
} catch (JSONException e) {
Log.e(TAG, "Exception caught!!", e);
}
}
}
Any Suggestions?
Here is where I'm pulling my json:
@Override
protected JSONObject doInBackground(Object... arg0) {
int responseCode = -1;
JSONObject jsonResponse = null;
try {
URL blogFeedUrl = new URL("http://x.com/api/Club/Getx/1/?count=" + NUMBER_OF_POSTS);
HttpURLConnection connection = (HttpURLConnection) blogFeedUrl.openConnection();
connection.connect();
responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = connection.getInputStream();
Reader reader = new InputStreamReader(inputStream);
int contentLength = connection.getContentLength();
char[] charArray = new char[contentLength];
reader.read(charArray);
String responseData = new String(charArray);
jsonResponse = new JSONObject(responseData);
}
else {
Log.i(TAG, "Unsuccessful HTTP Response Code: " + responseCode);
}
Log.i(TAG, "Code: " + responseCode);
}
catch (MalformedURLException e) {
logException(e);
}
catch (IOException e) {
logException(e);
}
catch (Exception e) {
logException(e);
}
return jsonResponse;
}'
JSONArray jsonPosts = mVideoData.getJSONArray("");is wrong. what ismVideoData?[...]. For reference this pretty much explains JSON syntax / formatting in one page json.org