Skip to content

Instantly share code, notes, and snippets.

@Maragues
Created October 1, 2013 10:04
Show Gist options
  • Save Maragues/6776295 to your computer and use it in GitHub Desktop.
Save Maragues/6776295 to your computer and use it in GitHub Desktop.
DataDroid + OkHttp Integration through reflection
public static ConnectionResult execute(...){
[...]
URL url = null;
String outputText = null;
switch (method) {
case GET:
case DELETE:
String fullUrlValue = urlValue;
if (paramBuilder.length() > 0) {
fullUrlValue += "?" + paramBuilder.toString();
}
url = new URL(fullUrlValue);
connection = urlConnectionFactory(url);
break;
case PUT:
case POST:
url = new URL(urlValue);
connection = urlConnectionFactory(url);
connection.setDoOutput(true);
[...]
break;
}
[...]
}
static HttpURLConnection urlConnectionFactory(URL url) throws IOException{
try {
Class<?> okHttpClientClass = Class.forName("com.squareup.okhttp.OkHttpClient");
Object okHttpObject = okHttpClientClass.newInstance();
java.lang.reflect.Method openMethod = okHttpClientClass.getDeclaredMethod("open", URL.class);
return (HttpURLConnection) openMethod.invoke(okHttpObject, url);
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
} catch (ClassNotFoundException e) {
} catch (NoSuchMethodException e) {
} catch (IllegalArgumentException e) {
} catch (InvocationTargetException e) {
}
return (HttpURLConnection) url.openConnection();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment