cancel
Showing results for 
Search instead for 
Did you mean: 

Regarding how to Read data when it is in offline in SMP 3.0

Former Member
0 Kudos

I have a big problem guyz...I am working on a hybrid Mobile App

When I am in Online I am able to read the data.When I kill the application and relaunch the app and place the App in Airplane mode and when I try to read the data It is throwing me a error in browser stating  "ERR_CONN_RESET" and in the device I am getting an error such as HTTPREQUESTFAILED....

Mithun Please help us in reading the data in offline Mode.Please help us it is on high priority

Tags edited by: Jitendra Kansal (Moderator)

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi Mithun,

I implemented the code given by you.But it still throws me a HTTP REQUEST FAILED ERROR please help me out with this

midhun_vp
Active Contributor
0 Kudos

Hi Abhinav,

To troubleshoot the issue I would suggest you to first try with the same Odata service used in the guide - . You can follow the same steps as per the guide. Later use your own Odata service.

Regards,Midhun

SAP Technology RIG

chinna_babu2
Active Participant
0 Kudos

Check whether you are calling sap.OData.applyHttpClient();  after store opened.

Regards,

Murthy

Former Member
0 Kudos

Yes, I calling sap.OData.applyHttpClient() ...

I am wondering if it can be a reference problem. When adding the OData plugin for a Windows project via the Cordova CLI, the terminal indicates the following:


        **********************************************************

        * Manual steps required to add this plugin on Windows 8.1 and Windows Ph

one 8.1 only.

        *

        * This plugin requires a Native windows references that cannot be added

automatically.

        *

        * ADD NATIVE REFERENCES FOR THE WINDOWS 8.1 PROJECT

        * 1) Open the Cordova generated solution in Visual Studio.

        * 2) Select the project generated for Windows 8.1. Select the References

node, Right Click and select "Add Reference".

        * 3) Click "Browse", locate the OfflineOData plugin location on your dis

k ({OfflineODataPluginDirectory}/windows81/bin/win)

        * 4) Select *.winmd and *.dll file for the appropriate CPU architecture

of your project (x86 or x64 or ARM). This file is located in {OfflineODataPlugin

Directory}/windows81/bin/win/[x86 or x64 or ARM]

        * 5) Select "Windows 8.1" -> "Extensions" and select "Microsoft Visual C

++ 2013 Runtime Package for Windows (Version 12.0 or higher)".

        * 6) Click OK to add the references.

        *

        * ADD NATIVE REFERENCES FOR THE WINDOWS PHONE 8.1 PROJECT

        * 1) Open the Cordova generated solution in Visual Studio.

        * 2) Select the project generated for Windows Phone 8.1. Select the Refe

rences node, Right Click and select "Add Reference".

        * 3) Click "Browse", locate the OfflineOData plugin location on your dis

k ({OfflineODataPluginDirectory}/windows81/bin/wp)

        * 4) Select *.winmd and *.dll file for the appropriate CPU architecture

of your project (x86 or ARM). This file is located in {OfflineODataPluginDirecto

ry}/windows81/bin/wp/[x86 or ARM]

        * 5) Select "Windows 8.1" -> "Extensions" and select "Microsoft Visual C

++ 2013 Runtime Package for Windows Phone (Version 12.0 or higher)".

        * 6) Click OK to add the references.

        **********************************************************

However, Visual Studio will not allow me to add any .dll files to my project, it only accepts the .winmd ones. I am trying to solve this and will let you know if it helps.

0 Kudos

Hi Midhun ,

Even i am facing the same issue as others in this chain.But the solutions provided are not working for me.

I have referred this link http://scn.sap.com/docs/DOC-65397

The below lines are not executing.

store = sap.OData.createOfflineStore(properties);

store.open(openStoreSuccessCallback, errorCallback);

Please provide the solution.

Thanks in advance

midhun_vp
Active Contributor
0 Kudos

Hi Bibisumayya,

Please create a new discussion with the complete details of the error you are facing and the steps you followed.

Regards, Midhun

SAP Technology RIG

Former Member
0 Kudos

Yes mithun I am able to open the offline store successfully.

I am able to enter in to the read function when i am offline but I get the error HTTP REQUEST FAILED in the device..How to make a request to the localDB could you please help me out...

midhun_vp
Active Contributor
0 Kudos

Hi Abhinav,

You should do something like below:

