Giter Club home page Giter Club logo

cordova-retrofithttp's Introduction

cordova-RetrofitHTTP

A Cordova/ PhoneGap Plugin (for Android only) which helps to make HTTP requests using the awesome Retrofit 2 library under the hood.

This project is inspired by https://github.com/wymsee/cordova-HTTP

Installation

cordova plugin add cordova-plugin-retrofit-http

Usage

AngularJS

This plugin creates a cordovaHTTP service inside of a cordovaHTTP module. You must load the module when you create your app's module.

var app = angular.module('myApp', ['ngRoute', 'ngAnimate', 'cordovaRetrofitHTTP']);

You can then inject the cordovaRetrofitHTTP service into your controllers. Make sure that you load cordova.js or phonegap.js after AngularJS is loaded.

Not AngularJS

This plugin registers a cordovaRetrofitHTTP global on window

Async Functions

These functions all take success and error callbacks as their last 2 arguments.

get

Execute a GET request. Takes a URL, parameters, and headers. See the post documentation for details on what is returned on success and failure.

cordovaRetrofitHTTP.get("https://example.com/", {
    id: 101,
    message: "test message"
}, { Authorization: "OAuth2: token" }, function(response) {
    console.log(response.status);
}, function(response) {
    console.error(response.error);
});

post

Execute a POST request. Takes a URL, parameters, and headers.

success

The success function receives a response object with 3 properties: status, data, and headers. Status is the HTTP response code. Data is the response from the server as a string. Headers is an object with the headers. Here's a quick example:

{
    status: 200,
    data: "{'id': 12, 'message': 'test'}",
    headers: {
        "Content-Length": "247"
    }
}

Most apis will return JSON meaning you'll want to parse the data like in the example below:

cordovaRetrofitHTTP.post("https://example.com/", {
    id: 12,
    message: "test"
}, { Authorization: "OAuth2: token" }, function(response) {
    // prints 200
    console.log(response.status);
    try {
        response.data = JSON.parse(response.data);
        // prints test
        console.log(response.data.message);
    } catch(e) {
        console.error("JSON parsing error");
    }
}, function(response) {
    // prints 403
    console.log(response.status);

    //prints Permission denied
    console.log(response.error);
});

failure

The error function receives a response object with 3 properties: status, error and headers. Status is the HTTP response code. Error is the error response from the server as a string. Headers is an object with the headers. Here's a quick example:

{
    status: 403,
    error: "Permission denied",
    headers: {
        "Content-Length": "247"
    }
}

Project Dependencies

JAR

-commons-io-2.5.jar
-converter-gson-2.3.0.jar
-gson-2.8.1.jar
-okio-1.13.0.jar
-retrofit-2.3.0.jar

Cordova Plugin

-cordova-plugin-file

cordova-retrofithttp's People

Contributors

joel-raju avatar sai7135 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.