Skip to content

Instantly share code, notes, and snippets.

@lukaspili
Created May 23, 2012 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukaspili/2774231 to your computer and use it in GitHub Desktop.
Save lukaspili/2774231 to your computer and use it in GitHub Desktop.
package com.siu.android.tennisparis.json;
import com.google.gson.JsonElement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author Lukasz Piliszczuk <lukasz.pili AT gmail.com>
*/
public class CommonDeserializer {
private static DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
public static Long getLong(JsonElement jsonElement) {
if (null == jsonElement) {
return null;
}
try {
return jsonElement.getAsLong();
} catch (Exception e) {
return null;
}
}
public static Integer getInteger(JsonElement jsonElement) {
if (null == jsonElement) {
return null;
}
try {
return jsonElement.getAsInt();
} catch (Exception e) {
return null;
}
}
public static String getString(JsonElement jsonElement) {
if (null == jsonElement) {
return null;
}
try {
return jsonElement.getAsString();
} catch (Exception e) {
return null;
}
}
public static Double getDouble(JsonElement jsonElement) {
if (null == jsonElement) {
return null;
}
try {
return jsonElement.getAsDouble();
} catch (Exception e) {
return null;
}
}
public static Boolean getBoolean(JsonElement jsonElement) {
if (null == jsonElement) {
return null;
}
try {
return jsonElement.getAsBoolean();
} catch (Exception e) {
return null;
}
}
public static Date getDate(JsonElement jsonElement) {
if (null == jsonElement) {
return null;
}
try {
return dateFormat.parse(jsonElement.getAsString());
} catch (Exception e) {
return null;
}
}
}
public class GsonFormatter {
private static Gson gson = new GsonBuilder()
.registerTypeAdapter(Tennis.class, new TennisDeserializer())
.registerTypeAdapter(Availability.class, new AvailabilityDeserializer())
.create();
public static Gson getGson() {
return gson;
}
}
package com.siu.android.tennisparis.json;
import android.util.Log;
import com.google.gson.*;
import com.siu.android.tennisparis.dao.model.Tennis;
import java.lang.reflect.Type;
/**
* @author Lukasz Piliszczuk <lukasz.pili AT gmail.com>
*/
public class TennisDeserializer implements JsonDeserializer<Tennis> {
private static final String WEBSERVICE_ID = "webservice_id";
private static final String LATITUDE = "latitude";
private static final String LONGITUDE = "longitude";
private static final String NAME = "name";
private static final String ADDRESS = "address";
private static final String POSTAL_CODE = "zip_code";
private static final String CITY = "ville";
private static final String ACTIVE = "is_active";
@Override
public Tennis deserialize(JsonElement json, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
Tennis tennis = new Tennis();
tennis.setWebserviceId(CommonDeserializer.getLong(jsonObject.get(WEBSERVICE_ID)));
tennis.setLatitude(CommonDeserializer.getDouble(jsonObject.get(LATITUDE)));
tennis.setLongitude(CommonDeserializer.getDouble(jsonObject.get(LONGITUDE)));
tennis.setName(CommonDeserializer.getString(jsonObject.get(NAME)));
tennis.setAddress(CommonDeserializer.getString(jsonObject.get(ADDRESS)));
tennis.setPostalCode(CommonDeserializer.getString(jsonObject.get(POSTAL_CODE)));
tennis.setCity(CommonDeserializer.getString(jsonObject.get(CITY)));
tennis.setActive(CommonDeserializer.getBoolean(jsonObject.get(ACTIVE)));
return tennis;
}
}
package com.siu.android.tennisparis.util;
import android.util.Log;
import java.net.MalformedURLException;
import java.net.URL;
/**
* @author Lukasz Piliszczuk <lukasz.pili AT gmail.com>
*/
public final class UrlUtils {
private UrlUtils() {
}
public static URL getUrl(String urlAsString) {
URL url;
try {
url = new URL(urlAsString);
} catch (MalformedURLException e) {
Log.w(UrlUtils.class.getName(), "Invalid format for url : " + urlAsString, e);
return null;
}
return url;
}
}
public class Webservice.java {
private List<Tennis> load() {
String data = getData("http://...");
List<Tennis> tennises = GsonFormatter.getGson().fromJson(data, new TypeToken<List<Tennis>>() {}.getType());
}
private String getData(String urlAsString) {
URL url = UrlUtils.getUrl(urlAsString);
if (null == url) {
Log.e(getClass().getName(), "Invalid URL : " + urlAsString);
return null;
}
InputStream is = null;
String data;
try {
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setUseCaches(false);
urlConnection.setConnectTimeout(15 * 1000);
Log.d(getClass().getName(), "Connection opened to : " + urlAsString);
long time = System.currentTimeMillis();
urlConnection.connect();
is = urlConnection.getInputStream();
data = IOUtils.toString(is);
Log.d(getClass().getName(), "Content downloaded in " + (System.currentTimeMillis() - time) + " ms");
} catch (IOException e) {
Log.e(getClass().getName(), "Error during reading connection stream : " + e.getMessage());
return null;
} finally {
IOUtils.closeQuietly(is);
}
return data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment