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.AndyFriends:vrequest:1.9'
}
dependencies {
implementation("com.github.AndyFriends:vrequest:1.9")
}
<dependency>
<groupId>com.github.AndyFriends</groupId>
<artifactId>vrequest</artifactId>
<version>1.9</version>
</dependency>
libraryDependencies += "com.github.AndyFriends" % "vrequest" % "1.9"
:dependencies [[com.github.AndyFriends/vrequest "1.9"]]
new VRequest()
.with(context)
.load(url)
.post(params)
.onSuccess(new Response.Listener<MyObject>() {
@Override
public void onResponse(MyObject myObject) {
//TODO be happy using your object
}
})
.fetch();
Add this to your Project build.grandle:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
reference: jitpack.io
and this to your App's build.gradle:
dependencies {
compile 'com.github.andyfriends:vrequest:v1.2' //look for current version
}
new VRequest()
.with(this) //our context
.load(url) //your endpoint
.post(params) //some JSONObject containing your data
.onSuccess(new Response.Listener<MyObject>() { //triggered on success
@Override
public void onResponse(MyObject myObject) {
//TODO be happy using your object
}
})
.fetch();
new VRequest()
....
.onError(new Response.ErrorListener<>() {
@Override
public void onResponse(VolleyError error) {
//TODO you can extend VRequest and use your own
//error class to make things easy for you
}
})
...
@Override
deliverError()
methodclass MyRequest<T> extends VRequest<T> {
@Override
public void deliverError(VolleyError error) {
// here we provide a response for the default listener
if (null != mVolleyErrorListener) mVolleyErrorListener.onErrorResponse(error);
// here we customize
if (null != mErrorListener) {
//this is how we get the error data from our API
NetworkResponse responseError = error.networkResponse;
String dataError = new String(responseError.data);
//maybe your API returns some Json
JSONObject jsonError = new JSONObject(dataError);
//Gson can give a hand
MyUnderstandableErrorClass errorClass = new Gson().fromJson(jsonError, MyUnderstandableErrorClass.class);
mErrorListener.onResponse((T) errorClass);
}
}
}
new MyRequest()
....
.onError(new Response.Listener<MyUnderstandableErrorClass>() {
@Override
public void onResponse(MyUnderstandableErrorClass error) {
//TODO give the user a better response than "something went wrong" everytime
}
})
...