diff options
author | Narayan Kamath <narayan@google.com> | 2014-12-19 13:00:20 +0000 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2014-12-19 13:00:20 +0000 |
commit | 87ec918aa6bbab666ff1f02e2e25d2f19d7617cd (patch) | |
tree | 86afc7e8bed6dce700ec086ab25eeecd33d455de /src | |
parent | 3a815d477f5a23af1e96a6b24e546e1506ab7bb2 (diff) | |
parent | 715deeacb019601d3570acb5a81db5c14c28d2f0 (diff) | |
download | packages_apps_Browser-87ec918aa6bbab666ff1f02e2e25d2f19d7617cd.zip packages_apps_Browser-87ec918aa6bbab666ff1f02e2e25d2f19d7617cd.tar.gz packages_apps_Browser-87ec918aa6bbab666ff1f02e2e25d2f19d7617cd.tar.bz2 |
am 715deeac: am de5a4d4a: Merge "Move browser over to java.net.URLConnection."
* commit '715deeacb019601d3570acb5a81db5c14c28d2f0':
Move browser over to java.net.URLConnection.
Diffstat (limited to 'src')
-rw-r--r-- | src/com/android/browser/GoogleAccountLogin.java | 60 | ||||
-rw-r--r-- | src/com/android/browser/search/OpenSearchSearchEngine.java | 49 |
2 files changed, 62 insertions, 47 deletions
diff --git a/src/com/android/browser/GoogleAccountLogin.java b/src/com/android/browser/GoogleAccountLogin.java index 2bd3c8c..93b10a6 100644 --- a/src/com/android/browser/GoogleAccountLogin.java +++ b/src/com/android/browser/GoogleAccountLogin.java @@ -27,18 +27,22 @@ import android.content.DialogInterface; import android.content.DialogInterface.OnCancelListener; import android.content.SharedPreferences.Editor; import android.net.Uri; -import android.net.http.AndroidHttpClient; import android.os.Bundle; import android.util.Log; import android.webkit.CookieSyncManager; import android.webkit.WebView; import android.webkit.WebViewClient; -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.HttpStatus; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.util.EntityUtils; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.Proxy; +import java.net.URL; +import java.nio.charset.Charset; +import java.nio.charset.IllegalCharsetNameException; +import java.nio.charset.UnsupportedCharsetException; +import libcore.io.Streams; +import libcore.net.http.ResponseUtils; public class GoogleAccountLogin implements Runnable, AccountManagerCallback<Bundle>, OnCancelListener { @@ -104,25 +108,27 @@ public class GoogleAccountLogin implements Runnable, // Runnable @Override public void run() { - String url = ISSUE_AUTH_TOKEN_URL.buildUpon() + String urlString = ISSUE_AUTH_TOKEN_URL.buildUpon() .appendQueryParameter("SID", mSid) .appendQueryParameter("LSID", mLsid) .build().toString(); - // Intentionally not using Proxy. - AndroidHttpClient client = AndroidHttpClient.newInstance(mUserAgent); - HttpPost request = new HttpPost(url); - String result = null; + HttpURLConnection connection = null; + String authToken = null; try { - HttpResponse response = client.execute(request); - int status = response.getStatusLine().getStatusCode(); - if (status != HttpStatus.SC_OK) { + URL url = new URL(urlString); + // Intentionally not using Proxy. + connection = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY); + connection.setRequestMethod("POST"); + connection.setRequestProperty("User-Agent", mUserAgent); + + int status = connection.getResponseCode(); + if (status != HttpURLConnection.HTTP_OK) { Log.d(LOGTAG, "LOGIN_FAIL: Bad status from auth url " - + status + ": " - + response.getStatusLine().getReasonPhrase()); + + status + ": " + connection.getResponseMessage()); // Invalidate the tokens once just in case the 403 was for other // reasons. - if (status == HttpStatus.SC_FORBIDDEN && !mTokensInvalidated) { + if (status == HttpURLConnection.HTTP_FORBIDDEN && !mTokensInvalidated) { Log.d(LOGTAG, "LOGIN_FAIL: Invalidating tokens..."); // Need to regenerate the auth tokens and try again. invalidateTokens(); @@ -134,24 +140,24 @@ public class GoogleAccountLogin implements Runnable, done(); return; } - HttpEntity entity = response.getEntity(); - if (entity == null) { - Log.d(LOGTAG, "LOGIN_FAIL: Null entity in response"); - done(); - return; - } - result = EntityUtils.toString(entity, "UTF-8"); + + final Charset responseCharset = ResponseUtils.responseCharset( + connection.getContentType()); + byte[] responseBytes = Streams.readFully(connection.getInputStream()); + authToken = new String(responseBytes, responseCharset); } catch (Exception e) { Log.d(LOGTAG, "LOGIN_FAIL: Exception acquiring uber token " + e); - request.abort(); done(); return; } finally { - client.close(); + if (connection != null) { + connection.disconnect(); + } } + final String newUrl = TOKEN_AUTH_URL.buildUpon() .appendQueryParameter("source", "android-browser") - .appendQueryParameter("auth", result) + .appendQueryParameter("auth", authToken) .appendQueryParameter("continue", BrowserSettings.getFactoryResetHomeUrl(mActivity)) .build().toString(); diff --git a/src/com/android/browser/search/OpenSearchSearchEngine.java b/src/com/android/browser/search/OpenSearchSearchEngine.java index e600aa9..0343d6e 100644 --- a/src/com/android/browser/search/OpenSearchSearchEngine.java +++ b/src/com/android/browser/search/OpenSearchSearchEngine.java @@ -17,10 +17,14 @@ package com.android.browser.search; import com.android.browser.R; -import org.apache.http.HttpResponse; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.params.HttpParams; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.Charset; +import java.nio.charset.IllegalCharsetNameException; +import java.nio.charset.UnsupportedCharsetException; +import libcore.io.Streams; +import libcore.net.http.ResponseUtils; import org.apache.http.util.EntityUtils; import org.json.JSONArray; import org.json.JSONException; @@ -33,7 +37,6 @@ import android.database.Cursor; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; -import android.net.http.AndroidHttpClient; import android.os.Bundle; import android.provider.Browser; import android.text.TextUtils; @@ -51,9 +54,6 @@ public class OpenSearchSearchEngine implements SearchEngine { private static final String USER_AGENT = "Android/1.0"; private static final int HTTP_TIMEOUT_MS = 1000; - // TODO: this should be defined somewhere - private static final String HTTP_TIMEOUT = "http.connection-manager.timeout"; - // Indices of the columns in the below arrays. private static final int COLUMN_INDEX_ID = 0; private static final int COLUMN_INDEX_QUERY = 1; @@ -80,13 +80,8 @@ public class OpenSearchSearchEngine implements SearchEngine { private final SearchEngineInfo mSearchEngineInfo; - private final AndroidHttpClient mHttpClient; - public OpenSearchSearchEngine(Context context, SearchEngineInfo searchEngineInfo) { mSearchEngineInfo = searchEngineInfo; - mHttpClient = AndroidHttpClient.newInstance(USER_AGENT); - HttpParams params = mHttpClient.getParams(); - params.setLongParameter(HTTP_TIMEOUT, HTTP_TIMEOUT_MS); } public String getName() { @@ -167,16 +162,31 @@ public class OpenSearchSearchEngine implements SearchEngine { /** * Executes a GET request and returns the response content. * - * @param url Request URI. + * @param urlString Request URI. * @return The response content. This is the empty string if the response * contained no content. */ - public String readUrl(String url) { + public String readUrl(String urlString) { try { - HttpGet method = new HttpGet(url); - HttpResponse response = mHttpClient.execute(method); - if (response.getStatusLine().getStatusCode() == 200) { - return EntityUtils.toString(response.getEntity()); + URL url = new URL(urlString); + HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); + urlConnection.setRequestProperty("User-Agent", USER_AGENT); + urlConnection.setConnectTimeout(HTTP_TIMEOUT_MS); + + if (urlConnection.getResponseCode() == 200) { + final Charset responseCharset; + try { + responseCharset = ResponseUtils.responseCharset(urlConnection.getContentType()); + } catch (UnsupportedCharsetException ucse) { + Log.i(TAG, "Unsupported response charset", ucse); + return null; + } catch (IllegalCharsetNameException icne) { + Log.i(TAG, "Illegal response charset", icne); + return null; + } + + byte[] responseBytes = Streams.readFully(urlConnection.getInputStream()); + return new String(responseBytes, responseCharset); } else { Log.i(TAG, "Suggestion request failed"); return null; @@ -192,7 +202,6 @@ public class OpenSearchSearchEngine implements SearchEngine { } public void close() { - mHttpClient.close(); } private boolean isNetworkConnected(Context context) { |