-1

ANDROID BEGINS

String JsonFeeds = object.getString("feeds");
JSONArray feeds = new JSONArray(JsonFeeds);
int feed_id;
String feed_radio, feed_title, feed_api, feed_frequency, feed_type, feed_date;


    for (int i=0; i < feeds.length();  i++){
       String feed_data = feeds.getString(i);
       JSONArray array_feed = new JSONArray(feed_data);
       for (int j=0; j < array_feed.length();  j++) {
          JSONObject row = array_feed.getJSONObject(j);
          feed_title = row.getString("feed_title");
          tv.setText(feed_title);
       }
    }

I'm just a beginner in android am trying to figure out how to do the follow loop

EXPECTED LOOP IF IT WAS PHP

foreach($array[feeds] as $feed_key => $array_feed_data){
    echo "<div>";
        echo "<span>" . $array[feeds][$feed_key][val1] . "<span>";
        echo "<span>" . $array[feeds][$feed_key][val2] . "<span>";
        echo "<span>" . $array[feeds][$feed_key][val3] . "<span>";
        echo "<span> <a href='$downloadlink'> Download </a><span>";
    echo "</div>";  
}

The problem I'm having with the android, i can't get to feeds[array_feed_id][my_value_to_show]

EDIT

 String JsonFeeds = object.getString("feeds");
 JSONArray feeds = new JSONArray(JsonFeeds);

JSONObject feeds = object.getJSONArray("feeds");

                   for (String key : feeds.getKeys()) {
                       JSONObject row = feeds.getJSONObject(key);
                       feed_title = row.getString("feed_title");
                       tv.setText(feed_title);
                   }
  • 1
    Can we see an example of the JSON you're parsing? – Michael Dodd Jan 25 '17 at 14:30
  • 1
    http://mobiledatabook.com/smsradio/voice/v1/radio/feeds.php?feeds_to_array=ok remove ?feeds_to_array=ok to see only json data otherwise it ll show both php_array and json.... i hope is what you are asking – LazyLoading Jan 25 '17 at 14:32
  • Try removing `?feeds_to_array=ok` from the end of the URL. That's displaying the feed as a PHP array rather than JSON, which may be why your PHP code is working fine. – Michael Dodd Jan 25 '17 at 14:35
  • I thought you need to know how the json is look? cos in android it has been converted to suite the environment – LazyLoading Jan 25 '17 at 14:38
  • 1
    Ah ok, just double checking you weren't accidentally using the wrong data format. – Michael Dodd Jan 25 '17 at 14:39
  • This is how i get the data from web URL mdb; mdb = new URL("http://192.168.23.1/smsradio/voice/v1/radio/feeds.php"); BufferedReader in = new BufferedReader(new InputStreamReader(mdb.openStream())); String jsonArray = in.readLine().toString(); in.close(); JSONObject object = (JSONObject) new JSONTokener(jsonArray).nextValue(); – LazyLoading Jan 25 '17 at 14:40

1 Answers1

0

The feed is itself a JSONObject, not a JSONArray as its elements are named. Give this a try:

JSONObject feeds = objects.getJSONObject("feeds");

Iterator<String> keys = feeds.keys();
while (keys.hasNext()) {
    JSONObject row = feeds.getJSONObject(keys.next());
    feed_title = row.getString("feed_title");
    tv.setText(feed_title);
}
Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
  • String JsonFeeds = object.getString("feeds"); JSONArray feeds = new JSONArray(JsonFeeds); //your code is below; how do i edit cos **JSONObject feeds = object.getJSONArray("feeds");** is giving error JSONObject feeds = object.getJSONArray("feeds"); for (String key : feeds.getKeys()) { JSONObject row = feeds.getJSONObject(key); feed_title = row.getString("feed_title"); tv.setText(feed_title); } – LazyLoading Jan 25 '17 at 15:14
  • You can edit your original post by clicking the "edit" button underneath the category tags. Better to do that as code does not show up very well in comments. – Michael Dodd Jan 25 '17 at 15:16
  • What error are you getting? Looking at [the sample feed you provided](http://mobiledatabook.com/smsradio/voice/v1/radio/feeds.php), `feeds` is a JSONObject, not a String. – Michael Dodd Jan 25 '17 at 15:18
  • If i comment JSONArray feeds = new JSONArray(JsonFeeds); your JSONObject feeds = object.getJSONArray("feeds"); its incompatible types. Required: org.json.JSONObject Found: org.json.JSONArray and getKeys() is red too – LazyLoading Jan 25 '17 at 15:23
  • Ah yes sorry, typo on my part, corrected my answer. You also don't need the `JSONArray feeds = new JSONArray(JsonFeeds);` line anymore. – Michael Dodd Jan 25 '17 at 15:24
  • Quite well now but getKeys() says cannot resolve method – LazyLoading Jan 25 '17 at 15:28
  • Yeah another typo sorry. It's been a long day! Updated. – Michael Dodd Jan 25 '17 at 15:37
  • keys() says not compatible with java.util.Iterator – LazyLoading Jan 25 '17 at 15:49
  • @LazyLoading That makes no sense, as `keys()` [returns an `Iterator`](https://developer.android.com/reference/org/json/JSONObject.html#keys()). This answer so far has just been from the top of my head. Give me a few hours and I'll investigate it properly at home. – Michael Dodd Jan 25 '17 at 15:51
  • That's me getting confused [between Iterators and Iterables](http://stackoverflow.com/questions/11216994/why-does-java-not-allow-foreach-on-iterators-only-on-iterables). Updated answer. Again, this is from memory, I'm not able to test this until later, apologies. – Michael Dodd Jan 25 '17 at 16:08
  • am able to see the sunshine you made it... Thanks – LazyLoading Jan 25 '17 at 16:19