If you think the Android project Go2-Rennes listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*******************************************************************************
* Copyright (c) 2011 Michel DAVID mimah35-at-gmail.com
* //www.java2s.com
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/package fr.gotorennes.remote;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.util.Log;
import fr.gotorennes.domain.NextDeparture;
publicclass NextDepartureService extends RemoteService<NextDeparture> {
publicstaticfinal String COMMAND = "getbusnextdepartures";
publicstaticfinal String NODE = "stopline";
privatestatic DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz");
privatestatic NextDepartureService instance;
private Date remoteDate;
private NextDepartureService(Context context) {
super();
}
protected String getVersion() {
return"2.2";
}
publicstaticsynchronized NextDepartureService getInstance(Context context) {
if (instance == null) {
instance = new NextDepartureService(context);
}
return instance;
}
public NextDeparture getNextDeparture(String stopId, String routeId, int direction) {
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("mode", "stopline");
parameters.put("stop][", stopId);
parameters.put("route][", routeId);
parameters.put("direction][", String.valueOf(direction));
return loadObject(COMMAND, NODE, parameters);
}
@Override
protected NextDeparture populate(JSONObject jsonObject) throws JSONException {
Log.d("GoToRennes", jsonObject.toString());
NextDeparture nextDeparture = new NextDeparture();
nextDeparture.stopId = jsonObject.getString("stop");
nextDeparture.routeId = jsonObject.getString("route");
nextDeparture.direction = Integer.parseInt(jsonObject.getString("direction"));
JSONObject departure = jsonObject.getJSONObject("departures").optJSONObject("departure");
if (departure == null) {
departure = jsonObject.getJSONObject("departures").getJSONArray("departure").getJSONObject(0);
}
try {
nextDeparture.departure = dateFormat.parse(departure.getString("content"));
}
catch (ParseException ex) {}
nextDeparture.remoteDate = remoteDate;
return nextDeparture;
}
@Override
protectedvoid onDataLoad(JSONObject data) throws JSONException {
JSONObject attributes = data.getJSONObject("@attributes");
try {
remoteDate = dateFormat.parse(attributes.getString("localdatetime"));
}
catch (ParseException ex) {
remoteDate = new Date();
}
}
}