AndyFriends/vrequest


Android Internet requests using Volley library

Download


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"]]
        
        

Readme


An Android Internet requests Library that uses Google Volley

A taste of how it works

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();

How to import

Android Studio

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
}

Than you can start getting, posting, patching, deleting, or customising

You can now post something

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();

You must want to handle errors

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
                    }
                })
                ...

Tip of advice:

extending VRequest you can @Override deliverError() method
class 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);
        }
    }
}
now it's easy to handle the errors on your Views or something
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
                    }
                })
                ...

Useful links

Google Volley

Google GSON

More details coming up soon folks