private void getTrafficSpots() { controller.showProgressBar(); String uploadWebsite = "http://" + controller.selectedCity.URL + "/php/trafficstatus.cache?dummy=ert43"; String[] ArrayOfData = null; StreamConnection c = null; InputStream s = null; StringBuffer b = new StringBuffer(); String url = uploadWebsite; System.out.print(url); try { c = (StreamConnection) Connector.open(url); s = c.openDataInputStream(); int ch; int k = 0; while ((ch = s.read()) != -1) { System.out.print((char) ch); b.append((char) ch); } // System.out.println("b"+b); try { JSONObject ff1 = new JSONObject(b.toString()); String data1 = ff1.getString("locations"); JSONArray jsonArray1 = new JSONArray(data1); Vector TrafficStatus = new Vector(); for (int i = 0; i < jsonArray1.length(); i++) { System.out.println(jsonArray1.getJSONArray(i).getString(3)); double aDoubleLat = Double.parseDouble(jsonArray1.getJSONArray(i).getString(1)); double aDoubleLon = Double.parseDouble(jsonArray1.getJSONArray(i).getString(2)); System.out.println(aDoubleLat + " " + aDoubleLon); TrafficStatus.addElement( new LocationPointer( jsonArray1.getJSONArray(i).getString(3), (float) aDoubleLon, (float) aDoubleLat, 1, true)); } controller.setCurrentScreen(controller.TrafficSpots(TrafficStatus)); } catch (Exception E) { controller.setCurrentScreen(traf); new Thread() { public void run() { controller.showAlert("Error in network connection.", Alert.FOREVER, AlertType.INFO); } }.start(); } } catch (Exception e) { controller.setCurrentScreen(traf); new Thread() { public void run() { controller.showAlert("Error in network connection.", Alert.FOREVER, AlertType.INFO); } }.start(); } }
public void fillInEchoNestInfo() throws IOException, EchoNestException { if (artist == null || title == null) { JSONObject songProfile = null; try { songProfile = EchoNestQuery.get("song", "profile", "id=" + EchoNestId + "&bucket=audio_summary"); } catch (EchoNestException e) { } JSONObject song = songProfile.optJSONArray("songs").optJSONObject(0); artist = new Artist(song.optString("artist_name"), song.optString("artist_id")); title = song.optString("title"); duration = song.optJSONObject("audio_summary").optInt("duration"); } else if (EchoNestId == null) { String artistName = URLEncoder.encode(artist.getName(), "UTF-8"); String title = URLEncoder.encode(this.title, "UTF-8"); JSONObject songSearch = null; try { songSearch = EchoNestQuery.get( "song", "search", "artist=" + artistName + "&title=" + title + "&results=1" + "&bucket=audio_summary"); } catch (EchoNestException e) { } JSONArray songs = songSearch.optJSONArray("songs"); if (songs.length() == 0) throw new EchoNestException(6, "No items found."); JSONObject song = songs.optJSONObject(0); artist = new Artist(song.optString("artist_name"), song.optString("artist_id")); EchoNestId = song.optString("id"); duration = song.optJSONObject("audio_summary").optInt("duration"); } else if (duration == -1) { JSONObject songProfile = null; try { songProfile = EchoNestQuery.get("song", "profile", "id=" + EchoNestId + "&bucket=audio_summary"); } catch (EchoNestException e) { } duration = songProfile .optJSONArray("songs") .optJSONObject(0) .optJSONObject("audio_summary") .optInt("duration"); } }
// El string debe estar em formato json public boolean updateRegistry(String dataJson) { try { // Crea objeto json con string por parametro JSONObject json = new JSONObject(dataJson); // Obtiene el array de Registos JSONArray arr = json.getJSONArray("Registry"); // Recorre el array for (int i = 0; i < arr.length(); i++) { // Obtiene los datos idSensor y value int idSensor = arr.getJSONObject(i).getInt("idSensor"); int value = arr.getJSONObject(i).getInt("value"); // Recorre la configuracion de registro for (RegistryConf reg : registryConf) { // Se fija si el registro corresponde a esta configuracion if (reg.getIdSensor() == idSensor) { // Checkea el criterio para guardar, o no en la BD // Checkea tambien si el valor es igual al anterior if (reg.getSaveTypeString() == "ONCHANGE" && lastRead.get(idSensor) != value) { // Actualizo la ultima lectura y guardo en la BD lastRead.put(idSensor, value); saveRegistry(idSensor, value); } else if (reg.getSaveTypeString() == "ONTIME") { // Variables auxiliares, para checkear tiempo Long auxLong = System.currentTimeMillis() / 1000; int now = auxLong.intValue(); int timeToSave = lastRead.get(idSensor) + reg.getValue(); // Checkea si ya es tiempo para guerdar un nuevo registro if (now >= timeToSave) { // Actualizo el ultimo guardado lastRead.put(idSensor, now); saveRegistry(idSensor, value); } } } } } } catch (Exception e) { e.printStackTrace(); } return false; }