function read() {

        updateStatus2("read request started");

        startTime = new Date();

        clearTable();

        if (!haveAppId()) {

            return;

        }

        var sURL = applicationContext.applicationEndpointURL + "/DateFormats";

  console.log("Date Formats Fetched"+  sURL);

        var oHeaders = {};

        oHeaders['Authorization'] = authStr;

  oHeaders['X-SMP-APPCID'] = applicationContext.applicationConnectionId;    //this header is provided by the logon plugin

  oHeaders['Content-Type'] = "application/xml";

        oHeaders['X-CSRF-Token'] = "FETCH";

      

        var request = {

            headers : oHeaders,

            requestUri : sURL,

            method : "GET"

        };

        console.log("read using " + sURL);

        OData.read(request, readSuccessCallback, errorCallback);

  }

if(navigator.onLine){

     read();

}

  else

{

  sap.OData.applyHttpClient();

  read();

  }

   

Regards, Midhun

SAP Technology RIG

midhun_vp
Active Contributor
0 Kudos

Hi Abhinav,

Provide more information on the issue -  what you are trying to do, steps you followed.

Provide the code you used for offline. Hope you followed this blog to implement offline - scn.sap.com/docs/DOC-58063

In hybrid app when you do a request it will not do an actual HTTP request, the error says it's actually doing an HTTP request and it failed.

Regards, Midhun

SAP Technology RIG

Former Member
0 Kudos

Yeah for sure mithun,

function read() {

  if(navigator.onLine){

  alert("hi");

        updateStatus2("read request started");

        startTime = new Date();

        clearTable();

        if (!haveAppId()) {

            return;

        }

        var sURL = applicationContext.applicationEndpointURL + "/DateFormats";

  console.log("Date Formats Fetched"+  sURL);

        var oHeaders = {};

        oHeaders['Authorization'] = authStr;

  oHeaders['X-SMP-APPCID'] = applicationContext.applicationConnectionId;    //this header is provided by the logon plugin

  oHeaders['Content-Type'] = "application/xml";

        oHeaders['X-CSRF-Token'] = "FETCH";

       

        var request = {

            headers : oHeaders,

            requestUri : sURL,

            method : "GET"

        };

        console.log("read using " + sURL);

        OData.read(request, readSuccessCallback, errorCallback);

  }

  else{

  sap.OData.applyHttpClient();

  //OData.read(request,readSuccessCallback,errorCallback);

  }

    }

    function readSuccessCallback(data, response) {

        var endTime = new Date();

        var duration = (endTime - startTime)/1000;

        updateStatus2("Read " + data.results.length + " records in " + duration + " seconds");

       

        var productsTable = document.getElementById("ProductsTable");

        for (var i = 0; i < data.results.length; i++) {

            var row = productsTable.insertRow(1);

            var cell1 = row.insertCell(0);

            var cell2 = row.insertCell(1);

          

            cell1.innerHTML = data.results[i].DateFormatType;

            cell2.innerHTML = data.results[i].DateFormatValue;

     

        }

    }

the above is my read() function in which the data is read.

I am also attaching my openStore() method:

if (!haveAppId()) {

            return;

        }

        startTime = new Date();

        updateStatus2("store.open called");

        var properties = {

            "name": "DateFormatsOfflineStore",

            "host": applicationContext.registrationContext.serverHost,

            "port": applicationContext.registrationContext.serverPort,

            "https": applicationContext.registrationContext.https,

            "serviceRoot" :  appId,

  "streamParams" : "custom_header=Authorization:Basic " + btoa(applicationContext.registrationContext.user + ":" + applicationContext.registrationContext.password) + ";custom_header=X-SMP-APPCID:" +  applicationContext.applicationConnectionId + ";",

            "definingRequests" : {

                "DateFormats" : "/DateFormats"

            }

        };

        store = sap.OData.createOfflineStore(properties);

  console.log("Offline Store Created");

        //var options = {};

        store.open(openStoreSuccessCallback, errorCallback/*, options*/);

  console.log("Offline Store Opened");

    }

When the device is in online , data is read absolutely fine where as in offline i am unable to read data and Http request failed error is getting displayed.....

Mithun Please help us in this on high priority

midhun_vp
Active Contributor
0 Kudos

Hi Abhinav,

Are you able to successfully open the offline store ?

After applying custom HTTP client you have to make a request. Which will make a query to LocalDB (UltraliteDB).

You could debug your code using Chrome to find where it fails: http://gonzalo123.com/2014/08/04/debugging-android-cordovaphonegap-apps-with-chrome/

Regards, Midhun

SAP Technology RIG

Former Member
0 Kudos

Hello Midhun,

I am encountering the exact same problem as abhinav described in his post.

I have been following the Kapsel offline OData tutorial and have been unable to successfully execute an OData.read request when the device is offline.

This is my code :


function openStore() { //called when the register() method succeeds

        console.log("store.open called");

        if (!haveAppId()) {

            return;

        }

        var properties = {

            "name": "NorthwindService",

            "host": applicationContext.registrationContext.serverHost,

            "port": applicationContext.registrationContext.serverPort,

            "https": applicationContext.registrationContext.https,

            "serviceRoot": appId, 

            "definingRequests": {

                "Products": "/Products"

            }

        };

        store = sap.OData.createOfflineStore(properties);

        store.open(openStoreSuccessCallback, errorCallback);

}

function openStoreSuccessCallback() {

        console.log("Store is OPEN.");

        testOfflineRetrieval();

}

function testOfflineRetrieval() {

     /*

    //option 1

    var oHeaders = {};

    oHeaders["Authorization"] = "Basic " + btoa(applicationContext.registrationContext.user + ":" + applicationContext.registrationContext.password);

    var request = {

     requestUri: applicationContext.applicationEndpointURL + "/Products",

        method: "GET",

        headers: oHeaders

    };

    OData.read(request,

        function (oEvent) {

            console.log("Came, saw, conquered");

        }, function (err) {

            console.log("came, saw and... failed.");

        }

        );

*/

    /*

    //option 2

    var uri = applicationContext.applicationEndpointURL;

    var user = applicationContext.registrationContext.user;

    var password = applicationContext.registrationContext.password;

    var headers = { "X-SMP-APPCID": applicationContext.applicationConnectionId };

    var oModel = new sap.ui.model.odata.ODataModel(uri, {

        json: "true",

        user: user,

        password: password,

        headers: headers

    });

       

    sap.ui.getCore().setModel(oModel);

    oModel.read("/CompanyLocations", {

        async: false,

        success: function (oEvent) {

            console.log("getdata1 successful");

            var msg = new Windows.UI.Popups.MessageDialog("success");

            msg.showAsync();

        },

        error: function (err) {

            console.log("you have failed");

            var msg = new Windows.UI.Popups.MessageDialog("fail");

            msg.showAsync();

        }

    });

*/

//option 3

   if (!navigator.onLine)

       sap.OData.applyHttpClient();

       var sURL = applicationContext.applicationEndpointURL + "/Products";

       var oHeaders = {};

       oHeaders['Authorization'] = authStr;

       oHeaders['X-SMP-APPCID'] = applicationContext.applicationConnectionId;

       oHeaders['Content-Type'] = "application/json";

       oHeaders['X-CSRF-Token'] = "FETCH";

       var request = {

           headers: oHeaders,

           requestUri: sURL,

           method: "GET"

        };

    OData.read(request,

        function (data, response) {

            console.log('success');

            var msg = new Windows.UI.Popups.MessageDialog("success");

            msg.showAsync();

        },

        function (err) {

            console.log('fail');

            var msg = new Windows.UI.Popups.MessageDialog("fail: " + err.message);

            msg.showAsync();

        }

    );

}

    }

When I execute this, the openStore() method is successful (I didn't bother to include the errorCallback() method) and leads to the testOfflineRetrieval method, where I try in different ways to read the data from the store.

No matter which of the three different options in testOfflineRetrieval() I choose, the behaviour of the application is the same : the OData.read() or oModel.read() method will succeed if the device is online, and fail with message "HTTP request failed" if the device is offline.

My application connects to my HCP Mobile Services application which retrieves the data from the backend system (whether it be my web application or the Northwind services).

I came across this line in the Kapsel tutorial :


Note, there is a known issue with this example and the SP06 SMP server due to a problem with redirects.  BCP Issue 1580013675.  The read request will fail.  This problem is not present in the SP05 server and  is addressed in SP06 PL02 and SP07 server versions.

Could the HCP MS behave the same way as the SP06 SMP server? I can't figure what the problem can be here.

Thanks,

Christopher.

midhun_vp
Active Contributor
0 Kudos

Hi Christopher,

HCPMS behave same as SMP 3 do.

Is this working when tested in online mode?

Regards, Midhun

SAP Technology RIG

Former Member
0 Kudos

Yes, in online mode everything works correctly.

I am using Cordova and Visual Studio for this project. Cordova generates a VS project, and I then use VS to run the app. Once the app is open and the offline store is created, I pin the app to the taskbar so as to be able to open the same instance of the app in offline mode. I am pretty confident this works and that the same store is kept from one time to the next.

I am double checking the OData references I use in the project, I will let you know if any luck comes from that side.