Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.SUKI-Huang:android_volley_pro:1.0.26'
}
dependencies {
implementation("com.github.SUKI-Huang:android_volley_pro:1.0.26")
}
<dependency>
<groupId>com.github.SUKI-Huang</groupId>
<artifactId>android_volley_pro</artifactId>
<version>1.0.26</version>
</dependency>
libraryDependencies += "com.github.SUKI-Huang" % "android_volley_pro" % "1.0.26"
:dependencies [[com.github.SUKI-Huang/android_volley_pro "1.0.26"]]
This library is base on android-volley, in order to decrease code and save developer's time, it easier to use than orginal volley library.
In the oringal Volley project, there are many kinds of request, developers use complicated flow and lots of code to build request, in addition, the usability of cache feature is not enough, so this is what VolleyPro solve for.
1. Gradle dependency (recommended)
build.gradle
:allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
build.gradle
:android {
...
useLibrary 'org.apache.http.legacy'
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
...
}
dependencies {
compile 'com.github.SUKI-Huang:android_volley_pro:1.0.19'
//Libraries down below are required
compile 'com.google.code.gson:gson:2.4'
compile 'com.android.support:support-annotations:25.1.1'
compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
compile('org.apache.httpcomponents:httpmime:4.3.6') {
exclude module: "httpclient"
}
}
2. Proguard file
#apache
-keep class org.apache.http.** { *; }
-keep class org.apache.commons.codec.** { *; }
-keep class org.apace.commons.logging.** { *; }
#android net relative
-keep class com.android.internal.http.multipart.** { *; }
-keep class android.net.compatibility.** { *; }
-keep class android.net.http.** { *; }
-dontwarn org.apache.http.**
-dontwarn android.net.**
3. In your Java code
String endpoint = "https://xxx.xxx.xxx/xxx/xx/xx";
volleyPro.setOnEvent(new SimpleEvent<String>(String.class) {
@Override
public void OnSuccess(String result) {
//on success
}
@Override
public void OnFailed(int code, String msg) {
//on failed
}
});
volleyPro.request(Method.GET, endpoint);
String endpoint = "https://xxx.xxx.xxx/xxx/xx/xx";
volleyPro.setOnEvent(new SimpleEvent<String>(String.class) {
@Override
public void OnSuccess(String result) {
//on success
}
@Override
public void OnFailed(int code, String msg) {
//on failed
}
});
volleyPro.request(
Method.POST,
endpoint,
new BaseVolleyPro.Option()
.setHeader(
new HashMap<String, String>() {
{
put("token", "xxxxxxxxxx");
put("userid", "xxxxxxxxx");
}
}
)
.setParameters(
new HashMap<String, String>() {
{
put("name", "Suki");
put("gender", "1");
}
}
)
);
String endpoint="https://xxx.xxx.xxx/xxx/xx/xx";
volleyPro.setOnEvent(new SimpleEvent<String>(String.class) {
@Override
public void OnSuccess(String result) {
//on success
}
@Override
public void OnFailed(int code, String msg) {
//on failed
}
});
volleyPro.request(
Method.GET,
endpoint,
new VolleyPro.Option()
//set header (optional)
.setHeader(
new HashMap<String, String>() {{
put("userid", "xxxx");
put("usercode", "xxxx");
put("apiKey", "xxxx");
}}
)
//set parameters (optional)
.setParameters(
new HashMap<String, String>() {{
put("title", "this is title");
}}
)
//set cache (optional)
.setCache(
//cache path
"data/data/" + getPackageName() + "/volleyCache.json",
//expired time (millisecond)
6000,
//if network is unavailable use older cache
true
)
);
String endpoint = "https://xxx.xxx.xxx/xxx/xx/xx";
volleyPro.setOnEvent(new SimpleEvent<String>(String.class) {
@Override
public void OnSuccess(String result) {
//on success
}
@Override
public void OnFailed(int code, String msg) {
//on failed
}
@Override
public void OnMultiPartProgress(float progress) {
//uplaod progress
}
});
volleyPro.request(
endpoint,
new VolleyPro.MultiPartOption()
.setHeader(
new HashMap<String, String>() {{
put("userid", "xxxx");
put("usercode", "xxxx");
put("apiKey", "xxxx");
}}
)
//set parameters (optional)
.setParameters(
new ContentHashMap<String, ContentBody>() {{
putText("title", "this is title");
putFile("file", filePath);
putFile("file", filePath);
putFile("file", new File(filePath));
putBinary("byte", getByte("filePath"), "xxx.jpg");
putStream("stream", getFileInputStream(filePath), "xxx.jpg");
}})
.enableMultiPartProgress()
);
String endpoint="https://xxx.xxx.xxx/xxx/xx/xx";
volleyPro.setOnEvent(new SimpleEvent<File>(File.class) {
@Override
public void OnSuccess(File file) {
//on success
}
@Override
public void OnFailed(int code, String msg) {
//on failed
}
@Override
public void OnFileProgress(float progress) {
//on download progress changed
}
});
volleyPro.requestFile(
Method.GET,
endpoint,
new VolleyPro.Option()
//set header (optional)
.setHeader(
new HashMap<String, String>() {{
put("userid", "xxxx");
put("usercode", "xxxx");
put("apiKey", "xxxx");
}}
)
//set parameters (optional)
.setParameters(
new HashMap<String, String>() {{
put("title", "this is title");
}}
)
//set cache (required)
.setCache(
//cache path
"data/data/" + getPackageName() + "/volleyCache.json",
//expired time (millisecond)
6000,
//if network is unavailable use older cache
true
)
//enable FileProgress callback
.enableFileProgress()
);
Example is DataItem
, change DataItem
to the class you need, VolleyPro will convert via gson automatically
String endpoint="https://xxx.xxx.xxx/xxx/xx/xx";
volleyPro.setOnEvent(new SimpleEvent<DataItem>(DataItem.class) {
@Override
public void OnSuccess(DataItem dataItem) {
//on success
}
@Override
public void OnFailed(int code, String msg) {
//on failed
}
});
volleyPro.request(Method.GET, endpoint);
Example is DataItem
, change DataItem
to the class you need, VolleyPro will convert via gson automatically
String endpoint="https://xxx.xxx.xxx/xxx/xx/xx";
volleyPro.setOnEvent(new SimpleEvent<ArrayList<DataItem>>(new TypeToken<ArrayList<DataItem>>(){}.getType()) {
@Override
public void OnSuccess(ArrayList<DataItem> result) {
//on success
}
@Override
public void OnFailed(int code, String msg) {
//on failed
}
});
volleyPro.request(Method.GET, endpoint);
Copyright (C) 2017 Suki Huang
Copyright (C) 2011 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.