OkHttpEngine improvements #138
This commit is contained in:
parent
3f959a3b46
commit
db871d4a22
@ -30,9 +30,7 @@ import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URL;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import okhttp3.Cache;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
@ -54,40 +52,8 @@ public class OkHttpEngine implements HttpEngine {
|
||||
mClientBuilder = new OkHttpClient.Builder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the response cache to be used to read and write cached responses.
|
||||
*/
|
||||
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;
|
||||
public OkHttpFactory(OkHttpClient.Builder clientBuilder) {
|
||||
mClientBuilder = clientBuilder;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -27,6 +27,7 @@ import java.io.File;
|
||||
import java.util.UUID;
|
||||
|
||||
import okhttp3.Cache;
|
||||
import okhttp3.OkHttpClient;
|
||||
|
||||
public class MapboxTest extends GdxMapApp {
|
||||
|
||||
@ -34,14 +35,15 @@ public class MapboxTest extends GdxMapApp {
|
||||
|
||||
@Override
|
||||
public void createLayers() {
|
||||
// Cache the tiles into file system
|
||||
File cacheDirectory = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
|
||||
int cacheSize = 10 * 1024 * 1024; // 10 MB
|
||||
Cache cache = new Cache(cacheDirectory, cacheSize);
|
||||
|
||||
OkHttpEngine.OkHttpFactory factory = new OkHttpEngine.OkHttpFactory();
|
||||
if (USE_CACHE)
|
||||
factory.cache(cache);
|
||||
OkHttpClient.Builder builder = new OkHttpClient.Builder();
|
||||
if (USE_CACHE) {
|
||||
// Cache the tiles into file system
|
||||
File cacheDirectory = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
|
||||
int cacheSize = 10 * 1024 * 1024; // 10 MB
|
||||
Cache cache = new Cache(cacheDirectory, cacheSize);
|
||||
builder.cache(cache);
|
||||
}
|
||||
OkHttpEngine.OkHttpFactory factory = new OkHttpEngine.OkHttpFactory(builder);
|
||||
|
||||
UrlTileSource tileSource = MapboxTileSource.builder()
|
||||
.apiKey("mapzen-xxxxxxx") // Put a proper API key
|
||||
|
@ -12,6 +12,7 @@ import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import okhttp3.Cache;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.mockwebserver.MockResponse;
|
||||
import okhttp3.mockwebserver.MockWebServer;
|
||||
import okhttp3.mockwebserver.RecordedRequest;
|
||||
@ -94,7 +95,8 @@ public class OkHttpEngineTest {
|
||||
Cache cache = new Cache(new File("tmp"), 1024);
|
||||
OSciMap4TileSource tileSource =
|
||||
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.requestCompleted(true);
|
||||
assertThat(cache.requestCount()).isEqualTo(1);
|
||||
|
Loading…
x
Reference in New Issue
Block a user