How Integrate Google app endpoints with a phonegap

2019-04-17 18:44发布

I have been developing a Phonegap client application and I create all the web services using google endpoints.

But i'm having a problem using the api. In my index.html i have this script

<head><script>
    var myapi;
    function initGoogleApis() {
        var ROOT = "https://myapitest.appspot.com/_ah/api";
        gapi.client.load("myapitest", "v1", function() {
            myapi = gapi.client.ratemyday;
        }, ROOT); 
    }
</script></head>
<script src="https://apis.google.com/js/client.js?onload=initGoogleApis"></script>

What I'm trying to do i'ts make the variable myapi global.

In another js file, i want to use myapi in the phonegap ondeviceready function , i'ts something like this

 document.addEventListener("deviceready", onDeviceReady, false);

    function onDeviceReady() {
        myapi.items.insert({
            'id' : 4,
            'name' : 'item',
        }).execute(function(resp) {
            console.log(resp);
        });
    }

The problem is that i'ts not working, i't seams like myapi is unknown What i'm doing wrong? How can i use my enpoints api using phonegap

2条回答
祖国的老花朵
2楼-- · 2019-04-17 18:47

I solve the problem!

It was a scope variable problem, in addition the phonegap onDeviceReady function must be call in the init function.

This work for me:

     <head><script>
        function initGoogleApis() {
            var ROOT = "https://myapitest.appspot.com/_ah/api";
            gapi.client.load("myapitest", "v1", function() {

            document.addEventListener("deviceready", onDeviceReady, false);

            }, ROOT); 
        }
    </script></head>

<script src="https://apis.google.com/js/client.js?onload=initGoogleApis"></script>
查看更多
Viruses.
3楼-- · 2019-04-17 18:49

it certainly looks like a scope variable problem. but I am not 100% sure about it. probably i will check and will let you know.

instead of doing

myapi.items.insert()

use

gapi.client.ratemyday.items.insert()

I am pretty sure this should work. In our application where we are consuming 50+ endpoint api calls using pretty much same way.

查看更多
登录 后发表回答