OkHttp 3 engine, closes #138
This commit is contained in:
parent
d22dc79655
commit
817bd13d7b
@ -15,6 +15,7 @@
|
||||
- House numbers (nodes) fix visibility [#168](https://github.com/mapsforge/vtm/issues/168)
|
||||
- Android fix quick scale vs long press [#250](https://github.com/mapsforge/vtm/issues/250)
|
||||
- Use baseline 160dpi in scaling [#236](https://github.com/mapsforge/vtm/issues/236)
|
||||
- OkHttp3 update [#138](https://github.com/mapsforge/vtm/issues/138)
|
||||
- libGDX double tap zoom [#263](https://github.com/mapsforge/vtm/issues/263)
|
||||
- MapFileTileSource zoom level API enhancements [#219](https://github.com/mapsforge/vtm/issues/219)
|
||||
- Animator enhancements with easing functions [#246](https://github.com/mapsforge/vtm/issues/246)
|
||||
|
@ -5,7 +5,7 @@ dependencies {
|
||||
compile project(':vtm-themes')
|
||||
compile project(':vtm-extras')
|
||||
compile 'com.noveogroup.android:android-logger:1.3.6'
|
||||
compile 'com.squareup.okhttp:okhttp:2.6.0'
|
||||
compile 'com.squareup.okhttp3:okhttp:3.6.0'
|
||||
}
|
||||
|
||||
android {
|
||||
|
@ -2,25 +2,26 @@ package org.osmdroid.utils;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import com.squareup.okhttp.OkHttpClient;
|
||||
import com.squareup.okhttp.Request;
|
||||
import com.squareup.okhttp.Response;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.OkHttpClient.Builder;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
/**
|
||||
* A "very very simple to use" class for performing http get and post requests.
|
||||
* So many ways to do that, and potential subtle issues.
|
||||
* If complexity should be added to handle even more issues, complexity should be put here and only here.
|
||||
* <p/>
|
||||
* <p>
|
||||
* Typical usage:
|
||||
* <pre>HttpConnection connection = new HttpConnection();
|
||||
* connection.doGet("http://www.google.com");
|
||||
* InputStream stream = connection.getStream();
|
||||
* if (stream != null) {
|
||||
* //use this stream, for buffer reading, or XML SAX parsing, or whatever...
|
||||
* //use this stream, for buffer reading, or XML SAX parsing, or whatever...
|
||||
* }
|
||||
* connection.close();</pre>
|
||||
*/
|
||||
@ -35,9 +36,14 @@ public class HttpConnection {
|
||||
|
||||
private static OkHttpClient getOkHttpClient() {
|
||||
if (client == null) {
|
||||
client = new OkHttpClient();
|
||||
Builder b = new Builder();
|
||||
b.connectTimeout(TIMEOUT_CONNECTION, TimeUnit.MILLISECONDS);
|
||||
b.readTimeout(TIMEOUT_SOCKET, TimeUnit.MILLISECONDS);
|
||||
client = b.build();
|
||||
/*
|
||||
client.setConnectTimeout(TIMEOUT_CONNECTION, TimeUnit.MILLISECONDS);
|
||||
client.setReadTimeout(TIMEOUT_SOCKET, TimeUnit.MILLISECONDS);
|
||||
*/
|
||||
}
|
||||
return client;
|
||||
}
|
||||
@ -73,15 +79,10 @@ public class HttpConnection {
|
||||
* @return the opened InputStream, or null if creation failed for any reason.
|
||||
*/
|
||||
public InputStream getStream() {
|
||||
try {
|
||||
if (response == null)
|
||||
return null;
|
||||
stream = response.body().byteStream();
|
||||
return stream;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
if (response == null)
|
||||
return null;
|
||||
}
|
||||
stream = response.body().byteStream();
|
||||
return stream;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -3,7 +3,7 @@ apply plugin: 'maven'
|
||||
|
||||
dependencies {
|
||||
compile project(':vtm')
|
||||
compile 'com.squareup.okhttp:okhttp:1.5.2'
|
||||
compile 'com.squareup.okhttp3:okhttp:3.6.0'
|
||||
}
|
||||
|
||||
sourceSets {
|
||||
|
@ -1,6 +1,7 @@
|
||||
/*
|
||||
* Copyright 2014 Charles Greb
|
||||
* Copyright 2014 Hannes Janetzek
|
||||
* Copyright 2017 devemux86
|
||||
*
|
||||
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
|
||||
*
|
||||
@ -17,24 +18,21 @@
|
||||
*/
|
||||
package org.oscim.tiling.source;
|
||||
|
||||
import com.squareup.okhttp.HttpResponseCache;
|
||||
import com.squareup.okhttp.OkHttpClient;
|
||||
|
||||
import org.oscim.core.Tile;
|
||||
import org.oscim.utils.IOUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import okhttp3.Cache;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class OkHttpEngine implements HttpEngine {
|
||||
static final Logger log = LoggerFactory.getLogger(OkHttpEngine.class);
|
||||
|
||||
private final OkHttpClient mClient;
|
||||
private final UrlTileSource mTileSource;
|
||||
@ -46,9 +44,10 @@ public class OkHttpEngine implements HttpEngine {
|
||||
mClient = new OkHttpClient();
|
||||
}
|
||||
|
||||
public OkHttpFactory(HttpResponseCache responseCache) {
|
||||
mClient = new OkHttpClient();
|
||||
mClient.setResponseCache(responseCache);
|
||||
public OkHttpFactory(Cache cache) {
|
||||
mClient = new OkHttpClient.Builder()
|
||||
.cache(cache)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -75,17 +74,13 @@ public class OkHttpEngine implements HttpEngine {
|
||||
throw new IllegalArgumentException("Tile cannot be null.");
|
||||
}
|
||||
URL url = new URL(mTileSource.getTileUrl(tile));
|
||||
HttpURLConnection conn = mClient.open(url);
|
||||
|
||||
Request.Builder builder = new Request.Builder()
|
||||
.url(url);
|
||||
for (Entry<String, String> opt : mTileSource.getRequestHeader().entrySet())
|
||||
conn.addRequestProperty(opt.getKey(), opt.getValue());
|
||||
|
||||
try {
|
||||
inputStream = conn.getInputStream();
|
||||
} catch (FileNotFoundException e) {
|
||||
throw new IOException("ERROR " + conn.getResponseCode()
|
||||
+ ": " + conn.getResponseMessage());
|
||||
}
|
||||
builder.addHeader(opt.getKey(), opt.getValue());
|
||||
Request request = builder.build();
|
||||
Response response = mClient.newCall(request).execute();
|
||||
inputStream = response.body().byteStream();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -103,9 +98,11 @@ public class OkHttpEngine implements HttpEngine {
|
||||
}).start();
|
||||
}
|
||||
|
||||
/**
|
||||
* OkHttp cache implemented through {@link OkHttpClient.Builder#cache(Cache)}.
|
||||
*/
|
||||
@Override
|
||||
public void setCache(OutputStream os) {
|
||||
// OkHttp cache implented through tileSource setResponseCache
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -2,8 +2,8 @@ apply plugin: 'java'
|
||||
|
||||
dependencies {
|
||||
compile project(':vtm-http')
|
||||
compile 'com.squareup.okhttp:okhttp:1.5.2'
|
||||
testCompile 'com.squareup.okhttp:mockwebserver:1.5.2'
|
||||
compile 'com.squareup.okhttp3:okhttp:3.6.0'
|
||||
testCompile 'com.squareup.okhttp3:mockwebserver:3.6.0'
|
||||
testCompile 'junit:junit:4.12'
|
||||
testCompile 'org.easytesting:fest-assert-core:2.0M10'
|
||||
testCompile 'org.mockito:mockito-all:1.10.19'
|
||||
|
@ -1,10 +1,5 @@
|
||||
package org.oscim.tiling.source;
|
||||
|
||||
import com.squareup.okhttp.HttpResponseCache;
|
||||
import com.squareup.okhttp.mockwebserver.MockResponse;
|
||||
import com.squareup.okhttp.mockwebserver.MockWebServer;
|
||||
import com.squareup.okhttp.mockwebserver.RecordedRequest;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@ -16,23 +11,26 @@ import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import okhttp3.Cache;
|
||||
import okhttp3.mockwebserver.MockResponse;
|
||||
import okhttp3.mockwebserver.MockWebServer;
|
||||
import okhttp3.mockwebserver.RecordedRequest;
|
||||
|
||||
import static org.fest.assertions.api.Assertions.assertThat;
|
||||
|
||||
public class OkHttpEngineTest {
|
||||
private OkHttpEngine engine;
|
||||
private MockWebServer server;
|
||||
private MockResponse mockResponse;
|
||||
private HttpResponseCache cache;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
mockResponse = new MockResponse();
|
||||
mockResponse.setBody("TEST RESPONSE".getBytes());
|
||||
MockResponse mockResponse = new MockResponse();
|
||||
mockResponse.setBody("TEST RESPONSE");
|
||||
server = new MockWebServer();
|
||||
server.enqueue(mockResponse);
|
||||
server.play();
|
||||
server.start();
|
||||
engine = (OkHttpEngine) new OkHttpEngine.OkHttpFactory()
|
||||
.create(new OSciMap4TileSource(server.getUrl("/tiles/vtm").toString()));
|
||||
.create(new OSciMap4TileSource(server.url("/tiles/vtm").toString()));
|
||||
}
|
||||
|
||||
@After
|
||||
@ -52,7 +50,7 @@ public class OkHttpEngineTest {
|
||||
|
||||
@Test
|
||||
public void sendRequest_shouldAppendXYZToPath() throws Exception {
|
||||
engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue()));
|
||||
engine.sendRequest(new Tile(1, 2, (byte) 3));
|
||||
|
||||
RecordedRequest request = server.takeRequest();
|
||||
assertThat(request.getPath()).isEqualTo("/tiles/vtm/3/1/2.vtm");
|
||||
@ -60,7 +58,7 @@ public class OkHttpEngineTest {
|
||||
|
||||
@Test
|
||||
public void read_shouldReturnResponseStream() throws Exception {
|
||||
engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue()));
|
||||
engine.sendRequest(new Tile(1, 2, (byte) 3));
|
||||
|
||||
InputStream responseStream = engine.read();
|
||||
String response = new BufferedReader(new InputStreamReader(responseStream)).readLine();
|
||||
@ -93,12 +91,12 @@ public class OkHttpEngineTest {
|
||||
|
||||
@Test
|
||||
public void create_shouldUseTileSourceCache() throws Exception {
|
||||
cache = new HttpResponseCache(new File("tmp"), 1024);
|
||||
Cache cache = new Cache(new File("tmp"), 1024);
|
||||
OSciMap4TileSource tileSource =
|
||||
new OSciMap4TileSource(server.getUrl("/tiles/vtm").toString());
|
||||
new OSciMap4TileSource(server.url("/tiles/vtm").toString());
|
||||
engine = (OkHttpEngine) new OkHttpEngine.OkHttpFactory(cache).create(tileSource);
|
||||
engine.sendRequest(new Tile(1, 2, new Integer(3).byteValue()));
|
||||
engine.sendRequest(new Tile(1, 2, (byte) 3));
|
||||
engine.requestCompleted(true);
|
||||
assertThat(cache.getRequestCount()).isEqualTo(1);
|
||||
assertThat(cache.requestCount()).isEqualTo(1);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user