Created
October 1, 2013 10:04
-
-
Save Maragues/6776295 to your computer and use it in GitHub Desktop.
DataDroid + OkHttp Integration through reflection
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
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