SamMan SamMan - 1 month ago 15
Java Question

Calling app engine API's in a phonegap application

I am looking to execute an API method in my phonegap application to retrive all the entries from the datastore. I have been testing this on a samsung S4 and can't seem to get any errors or anything in the console and the logs for the datastore indicate that no attempts have been made to retrieve data from the datastore.

I will post my base.js file below which is the function that is suppose the execute the API call.

I have been following this guide created by Google to implement this. The method is for POST rather than GET as according to another tutorial i was following, you must request the data through a post method rather than a get method.

var requestData = {};

google.appengine.CMS.client.getCalls = function() {
gapi.client.call.queryCalls(requestData).execute(function(resp) {
if (!resp.code) {
resp.calls = resp.calls || [];
var result = "";
for (var i=0;i<resp.calls.length;i++) {
result = result+ resp.calls[i].callId+ " : " + resp.calls[i].patientName+ " : " + resp.calls[i].doctor+ " : " + "<br/>" + resp.calls[i].address1+ " : " + resp.calls[i].address2+ " : " + resp.calls[i].address3+ " : " + "<br/>" + resp.calls[i].postCode + " : " + resp.calls[i].patientLocation+ " : " + resp.calls[i].symptoms+ " : " + "<br/>" + resp.calls[i].contactNumber+ " : " + resp.calls[i].callDateTime+ " : " + resp.calls[i].currentStatus+ "<br/>";
}
return result;
google.appengine.CMS.client.print(resp);
}
});
};


/**
* Enables the button callbacks in the UI.
*/
google.appengine.CMS.client.enableButtons = function() {
var ListCalls = document.querySelector('#listCalls');
ListCalls.addEventListener('click', google.appengine.CMS.client.getCalls);
};

/**
* Initializes the application.
* @param {string} apiRoot Root of the API's path.
*/
var apiRoot = 'generic root';
google.appengine.CMS.client.init = function(apiRoot) {
// Loads the OAuth and helloworld APIs asynchronously, and triggers login
// when they have completed.
var apisToLoad;
var callback = function() {
if (--apisToLoad == 0) {
google.appengine.CMS.client.enableButtons();
}
};

apisToLoad = 1; // must match number of calls to gapi.client.load()
gapi.client.load('call', 'v1', callback, apiRoot);
};

Answer

If you use a packaged phonegapp app, a webview that show localhost, you need:

  • authorize the client id for localhost origin as shown here [1]
  • use an absolute reference to the endpoint instead use the window.location.host as shown here [2]
  • or follow this part [3]

I suggest you to use a phonegap app that point directly pages on *.appspot.com instead of copy that on the app:

  • update the app without the users interaction
  • better development because you can test the app outside phonegap
  • switch appengine versions and automatically all users are up-to-date

[1] https://cloud.google.com/appengine/docs/java/endpoints/getstarted/backend/setup#creating_an_app_engine_app_for_deployment

[2] https://cloud.google.com/appengine/docs/java/endpoints/getstarted/clients/js/client_ui

instead of

<script>
        function init() {
            google.appengine.samples.hello.init('//' + window.location.host + '/_ah/api');
        }
    </script>
    <script src="https://apis.google.com/js/client.js?onload=init"></script>

use

<script>
        function init() {
            google.appengine.samples.hello.init('https://youappid.appspot.com/_ah/api');
        }
    </script>
    <script src="https://apis.google.com/js/client.js?onload=init"></script>

[3] https://cloud.google.com/appengine/docs/java/endpoints/consume_js