Last active
December 31, 2015 19:29
-
-
Save mburumaxwell/8034032 to your computer and use it in GitHub Desktop.
Using date time fields in android(Java) against a C# api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.myapphelper.util; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.util.Date; | |
import java.util.List; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.HttpStatus; | |
import org.apache.http.ParseException; | |
import org.apache.http.client.ClientProtocolException; | |
import org.apache.http.client.HttpClient; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.client.methods.HttpUriRequest; | |
import org.apache.http.conn.params.ConnManagerParams; | |
import org.apache.http.entity.StringEntity; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.params.HttpConnectionParams; | |
import org.apache.http.params.HttpParams; | |
import org.apache.http.util.EntityUtils; | |
import com.google.gson.Gson; | |
import com.google.gson.GsonBuilder; | |
import com.myapp.Constants; | |
import com.myapp.RecordModel; | |
import com.myapp.AppHelper; | |
public class ApiManager { | |
static final int HTTP_REQUEST_TIMEOUT_MS = 30 * 1000; | |
private final Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new UtcDateGsonAdapter()).create(); | |
private final HttpClient httpClient = new DefaultHttpClient(); | |
private final HttpParams httpParams = httpClient.getParams(); | |
public ApiManager() { | |
HttpConnectionParams.setConnectionTimeout(httpParams, HTTP_REQUEST_TIMEOUT_MS); | |
HttpConnectionParams.setSoTimeout(httpParams, HTTP_REQUEST_TIMEOUT_MS); | |
ConnManagerParams.setTimeout(httpParams, HTTP_REQUEST_TIMEOUT_MS); | |
} | |
public Gson getGson() { return gson; } | |
public HttpResponse execute(HttpUriRequest request) throws ClientProtocolException, IOException { | |
request.setHeader("Accept", "application/json"); | |
request.setHeader("Content-Type", "application/json"); | |
return httpClient.execute(request); | |
} | |
public List<RecordModel> getRecords(String recid, Date starting, Date ending) throws ClientProtocolException, IOException { | |
HttpGet get = new HttpGet("http://www.mywebste.com/api/records?recid=" | |
+ recid + "&start=" + AppHelper.unixToWindowsTime(starting.getTime()) | |
+ "&end=" + AppHelper.unixToWindowsTime(ending.getTime())); | |
HttpResponse response = execute(get); | |
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { | |
String responseString = EntityUtils.toString(response.getEntity()); | |
List<RecordModel> records = getGson().fromJson(responseString, Constants.RecordListType); | |
return records; | |
} | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.myapphelper.util; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.GregorianCalendar; | |
import java.util.TimeZone; | |
public class AppHelper { | |
public static final long UNIX_WINDOWS_TIME_DIFF_MILLIS = 62135596800000L; | |
public static final int TICKS_PER_MILLISECOND = 10000; | |
public static long unixToWindowsTime(long unixMillis) { | |
return ((unixMillis + UNIX_WINDOWS_TIME_DIFF_MILLIS) * TICKS_PER_MILLISECOND); | |
} | |
public static long windowsToUnixTime(long windowsTicks) { | |
return ((windowsTicks / TICKS_PER_MILLISECOND) - UNIX_WINDOWS_TIME_DIFF_MILLIS); | |
} | |
public static Date toUtcTime(long localTimeMillis) { | |
Calendar c = Calendar.getInstance(); | |
int offset = c.getTimeZone().getOffset(c.getTimeInMillis()); | |
GregorianCalendar utc = new GregorianCalendar(TimeZone.getTimeZone("UTC")); | |
utc.setTimeInMillis(localTimeMillis); | |
utc.add(Calendar.MILLISECOND, -offset); | |
return utc.getTime(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.myapphelper.util; | |
import java.lang.reflect.Type; | |
import java.text.DateFormat; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.Locale; | |
import java.util.TimeZone; | |
import com.google.gson.JsonDeserializationContext; | |
import com.google.gson.JsonDeserializer; | |
import com.google.gson.JsonElement; | |
import com.google.gson.JsonPrimitive; | |
import com.google.gson.JsonSerializationContext; | |
import com.google.gson.JsonSerializer; | |
public class UtcDateGsonAdapter implements JsonDeserializer<Date>, JsonSerializer<Date> { | |
private final DateFormat dateFormat; | |
public UtcDateGsonAdapter() { | |
dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US); | |
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); | |
} | |
@Override | |
public synchronized Date deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) { | |
try { | |
String str = jsonElement.getAsString(); | |
if (str.contains(".")) { | |
str = str.substring(0, str.indexOf(".")); | |
str += "Z"; | |
} | |
return dateFormat.parse(str); | |
} catch (java.text.ParseException e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
@Override | |
public JsonElement serialize(Date date, Type type, JsonSerializationContext jsonSerializationContext) { | |
return new JsonPrimitive(dateFormat.format(date)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment