diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..30aa626 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml deleted file mode 100644 index 96cc43e..0000000 --- a/.idea/compiler.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml deleted file mode 100644 index e7bedf3..0000000 --- a/.idea/copyright/profiles_settings.xml +++ /dev/null @@ -1,3 +0,0 @@ - - - \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml deleted file mode 100644 index 97626ba..0000000 --- a/.idea/encodings.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 45e5801..1e03db2 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -3,9 +3,8 @@ - + - - - - - 1.8 - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml index 5a6f281..f8d9a58 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -4,7 +4,7 @@ - + \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index b613135..f5d96d6 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,12 +1,12 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 26 - buildToolsVersion "26.0.1" + compileSdkVersion 27 + buildToolsVersion "28.0.0" defaultConfig { applicationId "com.rw.velocity" minSdkVersion 14 - targetSdkVersion 26 + targetSdkVersion 27 versionCode 2 versionName "1.0.1" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" @@ -24,7 +24,7 @@ dependencies { androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) - compile 'com.android.support:appcompat-v7:26.0.2' + compile 'com.android.support:appcompat-v7:27.1.1' testCompile 'junit:junit:4.12' compile project(':velocity') compile 'com.squareup.picasso:picasso:2.5.2' diff --git a/app/src/main/java/com/rw/velocityapp/MainActivity.java b/app/src/main/java/com/rw/velocityapp/MainActivity.java index b1b93c3..5539e56 100644 --- a/app/src/main/java/com/rw/velocityapp/MainActivity.java +++ b/app/src/main/java/com/rw/velocityapp/MainActivity.java @@ -56,6 +56,7 @@ protected void onCreate(Bundle savedInstanceState) Velocity.getSettings().setMaxRedirects(10); Velocity.getSettings().setLoggingEnabled(true); Velocity.getSettings().setCustomLogger(new CustomLogger("Velocity")); + //Velocity.getSettings().setResponseCompressionEnabled(true); @@ -71,7 +72,9 @@ protected void onCreate(Bundle savedInstanceState) @Override public void onClick(View view) { - textRequest("http://www.google.com"); + //textRequest("http://139.162.28.197:8080/listing/search"); + downloadRequest("http://139.162.28.197:8080/listing/image/a26d7290-ab75-4291-b86b-d39e43eb2eed"); + //textRequest("http://139.162.28.197:8080/listing/image/a26d7290-ab75-4291-b86b-d39e43eb2eed"); } }); @@ -146,6 +149,7 @@ private void downloadRequest(String url) Velocity.download(url) .setDownloadFile(filepath) + .withResponseCompression() .addToDownloadsFolder(this, "download test", "test file") .withProgressListener(new Velocity.ProgressListener() { @@ -180,19 +184,20 @@ private void textRequest(String url) { Velocity .get(url) - .withHeader("key", "velue") + .withHeader("postcode", "3020") + .withResponseCompression() .connect(new Velocity.ResponseListener() { @Override public void onVelocitySuccess(Velocity.Response response) { Log.d("IMG", "response: " + response.body); - textView.setText(response.body); + //textView.setText(response.body); if (response.image != null) imageView.setImageBitmap(response.image); Log.d("IMG", "response toString:"); - Log.d("IMG", response.toString()); + //Log.d("IMG", response.toString()); } @Override diff --git a/build.gradle b/build.gradle index 11780ce..2931355 100644 --- a/build.gradle +++ b/build.gradle @@ -3,9 +3,10 @@ buildscript { repositories { jcenter() + google() } dependencies { - classpath 'com.android.tools.build:gradle:2.3.3' + classpath 'com.android.tools.build:gradle:3.1.3' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index fd5be38..7a2ae64 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.8-all.zip diff --git a/velocity/build.gradle b/velocity/build.gradle index 149d4a3..3e88739 100644 --- a/velocity/build.gradle +++ b/velocity/build.gradle @@ -2,14 +2,14 @@ apply plugin: 'com.android.library' //noinspection GroovyMissingReturnStatement android { - compileSdkVersion 26 - buildToolsVersion "26.0.1" + compileSdkVersion 27 + buildToolsVersion "28.0.0" defaultConfig { minSdkVersion 14 - targetSdkVersion 26 - versionCode 4 - versionName "1.0.3" + targetSdkVersion 27 + versionCode 5 + versionName "1.0.5" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" @@ -28,6 +28,6 @@ dependencies { exclude group: 'com.android.support', module: 'support-annotations' }) testCompile 'junit:junit:4.12' - compile 'com.google.code.gson:gson:2.8.0' - compile 'com.android.support:support-annotations:26.0.2' + compile 'com.google.code.gson:gson:2.8.5' + compile 'com.android.support:support-annotations:27.1.1' } diff --git a/velocity/src/androidTest/java/com/rw/velocity/ExampleInstrumentedTest.java b/velocity/src/androidTest/java/com/rw/velocity/ExampleInstrumentedTest.java index 521ea52..1665195 100644 --- a/velocity/src/androidTest/java/com/rw/velocity/ExampleInstrumentedTest.java +++ b/velocity/src/androidTest/java/com/rw/velocity/ExampleInstrumentedTest.java @@ -52,6 +52,7 @@ public void get() throws Exception String url ="http://httpbin.org/get"; Velocity.initialize(3); + Velocity.getSettings().setResponseCompressionEnabled(true); Velocity.get(url) .withHeader("header1", "value1") .withHeader("header2", "value2") @@ -418,6 +419,7 @@ public void postMultiPart() throws Exception String url ="http://httpbin.org/post"; Velocity.initialize(3); + Velocity.getSettings().setResponseCompressionEnabled(true); Velocity.post(url) .withFormData("key1", "value1") .withFormData("key2", "value2") @@ -529,6 +531,7 @@ public void imagePng() throws Exception String url ="http://httpbin.org/image/png"; Velocity.initialize(3); + Velocity.getSettings().setResponseCompressionEnabled(true); Velocity.get(url) .connect(new Velocity.ResponseListener() { diff --git a/velocity/src/main/java/com/rw/velocity/DownloadRequest.java b/velocity/src/main/java/com/rw/velocity/DownloadRequest.java index ef6967a..4b71a24 100644 --- a/velocity/src/main/java/com/rw/velocity/DownloadRequest.java +++ b/velocity/src/main/java/com/rw/velocity/DownloadRequest.java @@ -5,10 +5,12 @@ import android.os.Build; import android.webkit.MimeTypeMap; +import java.io.BufferedInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.util.zip.GZIPInputStream; /** * velocity-android @@ -36,6 +38,11 @@ boolean readResponse() if (mResponseCode / 100 == 2) //all 2xx codes are OK { InputStream inputStream = mConnection.getInputStream(); + if(mConnection.getHeaderField("Content-Encoding") != null && mConnection.getHeaderField("Content-Encoding").contains("gzip")) + { + inputStream = new GZIPInputStream(inputStream); + } + FileOutputStream outputStream = new FileOutputStream(mBuilder.downloadFile); diff --git a/velocity/src/main/java/com/rw/velocity/Request.java b/velocity/src/main/java/com/rw/velocity/Request.java index 4e5576b..7d55b54 100644 --- a/velocity/src/main/java/com/rw/velocity/Request.java +++ b/velocity/src/main/java/com/rw/velocity/Request.java @@ -19,6 +19,7 @@ import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; +import java.util.zip.GZIPInputStream; import javax.net.ssl.HttpsURLConnection; @@ -100,9 +101,12 @@ void setupRequestHeaders() { mConnection.setRequestProperty("User-Agent", Velocity.Settings.USER_AGENT); - if(mBuilder.contentType != null && !mBuilder.contentType.equalsIgnoreCase(Velocity.ContentType.TEXT.toString())) + if((mBuilder.contentType != null && !mBuilder.contentType.equalsIgnoreCase(Velocity.ContentType.TEXT.toString())) || mBuilder.compressed) mConnection.setRequestProperty("Content-Type", mBuilder.contentType); + if(Velocity.Settings.GZIP_ENABLED) + mConnection.setRequestProperty("Accept-Encoding", "gzip,deflate"); + if (!mBuilder.headers.isEmpty()) { @@ -251,16 +255,21 @@ boolean readResponse() { if (mResponseCode / 100 == 2) //all 2xx codes are OK { + InputStream in = mConnection.getInputStream(); + if(mConnection.getHeaderField("Content-Encoding") != null && mConnection.getHeaderField("Content-Encoding").contains("gzip")) + { + in = new GZIPInputStream(in); + } if (mConnection.getContentType() != null && mConnection.getContentType().startsWith("image")) { - mResponseImage = BitmapFactory.decodeStream(mConnection.getInputStream()); + mResponseImage = BitmapFactory.decodeStream(in); mResponse.append(mConnection.getContentType()); } else { - InputStream in = new BufferedInputStream(mConnection.getInputStream()); - BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + BufferedInputStream inStream = new BufferedInputStream(in); + BufferedReader reader = new BufferedReader(new InputStreamReader(inStream)); String line; while ((line = reader.readLine()) != null) diff --git a/velocity/src/main/java/com/rw/velocity/RequestBuilder.java b/velocity/src/main/java/com/rw/velocity/RequestBuilder.java index 3409358..20969a5 100644 --- a/velocity/src/main/java/com/rw/velocity/RequestBuilder.java +++ b/velocity/src/main/java/com/rw/velocity/RequestBuilder.java @@ -43,6 +43,7 @@ public class RequestBuilder final String originUrl; Velocity.ProgressListener progressListener; boolean mocked = false; + boolean compressed = false; String mockResponse = "Global Mock is enabled. Velovity will mock all calls and return this message."; private Velocity.RequestType requestType = Velocity.RequestType.Text; @@ -318,6 +319,12 @@ public RequestBuilder withProgressListener(Velocity.ProgressListener listener) return this; } + public RequestBuilder withResponseCompression() + { + this.compressed = true; + return this; + } + /** * Make a network request to recieve data in the callback. * See also {@link RequestBuilder#connect(int, Velocity.ResponseListener)} diff --git a/velocity/src/main/java/com/rw/velocity/Velocity.java b/velocity/src/main/java/com/rw/velocity/Velocity.java index 49a31ec..93aef9d 100644 --- a/velocity/src/main/java/com/rw/velocity/Velocity.java +++ b/velocity/src/main/java/com/rw/velocity/Velocity.java @@ -308,6 +308,7 @@ public static class Settings static int MAX_REDIRECTS = 10; static boolean LOGS_ENABLED = false; static String USER_AGENT = "velocity-android-http-client"; + static boolean GZIP_ENABLED = false; //upload settings static final String LINEEND = "\r\n"; @@ -455,6 +456,16 @@ public void setCustomLogger(Logger logger) { NetLog.setLogger(logger); } + + /** + * Enable gzip compression + * Adds 'Accept-Encoding = gzip,deflate' header to all requests + * @param enabled if true, enabled compression + */ + public void setResponseCompressionEnabled(boolean enabled) + { + GZIP_ENABLED = enabled; + } } /**