Skip to content

Commit

Permalink
updated Gradle, added response compression (gzip)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravindu committed Jul 14, 2018
1 parent 1dd5da4 commit 3cacc9a
Show file tree
Hide file tree
Showing 18 changed files with 100 additions and 85 deletions.
29 changes: 29 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 0 additions & 22 deletions .idea/compiler.xml

This file was deleted.

3 changes: 0 additions & 3 deletions .idea/copyright/profiles_settings.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/encodings.xml

This file was deleted.

3 changes: 1 addition & 2 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions .idea/inspectionProfiles/Project_Default.xml

This file was deleted.

25 changes: 5 additions & 20 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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'
Expand Down
13 changes: 9 additions & 4 deletions app/src/main/java/com/rw/velocityapp/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);



Expand All @@ -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");
}
});

Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 7 additions & 7 deletions velocity/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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'
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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()
{
Expand Down
7 changes: 7 additions & 0 deletions velocity/src/main/java/com/rw/velocity/DownloadRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 13 additions & 4 deletions velocity/src/main/java/com/rw/velocity/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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())
{
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions velocity/src/main/java/com/rw/velocity/RequestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)}
Expand Down
Loading

0 comments on commit 3cacc9a

Please sign in to comment.