OkHttp 3 engine, closes #138

This commit is contained in:
Emux
2017-02-11 22:15:55 +02:00
parent d22dc79655
commit 817bd13d7b
7 changed files with 55 additions and 58 deletions

View File

@@ -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;
}
/**