diff options
author | Narayan Kamath <narayan@google.com> | 2014-12-16 17:03:02 +0000 |
---|---|---|
committer | Narayan Kamath <narayan@google.com> | 2014-12-16 17:08:25 +0000 |
commit | 3a2cf8007db4f3258b94fdcb74c147220350aa36 (patch) | |
tree | c8514f30328048f995b160ab82ed3a333343c2cc /src/com/android/browser/search | |
parent | 3bf59298765b441ba3cd26f0731253962c190387 (diff) | |
download | packages_apps_Browser-3a2cf8007db4f3258b94fdcb74c147220350aa36.zip packages_apps_Browser-3a2cf8007db4f3258b94fdcb74c147220350aa36.tar.gz packages_apps_Browser-3a2cf8007db4f3258b94fdcb74c147220350aa36.tar.bz2 |
Move browser over to java.net.URLConnection.
bug: 18027885
Change-Id: Ie1db66432f0bd2b2eaaf2a7998828f69145645d9
Diffstat (limited to 'src/com/android/browser/search')
-rw-r--r-- | src/com/android/browser/search/OpenSearchSearchEngine.java | 49 |
1 files changed, 29 insertions, 20 deletions
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) { |