GgaREST


Source link: https://github.com/greengrowapps/ggarest

GgaREST

Introduction

This library makes it easy to work with http connections on Android, but you can also make it work on Java vanilla by modifying only a couple of classes. It's especially designed to work with JSON, as it uses a JSON de/serializer by default. Its strongest points are the following :

  • When you make a request (get, post, put, delete) and you register the listeners, your listener code will be code in the main thread. This design allows the modification of UI components directly in the listener code.
  • You can specify the object that you are expecting and it will be deserialized using the Jackson library. If it's a list, you also can use the list listener to deserialize it as an object list.

Usage

To use the GgaRest class you only have to call the init method when your application start. Here is an example by adding default headers like an Api key.

GgaRest.init(this);
 GgaRest.addDefaulteader("Accept","application/json");

This example gets the content of the url given and deserializes the content as an OriginAndUrl object:

GgaRest.ws().get("http://httpbin.org/get")

.onSuccess(OriginAndUrl.class, new OnObjResponseListener<OriginAndUrl>() {

 @Override

 public void onResponse(int code, OriginAndUrl object, Response fullResponse) {

  txtUrl.setText(object.url);

  txtIp.setText(object.origin);

 
}

}
)

.onOther(new OnResponseListener() {

 @Override

 public void onResponse(int code, Response fullResponse, Exception exception) {

  Toast.makeText(MainActivity.this, "Get failed", Toast.LENGTH_SHORT).show();

 
}

}
)

.execute();

Or here we use basic auth to retrieve the commits of this repository:

 GgaRest.useBasicAuth(GITHUB_USERNAME,PASSWORD);

GgaRest.ws()

 .get("https://api.github.com/repos/greengrowapps/ggarest/commits")

 .onSuccess(Commit.class, new OnListResponseListener<Commit>() {

  @Override

  public void onResponse(int code, List<Commit> object, Response fullResponse) {

populateCommits(object);

  
}

 
}
)

 .onResponse(401, new OnResponseListener() {

  @Override

  public void onResponse(int code, Response fullResponse, Exception exception) {

Toast.makeText(MainActivity.this, "Unauthorized", Toast.LENGTH_SHORT).show();

  
}

 
}
)

 .execute();

It's possible to mock some calls for test prouposes:


  GgaRest.ws().mockGet("http://mysite/someendpoint/")

  .responseCode(200)

  .responseContent("{
\"text\":\"hello\"
}
")

  .save();

   

Integration

1) Add as a dependency to your build.gradle:

dependencies {

  compile 'com.greengrowapps:ggarest:0.8' 
}

2) Avoid duplicate files copied in APK adding this inside the android tag in your build.gradle:

 packagingOptions {

exclude 'META-INF/ASL2.0'

exclude 'META-INF/LICENSE'

exclude 'META-INF/MANIFEST.MF'

exclude 'META-INF/NOTICE'

exclude 'META-INF/LICENSE.txt'

exclude 'META-INF/NOTICE.txt'
  
}

3) Initialize the object in one of your activities' onCreate to start using it:

GgaRest.init(this);

Things to develop

This version is still not 1.0, which means api changes could happen. It needs some test and development.

If you want to collaborate, here is a checklist of planned features you can help working on:

  • More authentication methods like OAuth, OAuth2
  • Response stream to string using the headers encoding
  • Request cache
  • More tests

License


Apache License Version 2.0, January 2004
  Copyright 2015 Green Grow Apps SC
  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.  

Resources

The Android Support Library for NFC.

Simplest way to get all images based on date, name, id, and other customizations.

A Magic library to take photos and select pictures in Android. In a simple way and if you need it also save the pictures in device.

A simple wrapper around most Firebase functions with RxJava.

Fluent mockito.

Gradle Plugin to check library licenses and generate license pages.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes