diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 7919edf..0b9b3ff 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -3,6 +3,7 @@ package="com.onirica.carrousel" android:versionCode="1" android:versionName="1.0"> + diff --git a/src/com/onirica/carrousel/Results.java b/src/com/onirica/carrousel/Results.java index 607c3c2..6cd1d10 100644 --- a/src/com/onirica/carrousel/Results.java +++ b/src/com/onirica/carrousel/Results.java @@ -1,15 +1,23 @@ package com.onirica.carrousel; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.URL; +import java.net.URLConnection; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Timer; import java.util.TimerTask; +import org.json.JSONArray; +import org.json.JSONObject; + import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; +import android.util.Log; public class Results extends Service { private final IBinder mBinder = new LocalBinder(); @@ -49,22 +57,66 @@ public class Results extends Service { private HashMap retrieveMatches() { HashMap matches = new HashMap(); - - Match match = new Match("bbva1", "Deportivo", "Atelico de Madrid"); - matches.put(match.getId(), match); - - match = new Match("bbva2", "Betis", "Sevilla"); - matches.put(match.getId(), match); - - match = new Match("bbva3", "Oviedo", "Sporting", Match.State.MATCH_SECOND_ROUND, 84); - - match = new Match("bbva13", "Madrid", "Barcelona", Match.State.MATCH_FIRST_ROUND, 13); - Match.Goal goal = match.new Goal(12, true, "Cristiano Ronaldo"); - match.addGoal(goal); - matches.put(match.getId(), match); + try { + URL url = new URL("http://192.168.1.10/matches.json"); + URLConnection urlConnection = url.openConnection(); + BufferedReader in = new BufferedReader( + new InputStreamReader( + urlConnection.getInputStream())); + String line; + StringBuilder builder = new StringBuilder(); + while ((line = in.readLine()) != null) { + builder.append(line); + } + String jString = builder.toString(); + + JSONObject jObject; + jObject = new JSONObject(jString); + JSONArray matchesArray = jObject.getJSONArray("matches"); + if (matchesArray == null) + throw new Exception("No matches object in json response"); + + for (int i = 0; i < matchesArray.length(); i++) { + JSONObject matchObject = matchesArray.getJSONObject(i); + + String id = matchObject.getString("id"); + String localTeam = matchObject.getString("localTeam"); + String visitorTeam = matchObject.getString("visitorTeam"); + int min = (int) matchObject.getLong("min"); + String stateStr = matchObject.getString("state"); + Match.State state; + if (stateStr == "NOT STARTED") + state = Match.State.MATCH_NO_STARTED; + else if (stateStr == "FIRST ROUND") + state = Match.State.MATCH_FIRST_ROUND; + else if (stateStr == "HALF TIME") + state = Match.State.MATCH_HALF_TIME; + else if (stateStr == "SECOND ROUND") + state = Match.State.MATCH_SECOND_ROUND; + else if (stateStr == "ENDED") + state = Match.State.MATCH_ENDED; + else + state = Match.State.MATCH_NO_STARTED; + Match match = new Match(id, localTeam, visitorTeam, state, min); + JSONArray goalsArray = matchObject.getJSONArray("goals"); + if (goalsArray != null) { + for (int j = 0; j < goalsArray.length(); j++) { + JSONObject goalObject = goalsArray.getJSONObject(j); + int goalMin = (int) goalObject.getLong("min"); + boolean isLocal = goalObject.getBoolean("isLocal"); + String who = goalObject.getString("who"); + Match.Goal goal = match.new Goal (goalMin, isLocal, who); + match.addGoal(goal); + } + } + matches.put(match.getId(), match); + } + } catch (Exception e) { + Log.v("Results", "Cannot parse matches json info: " + e.getMessage()); + } return matches; -} + } private class pollingTask extends TimerTask { @Override