Last active
August 29, 2015 14:01
-
-
Save kyle-herzog/51a6916dcf573f10eb80 to your computer and use it in GitHub Desktop.
Java REST helper methods. Requires Gson for serialization to/deserialization from json.
This file contains hidden or 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.kyzog.android; | |
import java.lang.reflect.Type; | |
import java.util.Date; | |
import com.google.gson.JsonDeserializationContext; | |
import com.google.gson.JsonDeserializer; | |
import com.google.gson.JsonElement; | |
import com.google.gson.JsonParseException; | |
public class DotNetDateJsonDeserializer implements JsonDeserializer<Date> { | |
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { | |
String jsonString = json.getAsJsonPrimitive().getAsString(); | |
if(jsonString != null) { | |
jsonString = jsonString.replace("/Date(", ""); | |
int indexOfTimeZone = jsonString.indexOf("+"); | |
if (indexOfTimeZone < 0){ | |
indexOfTimeZone = jsonString.indexOf("-"); | |
} | |
if(indexOfTimeZone > -1){ | |
jsonString = jsonString.substring(0, indexOfTimeZone); | |
} | |
jsonString = jsonString.replace(")/", ""); | |
} | |
return new Date(Long.valueOf(jsonString)); | |
} | |
} |
This file contains hidden or 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.kyzog.android; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.util.Date; | |
import org.apache.http.HttpEntity; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.client.ClientProtocolException; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.client.methods.HttpPost; | |
import org.apache.http.client.methods.HttpPut; | |
import org.apache.http.client.methods.HttpUriRequest; | |
import org.apache.http.entity.StringEntity; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.util.EntityUtils; | |
import android.util.Base64; | |
import com.google.gson.FieldNamingPolicy; | |
import com.google.gson.Gson; | |
import com.google.gson.GsonBuilder; | |
public class RestHelper { | |
public static <OutT extends Object> OutT PerformRequest(String url, String userName, String password, Object payload, HttpMethod method, Boolean keepAlive, Class<OutT> outputClass) | |
throws Exception { | |
OutT returnValue = null; | |
Gson gson = new GsonBuilder() | |
.registerTypeAdapter(Date.class, | |
new DotNetDateJsonDeserializer()) | |
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE) | |
.create(); | |
DefaultHttpClient client = new DefaultHttpClient(); | |
HttpUriRequest methodEntity = null; | |
StringEntity payloadEntity; | |
switch (method) { | |
case DELETE: | |
break; | |
case GET: | |
HttpGet get = new HttpGet(url); | |
methodEntity = get; | |
break; | |
case POST: | |
HttpPost post = new HttpPost(url); | |
payloadEntity = new StringEntity(gson.toJson(payload)); | |
payloadEntity.setContentType("application/json"); | |
post.setEntity(payloadEntity); | |
methodEntity = post; | |
break; | |
case PUT: | |
HttpPut put = new HttpPut(url); | |
payloadEntity = new StringEntity(gson.toJson(payload)); | |
payloadEntity.setContentType("application/json"); | |
put.setEntity(payloadEntity); | |
methodEntity = put; | |
break; | |
} | |
methodEntity.addHeader( | |
"Authorization", | |
"Basic " | |
+ Base64.encodeToString( | |
(userName + ":" + password).getBytes(), | |
Base64.NO_WRAP)); | |
String responsePayload = ""; | |
try { | |
HttpResponse response = client.execute(methodEntity); | |
HttpEntity responseEntity = response.getEntity(); | |
responsePayload = EntityUtils.toString(responseEntity); | |
returnValue = (OutT) gson.fromJson(responsePayload, outputClass); | |
return returnValue; | |
} catch (Exception e) { | |
throw new Exception("There was an error performing a REST call\r\nResponsePayload: " + responsePayload, e); | |
} | |
} | |
public static InputStream PerformStreamRequest(String url, String userName, String password) | |
throws IOException { | |
InputStream returnValue = null; | |
String authString = userName+":"+password; | |
String base64 = Base64.encodeToString(authString.getBytes(), 0); | |
URL urlObject = new URL(url); | |
URLConnection urlConn = urlObject.openConnection(); | |
urlConn.setRequestProperty("Authorization", "Basic " + base64); | |
HttpURLConnection httpConn = (HttpURLConnection) urlConn; | |
httpConn.setRequestProperty("Authorization", "Basic " + base64); | |
httpConn.connect(); | |
returnValue = httpConn.getInputStream(); | |
return returnValue; | |
} | |
public static <OutT extends Object> OutT Post(String url, String userName, String password, Object payload, Class<OutT> outputClass) | |
throws Throwable { | |
return RestHelper.<OutT> PerformRequest(url, userName, password, | |
payload, HttpMethod.POST, true, outputClass); | |
} | |
public static <OutT extends Object> OutT Get(String url, String userName, String password, Class<OutT> outputClass) | |
throws Throwable { | |
return RestHelper.<OutT> PerformRequest(url, userName, password, null, | |
HttpMethod.GET, true, outputClass); | |
} | |
public static <OutT extends Object> OutT Put( String url, String userName, String password, Object payload, Class<OutT> outputClass) | |
throws Throwable { | |
return PerformRequest(url, userName, password, payload, HttpMethod.PUT, | |
true, outputClass); | |
} | |
public static <OutT extends Object, InT extends Object> OutT Delete( String url, String userName, String password, Class<InT> payload, Class<OutT> outputClass) | |
throws Throwable { | |
return PerformRequest(url, userName, password, payload, | |
HttpMethod.DELETE, true, outputClass); | |
} | |
public static InputStream getStream(String url, String userName, | |
String password) throws IOException { | |
return PerformStreamRequest(url, userName, password); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment