Smartface Developer Center

Smartface Developer Center

Welcome to the Smartface Developer Hub. You'll find comprehensive guides and documentation to help you start developing and managing native iOS and Android apps with Smartface Cloud.

Get Started    

Http

API Reference: Net.Http

Http module is used for sending http requests.

RESTful Services

"request()" method of Http provides making a REST request. It takes an argument named method. You can specify the method by setting this parameter.

Handling Response of a Request

Each http method has a "onLoad" callback and a "onError" callback. You can handle error and response using these callbacks.

const Page = require("sf-core/ui/page");
const extend = require("js-base/core/extend");
const ImageView = require('sf-core/ui/imageview');
const FlexLayout = require('sf-core/ui/flexlayout');
const Http = require("sf-core/net/http");

var PageHttp = extend(Page)(
    function PageHttp(_super) {
        _super(this, {
            onShow: function() {
                this.statusBar.visible = false;
                this.headerBar.visible = false;
            }
        });
				
      	var sessionManager = new Http();
        var request = sessionManager.requestImage({
          url : 'https://httpbin.org/image/png',
          onLoad : function(e) {
            // Image loaded.
            myImageView.image = e.image;
        	},
          onError : function(e) {
            // Http request image failed.
            alert(e.message);
        	}
        });
        
        var myImageView = new ImageView({
            width: 100,
            height: 100,
            alignSelf: FlexLayout.AlignSelf.CENTER,
            imageFillType: ImageView.FillType.STRETCH
        });

        this.layout.addChild(myImageView);
        this.layout.justifyContent = FlexLayout.JustifyContent.CENTER;
    }
);
module.exports = PageHttp;

Consider Http Behavior

If the given url is invalid, Http request does not fire onError callback.

Android Permissions

You have to add following permission to your AndroidManifest.xml file before publishing your project.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Also you can check Application Permission Management for other permissions.

Notes

If you try to make a request when no network connection, onError callback triggers.

Also, onError callbacks triggers when timeout.

If you pass body parameter with http methods that do not require body parameter, iOS throws an exception, but Android ignores the body parameter and works fine.