OkHttpEngine improvements #138
This commit is contained in:
@@ -30,9 +30,7 @@ import java.io.InputStream;
|
|||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
import okhttp3.Cache;
|
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
@@ -54,40 +52,8 @@ public class OkHttpEngine implements HttpEngine {
|
|||||||
mClientBuilder = new OkHttpClient.Builder();
|
mClientBuilder = new OkHttpClient.Builder();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public OkHttpFactory(OkHttpClient.Builder clientBuilder) {
|
||||||
* Sets the response cache to be used to read and write cached responses.
|
mClientBuilder = clientBuilder;
|
||||||
*/
|
|
||||||
public OkHttpFactory cache(Cache cache) {
|
|
||||||
mClientBuilder.cache(cache);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the default connect timeout for new connections. A value of 0 means no timeout,
|
|
||||||
* otherwise values must be between 1 and {@link Integer#MAX_VALUE} when converted to
|
|
||||||
* milliseconds.
|
|
||||||
*/
|
|
||||||
public OkHttpFactory connectTimeout(long timeout, TimeUnit unit) {
|
|
||||||
mClientBuilder.connectTimeout(timeout, unit);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the default read timeout for new connections. A value of 0 means no timeout, otherwise
|
|
||||||
* values must be between 1 and {@link Integer#MAX_VALUE} when converted to milliseconds.
|
|
||||||
*/
|
|
||||||
public OkHttpFactory readTimeout(long timeout, TimeUnit unit) {
|
|
||||||
mClientBuilder.readTimeout(timeout, unit);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the default write timeout for new connections. A value of 0 means no timeout, otherwise
|
|
||||||
* values must be between 1 and {@link Integer#MAX_VALUE} when converted to milliseconds.
|
|
||||||
*/
|
|
||||||
public OkHttpFactory writeTimeout(long timeout, TimeUnit unit) {
|
|
||||||
mClientBuilder.writeTimeout(timeout, unit);
|
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ import java.io.File;
|
|||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import okhttp3.Cache;
|
import okhttp3.Cache;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
|
||||||
public class MapboxTest extends GdxMapApp {
|
public class MapboxTest extends GdxMapApp {
|
||||||
|
|
||||||
@@ -34,14 +35,15 @@ public class MapboxTest extends GdxMapApp {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void createLayers() {
|
public void createLayers() {
|
||||||
// Cache the tiles into file system
|
OkHttpClient.Builder builder = new OkHttpClient.Builder();
|
||||||
File cacheDirectory = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
|
if (USE_CACHE) {
|
||||||
int cacheSize = 10 * 1024 * 1024; // 10 MB
|
// Cache the tiles into file system
|
||||||
Cache cache = new Cache(cacheDirectory, cacheSize);
|
File cacheDirectory = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
|
||||||
|
int cacheSize = 10 * 1024 * 1024; // 10 MB
|
||||||
OkHttpEngine.OkHttpFactory factory = new OkHttpEngine.OkHttpFactory();
|
Cache cache = new Cache(cacheDirectory, cacheSize);
|
||||||
if (USE_CACHE)
|
builder.cache(cache);
|
||||||
factory.cache(cache);
|
}
|
||||||
|
OkHttpEngine.OkHttpFactory factory = new OkHttpEngine.OkHttpFactory(builder);
|
||||||
|
|
||||||
UrlTileSource tileSource = MapboxTileSource.builder()
|
UrlTileSource tileSource = MapboxTileSource.builder()
|
||||||
.apiKey("mapzen-xxxxxxx") // Put a proper API key
|
.apiKey("mapzen-xxxxxxx") // Put a proper API key
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import java.io.InputStream;
|
|||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
|
||||||
import okhttp3.Cache;
|
import okhttp3.Cache;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
import okhttp3.mockwebserver.MockResponse;
|
import okhttp3.mockwebserver.MockResponse;
|
||||||
import okhttp3.mockwebserver.MockWebServer;
|
import okhttp3.mockwebserver.MockWebServer;
|
||||||
import okhttp3.mockwebserver.RecordedRequest;
|
import okhttp3.mockwebserver.RecordedRequest;
|
||||||
@@ -94,7 +95,8 @@ public class OkHttpEngineTest {
|
|||||||
Cache cache = new Cache(new File("tmp"), 1024);
|
Cache cache = new Cache(new File("tmp"), 1024);
|
||||||
OSciMap4TileSource tileSource =
|
OSciMap4TileSource tileSource =
|
||||||
new OSciMap4TileSource(server.url("/tiles/vtm").toString());
|
new OSciMap4TileSource(server.url("/tiles/vtm").toString());
|
||||||
engine = (OkHttpEngine) new OkHttpEngine.OkHttpFactory().cache(cache).create(tileSource);
|
OkHttpClient.Builder builder = new OkHttpClient.Builder().cache(cache);
|
||||||
|
engine = (OkHttpEngine) new OkHttpEngine.OkHttpFactory(builder).create(tileSource);
|
||||||
engine.sendRequest(new Tile(1, 2, (byte) 3));
|
engine.sendRequest(new Tile(1, 2, (byte) 3));
|
||||||
engine.requestCompleted(true);
|
engine.requestCompleted(true);
|
||||||
assertThat(cache.requestCount()).isEqualTo(1);
|
assertThat(cache.requestCount()).isEqualTo(1);
|
||||||
|
|||||||
Reference in New Issue
Block a user