Gga Rest - Java version

A small library to make easy connect to a REST webservice

License

License

Categories

Categories

Java Languages
GroupId

GroupId

com.greengrowapps
ArtifactId

ArtifactId

ggarest-java
Last Version

Last Version

0.0.2
Release Date

Release Date

Type

Type

jar
Description

Description

Gga Rest - Java version
A small library to make easy connect to a REST webservice
Project URL

Project URL

https://github.com/greengrowapps/ggarest
Source Code Management

Source Code Management

https://github.com/greengrowapps/ggarest

Download ggarest-java

How to add to project

<!-- https://jarcasting.com/artifacts/com.greengrowapps/ggarest-java/ -->
<dependency>
    <groupId>com.greengrowapps</groupId>
    <artifactId>ggarest-java</artifactId>
    <version>0.0.2</version>
</dependency>
// https://jarcasting.com/artifacts/com.greengrowapps/ggarest-java/
implementation 'com.greengrowapps:ggarest-java:0.0.2'
// https://jarcasting.com/artifacts/com.greengrowapps/ggarest-java/
implementation ("com.greengrowapps:ggarest-java:0.0.2")
'com.greengrowapps:ggarest-java:jar:0.0.2'
<dependency org="com.greengrowapps" name="ggarest-java" rev="0.0.2">
  <artifact name="ggarest-java" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.greengrowapps', module='ggarest-java', version='0.0.2')
)
libraryDependencies += "com.greengrowapps" % "ggarest-java" % "0.0.2"
[com.greengrowapps/ggarest-java "0.0.2"]

Dependencies

compile (2)

Group / Artifact Type Version
org.codehaus.jackson : jackson-core-asl jar 1.9.10
org.codehaus.jackson : jackson-mapper-asl jar 1.9.10

test (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

GgaREST

Maven Central Maven Central Travis Android Arsenal

Introduction

This library makes it easy to work with http connections on Android or Java. 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.

Android Configuration

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

Java Configuration

You not need to init the Library. You can get a Webservice instance directly calling GgaRest.ws()

Usage

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

   

Android Integration

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

dependencies {
    compile 'com.greengrowapps:ggarest:0.16'
}

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 your application onStart or in one of your activities' onCreate to start using it:

GgaRest.init(this);

Java Integration

Add as a dependency to your build.gradle or pom.xml:

dependencies {
    compile 'com.greengrowapps:ggarest-java:0.0.2'
}
<dependency>
    <groupId>com.greengrowapps</groupId>
    <artifactId>ggarest-java</artifactId>
    <version>0.0.2</version>
</dependency>

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.

Versions

Version
0.0.2
0.0.1