mirror of
https://github.com/viq/NewsBlur.git
synced 2025-09-18 21:43:31 +00:00
Upgrade HTTP libs.
This commit is contained in:
parent
45af1dc361
commit
1b517e41e3
9 changed files with 45 additions and 29 deletions
Binary file not shown.
BIN
clients/android/NewsBlur/libs/okhttp-3.1.2.jar
Normal file
BIN
clients/android/NewsBlur/libs/okhttp-3.1.2.jar
Normal file
Binary file not shown.
Binary file not shown.
BIN
clients/android/NewsBlur/libs/okio-1.6.0.jar
Normal file
BIN
clients/android/NewsBlur/libs/okio-1.6.0.jar
Normal file
Binary file not shown.
|
@ -9,8 +9,8 @@ import java.util.Map;
|
|||
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.squareup.okhttp.FormEncodingBuilder;
|
||||
import com.squareup.okhttp.RequestBody;
|
||||
import okhttp3.FormBody;
|
||||
import okhttp3.RequestBody;
|
||||
|
||||
/**
|
||||
* A String-to-String multimap that serializes to JSON or HTTP request params.
|
||||
|
@ -19,7 +19,6 @@ import com.squareup.okhttp.RequestBody;
|
|||
public class ValueMultimap implements Serializable {
|
||||
|
||||
private Map<String, List<String>> multimap;
|
||||
private String TAG = "ValueMultimap";
|
||||
|
||||
public ValueMultimap() {
|
||||
multimap = new HashMap<String, List<String>>();
|
||||
|
@ -49,7 +48,7 @@ public class ValueMultimap implements Serializable {
|
|||
}
|
||||
|
||||
public RequestBody asFormEncodedRequestBody() {
|
||||
FormEncodingBuilder formEncodingBuilder = new FormEncodingBuilder();
|
||||
FormBody.Builder formEncodingBuilder = new FormBody.Builder();
|
||||
for (String key : multimap.keySet()) {
|
||||
for (String value : multimap.get(key)) {
|
||||
formEncodingBuilder.add(key, value);
|
||||
|
|
|
@ -9,6 +9,7 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
|
@ -52,11 +53,11 @@ import com.newsblur.util.PrefsUtils;
|
|||
import com.newsblur.util.ReadFilter;
|
||||
import com.newsblur.util.StoryOrder;
|
||||
|
||||
import com.squareup.okhttp.FormEncodingBuilder;
|
||||
import com.squareup.okhttp.OkHttpClient;
|
||||
import com.squareup.okhttp.Request;
|
||||
import com.squareup.okhttp.RequestBody;
|
||||
import com.squareup.okhttp.Response;
|
||||
import okhttp3.FormBody;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class APIManager {
|
||||
|
||||
|
@ -84,7 +85,11 @@ public class APIManager {
|
|||
Build.VERSION.RELEASE + " " +
|
||||
appVersion + ")";
|
||||
|
||||
this.httpClient = new OkHttpClient();
|
||||
this.httpClient = new OkHttpClient.Builder()
|
||||
.connectTimeout(AppConstants.API_CONN_TIMEOUT_SECONDS, TimeUnit.SECONDS)
|
||||
.readTimeout(AppConstants.API_READ_TIMEOUT_SECONDS, TimeUnit.SECONDS)
|
||||
.followSslRedirects(true)
|
||||
.build();
|
||||
}
|
||||
|
||||
public LoginResponse login(final String username, final String password) {
|
||||
|
@ -111,8 +116,9 @@ public class APIManager {
|
|||
// just get the cookie from the 302 and stop, we directly use a one-off OkHttpClient.
|
||||
Request.Builder requestBuilder = new Request.Builder().url(urlString);
|
||||
addCookieHeader(requestBuilder);
|
||||
OkHttpClient noredirHttpClient = new OkHttpClient();
|
||||
noredirHttpClient.setFollowRedirects(false);
|
||||
OkHttpClient noredirHttpClient = new OkHttpClient.Builder()
|
||||
.followRedirects(false)
|
||||
.build();
|
||||
try {
|
||||
Response response = noredirHttpClient.newCall(requestBuilder.build()).execute();
|
||||
if (!response.isRedirect()) return false;
|
||||
|
@ -661,7 +667,7 @@ public class APIManager {
|
|||
}
|
||||
|
||||
private APIResponse post(final String urlString, final ContentValues values) {
|
||||
FormEncodingBuilder formEncodingBuilder = new FormEncodingBuilder();
|
||||
FormBody.Builder formEncodingBuilder = new FormBody.Builder();
|
||||
for (Entry<String, Object> entry : values.valueSet()) {
|
||||
formEncodingBuilder.add(entry.getKey(), (String)entry.getValue());
|
||||
}
|
||||
|
|
|
@ -13,9 +13,10 @@ import com.newsblur.network.domain.LoginResponse;
|
|||
import com.newsblur.network.domain.NewsBlurResponse;
|
||||
import com.newsblur.network.domain.RegisterResponse;
|
||||
import com.newsblur.util.AppConstants;
|
||||
import com.squareup.okhttp.OkHttpClient;
|
||||
import com.squareup.okhttp.Request;
|
||||
import com.squareup.okhttp.Response;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
/**
|
||||
* A JSON-encoded response from the API servers. This class encodes the possible outcomes of
|
||||
|
@ -53,7 +54,7 @@ public class APIResponse {
|
|||
this.responseCode = response.code();
|
||||
|
||||
if (responseCode != expectedReturnCode) {
|
||||
Log.e(this.getClass().getName(), "API returned error code " + response.code() + " calling " + request.urlString() + " - expected " + expectedReturnCode);
|
||||
Log.e(this.getClass().getName(), "API returned error code " + response.code() + " calling " + request.url().toString() + " - expected " + expectedReturnCode);
|
||||
this.isError = true;
|
||||
return;
|
||||
}
|
||||
|
@ -65,7 +66,7 @@ public class APIResponse {
|
|||
this.responseBody = response.body().string();
|
||||
readTime = System.currentTimeMillis() - startTime;
|
||||
} catch (Exception e) {
|
||||
Log.e(this.getClass().getName(), e.getClass().getName() + " (" + e.getMessage() + ") reading " + request.urlString(), e);
|
||||
Log.e(this.getClass().getName(), e.getClass().getName() + " (" + e.getMessage() + ") reading " + request.url().toString(), e);
|
||||
this.isError = true;
|
||||
return;
|
||||
}
|
||||
|
@ -83,11 +84,11 @@ public class APIResponse {
|
|||
}
|
||||
|
||||
if (AppConstants.VERBOSE_LOG_NET) {
|
||||
Log.d(this.getClass().getName(), String.format("called %s in %dms and %dms to read %dB", request.urlString(), connectTime, readTime, responseBody.length()));
|
||||
Log.d(this.getClass().getName(), String.format("called %s in %dms and %dms to read %dB", request.url().toString(), connectTime, readTime, responseBody.length()));
|
||||
}
|
||||
|
||||
} catch (IOException ioe) {
|
||||
Log.e(this.getClass().getName(), "Error (" + ioe.getMessage() + ") calling " + request.urlString(), ioe);
|
||||
Log.e(this.getClass().getName(), "Error (" + ioe.getMessage() + ") calling " + request.url().toString(), ioe);
|
||||
this.isError = true;
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -45,6 +45,14 @@ public class AppConstants {
|
|||
// for how long to back off from background syncs after a hard API failure
|
||||
public static final long API_BACKGROUND_BACKOFF_MILLIS = 5L * 60L * 1000L;
|
||||
|
||||
// timeouts for API calls, set to something more sane than the default of infinity
|
||||
public static final long API_CONN_TIMEOUT_SECONDS = 60L;
|
||||
public static final long API_READ_TIMEOUT_SECONDS = 120L;
|
||||
|
||||
// timeouts for image prefetching, which are a bit tighter, since they are only for caching
|
||||
public static final long IMAGE_PREFETCH_CONN_TIMEOUT_SECONDS = 10L;
|
||||
public static final long IMAGE_PREFETCH_READ_TIMEOUT_SECONDS = 30L;
|
||||
|
||||
// when generating a request for multiple feeds, limit the total number requested to prevent
|
||||
// unworkably long URLs
|
||||
public static final int MAX_FEED_LIST_SIZE = 250;
|
||||
|
|
|
@ -4,26 +4,28 @@ import android.content.Context;
|
|||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
|
||||
import com.squareup.okhttp.OkHttpClient;
|
||||
import com.squareup.okhttp.Request;
|
||||
import com.squareup.okhttp.Response;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
import okio.BufferedSink;
|
||||
import okio.Okio;
|
||||
|
||||
public class NetworkUtils {
|
||||
|
||||
private static OkHttpClient httpClient = new OkHttpClient();
|
||||
private static OkHttpClient ImageFetchHttpClient;
|
||||
|
||||
static {
|
||||
// By default OkHttpClient follows redirects (inc HTTP -> HTTPS and HTTPS -> HTTP).
|
||||
httpClient.setConnectTimeout(10, TimeUnit.SECONDS);
|
||||
httpClient.setReadTimeout(30, TimeUnit.SECONDS);
|
||||
ImageFetchHttpClient = new OkHttpClient.Builder()
|
||||
.connectTimeout(AppConstants.IMAGE_PREFETCH_CONN_TIMEOUT_SECONDS, TimeUnit.SECONDS)
|
||||
.readTimeout(AppConstants.IMAGE_PREFETCH_READ_TIMEOUT_SECONDS, TimeUnit.SECONDS)
|
||||
.followSslRedirects(true)
|
||||
.build();
|
||||
}
|
||||
|
||||
public static boolean isOnline(Context context) {
|
||||
|
@ -36,7 +38,7 @@ public class NetworkUtils {
|
|||
long bytesRead = 0;
|
||||
try {
|
||||
Request.Builder requestBuilder = new Request.Builder().url(url);
|
||||
Response response = httpClient.newCall(requestBuilder.build()).execute();
|
||||
Response response = ImageFetchHttpClient.newCall(requestBuilder.build()).execute();
|
||||
if (response.isSuccessful()) {
|
||||
BufferedSink sink = Okio.buffer(Okio.sink(file));
|
||||
bytesRead = sink.writeAll(response.body().source());
|
||||
|
|
Loading…
Add table
Reference in a new issue