项目作者: ferozbaig96

项目描述 :
Android Library to make API calls simpler using Google Volley :relaxed:
高级语言: Java
项目地址: git://github.com/ferozbaig96/VolleySimple.git
创建时间: 2017-05-28T17:25:08Z
项目社区:https://github.com/ferozbaig96/VolleySimple

开源协议:Apache License 2.0

下载


VolleySimple

Android Library to make API calls simpler using Google Volley

Specs

Show some :heart:

GitHub stars GitHub forks GitHub watchers GitHub followers

Setup and usage

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

  1. repositories {
  2. jcenter()
  3. ...
  4. maven { url 'https://www.jitpack.io' }
  5. }
  6. dependencies {
  7. ...
  8. compile 'com.github.ferozbaig96:VolleySimple:1.1'
  9. }

Then in your Activity :

  1. VolleySimple.getInstance(this)
  2. .placeJsonObjectRequest(
  3. "myTag", // tag (optional)
  4. url, // url of the request
  5. Request.Method.GET, // Request.Method.GET or Request.Method.POST
  6. params, // parameters (optional)
  7. headers, // http headers (optional)
  8. this); // callback for handling response

Then make your Activity implement ServerCallback

  1. public class MainActivity extends AppCompatActivity implements ServerCallback {

Handle the response in your Activity

  1. @Override
  2. public void onAPIResponse(String apiTag, Object response) {
  3. //handle response
  4. }
  5. @Override
  6. public void onErrorResponse(String apiTag, VolleyError error) {
  7. // handle error
  8. }

Additional Options

Apart from JsonObjectRequest, you can make the following requests

  1. // StringRequest
  2. VolleySimple.getInstance(this)
  3. .placeStringRequest(...);
  4. // JsonArrayRequest
  5. VolleySimple.getInstance(this)
  6. .placeJsonArrayRequest(...);

Customizations

Set timeout for requests

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

Set maximum no. of retries for requests

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

Tips & Tricks

GSON for handling responses

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


Changelog

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


License

  1. Copyright 2017 Feroz Baig
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.