This repository was archived by the owner on Feb 14, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 56
Convert to HttpURLConnection/OkHttp #12
Merged
grantland
merged 1 commit into
parse-community:master
from
zefei:convert_to_httpurlconnection_okhttp
Nov 5, 2015
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
42 changes: 42 additions & 0 deletions
42
library/src/main/java/com/parse/internal/signpost/basic/HttpURLConnectionClient.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.parse.internal.signpost.basic; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Method; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
|
||
/** | ||
* Adapter class to provide HttpURLConnection, using android HttpURLConnection or OkHttp | ||
*/ | ||
public final class HttpURLConnectionClient { | ||
private final boolean isUsingOkHttp; | ||
private final Object okUrlFactory; | ||
private final Method okUrlFactoryOpen; | ||
|
||
private HttpURLConnectionClient(boolean isUsingOkHttp, Object okUrlFactory, Method okUrlFactoryOpen) { | ||
this.isUsingOkHttp = isUsingOkHttp; | ||
this.okUrlFactory = okUrlFactory; | ||
this.okUrlFactoryOpen = okUrlFactoryOpen; | ||
} | ||
|
||
public static HttpURLConnectionClient create() { | ||
try { | ||
final Class okHttpClientClass = Class.forName("com.squareup.okhttp.OkHttpClient"); | ||
final Object okHttpClient = okHttpClientClass.getConstructor().newInstance(); | ||
final Class okUrlFactoryClass = Class.forName("com.squareup.okhttp.OkUrlFactory"); | ||
final Object okUrlFactory = okUrlFactoryClass.getConstructor(okHttpClientClass).newInstance(okHttpClient); | ||
final Method okUrlFactoryOpen = okUrlFactoryClass.getMethod("open", URL.class); | ||
return new HttpURLConnectionClient(true, okUrlFactory, okUrlFactoryOpen); | ||
} catch (Exception e) { | ||
return new HttpURLConnectionClient(true, null, null); | ||
} | ||
} | ||
|
||
public HttpURLConnection open(URL url) throws Exception { | ||
if (isUsingOkHttp) { | ||
return (HttpURLConnection) okUrlFactoryOpen.invoke(okUrlFactory, url); | ||
} else { | ||
return (HttpURLConnection) url.openConnection(); | ||
} | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're not the biggest fans of static globals, could you move the reference to a instance on
Twitter
that get's passed intoDefaultOAuthProvider
? I'm ok with removing the global reference, but using a static factorynewInstance()
instead ofgetInstance()
.