create HttpEngine instances for each loader thread
This commit is contained in:
parent
f3035d827b
commit
f75702a575
@ -1,19 +1,24 @@
|
||||
package org.oscim.tiling.source;
|
||||
|
||||
import org.oscim.core.Tile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import org.oscim.core.Tile;
|
||||
|
||||
public interface HttpEngine {
|
||||
InputStream read() throws IOException;
|
||||
|
||||
boolean sendRequest(UrlTileSource tileSource, Tile tile) throws IOException;
|
||||
InputStream read() throws IOException;
|
||||
|
||||
void close();
|
||||
boolean sendRequest(UrlTileSource tileSource, Tile tile) throws IOException;
|
||||
|
||||
void setCache(OutputStream os);
|
||||
void close();
|
||||
|
||||
boolean requestCompleted(boolean success);
|
||||
void setCache(OutputStream os);
|
||||
|
||||
boolean requestCompleted(boolean success);
|
||||
|
||||
public interface Factory {
|
||||
public HttpEngine create();
|
||||
}
|
||||
}
|
||||
|
@ -1,23 +1,35 @@
|
||||
package org.oscim.tiling.source;
|
||||
|
||||
import com.squareup.okhttp.OkHttpClient;
|
||||
|
||||
import org.oscim.core.Tile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
import org.oscim.core.Tile;
|
||||
|
||||
import com.squareup.okhttp.OkHttpClient;
|
||||
|
||||
public class OkHttpEngine implements HttpEngine {
|
||||
private final URL baseUrl;
|
||||
private final OkHttpClient client;
|
||||
|
||||
public static class OkHttpFactory implements HttpEngine.Factory {
|
||||
private final OkHttpClient client;
|
||||
|
||||
public OkHttpFactory() {
|
||||
this.client = new OkHttpClient();
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpEngine create() {
|
||||
return new OkHttpEngine(client);
|
||||
}
|
||||
}
|
||||
|
||||
private InputStream inputStream;
|
||||
|
||||
public OkHttpEngine(URL baseUrl) {
|
||||
this.baseUrl = baseUrl;
|
||||
this.client = new OkHttpClient();
|
||||
public OkHttpEngine(OkHttpClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -31,14 +43,14 @@ public class OkHttpEngine implements HttpEngine {
|
||||
throw new IllegalArgumentException("Tile cannot be null.");
|
||||
}
|
||||
|
||||
final URL requestUrl = new URL(baseUrl.toString()
|
||||
+ "/"
|
||||
+ Byte.toString(tile.zoomLevel)
|
||||
+ "/"
|
||||
+ tile.tileX
|
||||
+ "/"
|
||||
+ tile.tileY
|
||||
+ ".vtm");
|
||||
final URL requestUrl = new URL(tileSource.getUrl()
|
||||
+ "/"
|
||||
+ Byte.toString(tile.zoomLevel)
|
||||
+ "/"
|
||||
+ tile.tileX
|
||||
+ "/"
|
||||
+ tile.tileY
|
||||
+ ".vtm");
|
||||
|
||||
final HttpURLConnection connection = client.open(requestUrl);
|
||||
|
||||
|
@ -26,7 +26,7 @@ public abstract class UrlTileSource extends TileSource {
|
||||
|
||||
private final URL mUrl;
|
||||
private byte[] mExt;
|
||||
private HttpEngine httpEngine;
|
||||
private HttpEngine.Factory mHttpFactory;
|
||||
|
||||
public UrlTileSource(String urlString) {
|
||||
URL url = null;
|
||||
@ -109,15 +109,15 @@ public abstract class UrlTileSource extends TileSource {
|
||||
return mUrl;
|
||||
}
|
||||
|
||||
public void setHttpEngine(HttpEngine httpEngine) {
|
||||
this.httpEngine = httpEngine;
|
||||
public void setHttpEngine(HttpEngine.Factory httpFactory) {
|
||||
mHttpFactory = httpFactory;
|
||||
}
|
||||
|
||||
public HttpEngine getHttpEngine() {
|
||||
if (httpEngine == null) {
|
||||
httpEngine = new LwHttp(getUrl());
|
||||
if (mHttpFactory == null) {
|
||||
return new LwHttp(getUrl());
|
||||
}
|
||||
|
||||
return httpEngine;
|
||||
return mHttpFactory.create();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user