Using okhttpclient for suggestions downloads
This commit is contained in:
parent
b46129f83a
commit
79febae033
@ -54,7 +54,6 @@ class FetchUrlMimeType extends Thread {
|
|||||||
public void run() {
|
public void run() {
|
||||||
// User agent is likely to be null, though the AndroidHttpClient
|
// User agent is likely to be null, though the AndroidHttpClient
|
||||||
// seems ok with that.
|
// seems ok with that.
|
||||||
final Bus eventBus = BrowserApp.getBus(mContext);
|
|
||||||
String mimeType = null;
|
String mimeType = null;
|
||||||
String contentDisposition = null;
|
String contentDisposition = null;
|
||||||
HttpURLConnection connection = null;
|
HttpURLConnection connection = null;
|
||||||
|
@ -23,10 +23,12 @@ import java.util.Locale;
|
|||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.zip.GZIPInputStream;
|
import java.util.zip.GZIPInputStream;
|
||||||
|
|
||||||
import javax.net.ssl.HttpsURLConnection;
|
|
||||||
|
|
||||||
import acr.browser.lightning.database.HistoryItem;
|
import acr.browser.lightning.database.HistoryItem;
|
||||||
import acr.browser.lightning.utils.Utils;
|
import acr.browser.lightning.utils.Utils;
|
||||||
|
import okhttp3.CacheControl;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.Response;
|
||||||
|
|
||||||
abstract class BaseSuggestionsTask {
|
abstract class BaseSuggestionsTask {
|
||||||
|
|
||||||
@ -38,6 +40,8 @@ abstract class BaseSuggestionsTask {
|
|||||||
@Nullable private static String sLanguage;
|
@Nullable private static String sLanguage;
|
||||||
@NonNull private final SuggestionsResult mResultCallback;
|
@NonNull private final SuggestionsResult mResultCallback;
|
||||||
@NonNull private final Application mApplication;
|
@NonNull private final Application mApplication;
|
||||||
|
@NonNull private final OkHttpClient mHttpClient = new OkHttpClient();
|
||||||
|
@NonNull private final CacheControl mCacheControl;
|
||||||
@NonNull private String mQuery;
|
@NonNull private String mQuery;
|
||||||
|
|
||||||
protected abstract String getQueryUrl(@NonNull String query, @NonNull String language);
|
protected abstract String getQueryUrl(@NonNull String query, @NonNull String language);
|
||||||
@ -52,6 +56,7 @@ abstract class BaseSuggestionsTask {
|
|||||||
mQuery = query;
|
mQuery = query;
|
||||||
mResultCallback = callback;
|
mResultCallback = callback;
|
||||||
mApplication = application;
|
mApplication = application;
|
||||||
|
mCacheControl = new CacheControl.Builder().maxStale(1, TimeUnit.DAYS).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
@ -116,19 +121,22 @@ abstract class BaseSuggestionsTask {
|
|||||||
FileOutputStream fos = null;
|
FileOutputStream fos = null;
|
||||||
try {
|
try {
|
||||||
URL url = new URL(queryUrl);
|
URL url = new URL(queryUrl);
|
||||||
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
|
Request suggestionsRequest = new Request.Builder().url(url)
|
||||||
connection.setDoInput(true);
|
.addHeader("Accept-Encoding", "gzip")
|
||||||
connection.setRequestProperty("Accept-Encoding", "gzip");
|
.addHeader("Accept-Charset", getEncoding())
|
||||||
connection.setRequestProperty("Accept-Charset", getEncoding());
|
.cacheControl(mCacheControl)
|
||||||
connection.connect();
|
.build();
|
||||||
if (connection.getResponseCode() >= HttpURLConnection.HTTP_MULT_CHOICE ||
|
|
||||||
connection.getResponseCode() < HttpURLConnection.HTTP_OK) {
|
Response suggestionsResponse = mHttpClient.newCall(suggestionsRequest).execute();
|
||||||
Log.e(TAG, "Search API Responded with code: " + connection.getResponseCode());
|
|
||||||
connection.disconnect();
|
if (suggestionsResponse.code() >= HttpURLConnection.HTTP_MULT_CHOICE ||
|
||||||
|
suggestionsResponse.code() < HttpURLConnection.HTTP_OK) {
|
||||||
|
Log.e(TAG, "Search API Responded with code: " + suggestionsResponse.code());
|
||||||
|
suggestionsResponse.body().close();
|
||||||
return cacheFile;
|
return cacheFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
in = connection.getInputStream();
|
in = suggestionsResponse.body().byteStream();
|
||||||
|
|
||||||
if (in != null) {
|
if (in != null) {
|
||||||
in = new GZIPInputStream(in);
|
in = new GZIPInputStream(in);
|
||||||
@ -140,7 +148,7 @@ abstract class BaseSuggestionsTask {
|
|||||||
}
|
}
|
||||||
fos.flush();
|
fos.flush();
|
||||||
}
|
}
|
||||||
connection.disconnect();
|
suggestionsResponse.body().close();
|
||||||
cacheFile.setLastModified(System.currentTimeMillis());
|
cacheFile.setLastModified(System.currentTimeMillis());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.w(TAG, "Problem getting search suggestions", e);
|
Log.w(TAG, "Problem getting search suggestions", e);
|
||||||
@ -159,8 +167,8 @@ abstract class BaseSuggestionsTask {
|
|||||||
@Nullable
|
@Nullable
|
||||||
private static NetworkInfo getActiveNetworkInfo(@NonNull Context context) {
|
private static NetworkInfo getActiveNetworkInfo(@NonNull Context context) {
|
||||||
ConnectivityManager connectivity = (ConnectivityManager) context
|
ConnectivityManager connectivity = (ConnectivityManager) context
|
||||||
.getApplicationContext()
|
.getApplicationContext()
|
||||||
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
if (connectivity == null) {
|
if (connectivity == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user