1
0
mirror of https://github.com/balkian/Carrousel-Android.git synced 2024-12-22 21:18:13 +00:00

I14: Resultados de la red (json)

This commit is contained in:
Fernando Herrera 2011-03-10 01:49:57 +01:00
parent aaf212fda3
commit fb858d96a8
2 changed files with 67 additions and 14 deletions

View File

@ -3,6 +3,7 @@
package="com.onirica.carrousel" package="com.onirica.carrousel"
android:versionCode="1" android:versionCode="1"
android:versionName="1.0"> android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name"> <application android:icon="@drawable/icon" android:label="@string/app_name">

View File

@ -1,15 +1,23 @@
package com.onirica.carrousel; 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.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Service; import android.app.Service;
import android.content.Intent; import android.content.Intent;
import android.os.Binder; import android.os.Binder;
import android.os.IBinder; import android.os.IBinder;
import android.util.Log;
public class Results extends Service { public class Results extends Service {
private final IBinder mBinder = new LocalBinder(); private final IBinder mBinder = new LocalBinder();
@ -49,22 +57,66 @@ public class Results extends Service {
private HashMap<String, Match> retrieveMatches() { private HashMap<String, Match> retrieveMatches() {
HashMap<String, Match> matches = new HashMap<String, Match>(); HashMap<String, Match> matches = new HashMap<String, Match>();
try {
Match match = new Match("bbva1", "Deportivo", "Atelico de Madrid"); URL url = new URL("http://192.168.1.10/matches.json");
matches.put(match.getId(), match); URLConnection urlConnection = url.openConnection();
BufferedReader in = new BufferedReader(
match = new Match("bbva2", "Betis", "Sevilla"); new InputStreamReader(
matches.put(match.getId(), match); urlConnection.getInputStream()));
String line;
match = new Match("bbva3", "Oviedo", "Sporting", Match.State.MATCH_SECOND_ROUND, 84); StringBuilder builder = new StringBuilder();
while ((line = in.readLine()) != null) {
match = new Match("bbva13", "Madrid", "Barcelona", Match.State.MATCH_FIRST_ROUND, 13); builder.append(line);
Match.Goal goal = match.new Goal(12, true, "Cristiano Ronaldo"); }
match.addGoal(goal); String jString = builder.toString();
matches.put(match.getId(), match);
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; return matches;
} }
private class pollingTask extends TimerTask { private class pollingTask extends TimerTask {
@Override @Override