loadJSONArray in Android Mode

edited January 2015 in Android Mode

Hi. I'm having trouble figuring how to load a JSON array from a url in Android mode:

JSONArray values;

void setup() {
  size(400, 516);
  smooth();

  values = loadJSONArray("https://" + "data.cityofchicago.org/resource/t2qc-9pjd.json");

}

void draw() {

  for (int i = 0; i < values.size (); i++) {

    JSONObject congestion = values.getJSONObject(i); 


    Float lat = congestion.getFloat("_south");
    Float lon = congestion.getFloat("_west");
    Float speed = congestion.getFloat("current_speed");


  }
}

When I compile, I always get an error from inside the Android tools. Does anyone know a solution to this?

Thanks!

Tagged:

Answers

  • also, the Internet permissions are checked

  • i've also tried this:

    String lines[] = loadStrings("https://data.cityofchicago.org/resource/t2qc-9pjd.json"); JSONArray values = JSONArray.parse(lines[0]);

    but i get a null result from the values array.

  • i have tried your code in java mode: it works android mode: same error for the method loadJSonArray(); seems that this method does not exist in android mode...

  • @jnchmbrs::

    with the code below i can acceed (android mode) to the jsonArray; i have not tried but i think that you can convert the file to jsonArray...

            import java.net.*;
            import java.io.*;
            JSONArray values;
            String resultat;
    
    
              void setup(){
    
               resultat   = getUrlContents("https://data.cityofchicago.org/resource/t2qc-9pjd.json");
    
            println(resultat);//or save it?
              };
    
    
             void draw(){
    
            //    for (int i = 0; i < values.size (); i++) {
            // 
            //    JSONObject congestion = values.getJSONObject(i); 
            // 
            // 
            //    Float lat = congestion.getFloat("_south");
            //    Float lon = congestion.getFloat("_west");
            //    Float speed = congestion.getFloat("current_speed");
            // 
            // 
            //  }
            }
    
              private String getUrlContents(String ladresse)
              {
                StringBuilder contenu = new StringBuilder();
    
                try
                {
    
                  URL url = new URL(ladresse);
    
    
                  URLConnection urlConnection = url.openConnection();
                  BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
    
                  String line;
    
                  while ((line = bufferedReader.readLine()) != null)
                  {
                    contenu.append(line + "\n");
                    //println(line);
                  }
                  bufferedReader.close();
                }
                catch(Exception e)
                {
                  e.printStackTrace();
                }
                return contenu.toString();
              };
    
  • @akenaton

    OMG thank you so much!! This does work in Android mode.

  • cannot answer to this question!

  • @jnchmbrs::
    
    i have added some method to the sketch in order to get values...dont forget to add the jar for json simple import to your sketch!
    
    
    import java.net.*;
    import java.io.*;
    JSONArray values;
    String resultat;
    import org.json.simple.JSONArray;
    import org.json.simple.JSONValue;
    import org.json.simple.parser.JSONParser;
    import org.json.simple.parser.ParseException;
    import org.json.simple.JSONObject;
    
    
    void setup() {
    
      resultat   = getUrlContents("https://" + "data.cityofchicago.org/resource/t2qc-9pjd.json");
    values = new JSONArray();
      println(resultat);//or save it?
      Object obj= JSONValue.parse(resultat);
      values =(JSONArray)obj;
      recupereValeurs();
    };
    
    
    void draw() {
    }
    /////////////////////////////////////////////////getURL (better with a thread)  
    
    private String getUrlContents(String ladresse)
    {
      StringBuilder contenu = new StringBuilder();
    
      try
      {
    
        URL url = new URL(ladresse);
    
    
        URLConnection urlConnection = url.openConnection();
    
    
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
    
        String line;
    
    
        while ( (line = bufferedReader.readLine ()) != null)
        {
          contenu.append(line + "\n");
          //println(line);
        }
        bufferedReader.close();
      }
      catch(Exception e)
      {
        e.printStackTrace();
      }
      return contenu.toString();
    };
    
    ///////////////////////////////////////////////get values from JSONOjects
    
    private  void recupereValeurs() {
    
      for (int i = 0; i< (values.size()); i++) {
    
        JSONObject obj2= (JSONObject)values.get(i);
        JSONParser parser = new JSONParser();
    
    
        String valeur = (String) obj2.get("_south");
        System.out.println("la coordonnée est======================" + valeur);
      }
    };
    
Sign In or Register to comment.