Skip to content

Android Library to make API calls simpler using Google Volley ☺️

License

Notifications You must be signed in to change notification settings

ferozbaig96/VolleySimple

Repository files navigation

VolleySimple

Android Library to make API calls simpler using Google Volley

Specs

Show some ❤️

GitHub stars GitHub forks GitHub watchers GitHub followers

Setup and usage

To include this library to your project add dependency in build.gradle file:

    repositories {
        jcenter()
        ...
        maven { url 'https://www.jitpack.io' }

    }
    
    dependencies {
        ...
        compile 'com.github.ferozbaig96:VolleySimple:1.1'
    }

Then in your Activity :

    VolleySimple.getInstance(this)
                .placeJsonObjectRequest(
                "myTag",  // tag (optional)
                url,      // url of the request
                Request.Method.GET,   // Request.Method.GET or Request.Method.POST
                params,    // parameters (optional)
                headers,   // http headers (optional)
                this);     // callback for handling response

Then make your Activity implement ServerCallback

    public class MainActivity extends AppCompatActivity implements ServerCallback {

Handle the response in your Activity

    @Override
    public void onAPIResponse(String apiTag, Object response) {
       //handle response
    }

    @Override
    public void onErrorResponse(String apiTag, VolleyError error) {
       // handle error 
    }

Additional Options

Apart from JsonObjectRequest, you can make the following requests

    // StringRequest
    VolleySimple.getInstance(this)
                .placeStringRequest(...);
    
    // JsonArrayRequest
    VolleySimple.getInstance(this)
                .placeJsonArrayRequest(...);
                

Customizations

Set timeout for requests

    VolleySimple.getInstance(this)
                .setInitialTimeoutMs(5000) // Default value = 3000 ms
                .placeJsonObjectRequest(...);         

Set maximum no. of retries for requests

    VolleySimple.getInstance(this)
                .setMaxNoOfTries(4) // Default value = 2
                .placeJsonObjectRequest(...);         

Tips & Tricks

GSON for handling responses

  // Create your own GSON Pojo Class. Here, SamplePojo represents a GSON Pojo
  
   @Override
    public void onAPIResponse(String apiTag, Object response) {
        if ("SIGN_UP_REQUEST".equals(apiTag)) {
            SamplePojo samplePojo = new Gson().fromJson(response.toString(), SamplePojo.class);
  
            displayname_textview.setText(samplePojo.getName);
            age_textview.setText(samplePojo.getAge());
            ...
        }
    }

Changelog

Version Changes
v.1.0 First public release
v.1.1 Minor changes

License

Copyright 2017 Feroz Baig

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.

About

Android Library to make API calls simpler using Google Volley ☺️

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages