1

I am new to worklight. I tried to get the json response from the http adapter, but I am unable to get it to display on device. I added some alerts in my javascript code and found that it is returning the size of json object as `undefined``.

Here is my adapter javascript file:

function getGooglePlaces(location,name) {

    var input = {
        method : 'get',
        returnedContentType : 'json',
        path : 'maps/api/place/search/json',
        headers: {
            Host: 'maps.googleapis.com'
        },
        parameters : {
            'key'       :   MyKey,
            'location'  :  location,
            'radius'    :   '10000',
            'sensor'    :   'false',
            'name'      :  name 
        }
    };

    var response = WL.Server.invokeHttp(input);

    return response;

}

function addGooglePlace(param1) {

    var input = {
        method : 'put',
        returnedContentType : 'json',
        path : 'userInputRequired'
    };

    return WL.Server.invokeHttp(input);
}

My main.js file:

function getLocation()
{

    if (navigator.geolocation)
    {
        navigator.geolocation.getCurrentPosition(loadHTTPRecords);
    }
    else{x.innerHTML="Geolocation is not supported by this browser.";}
}

function loadHTTPRecords(position){

    var invocationData = {
        adapter : 'GooglePlaces',
        procedure : 'getGooglePlaces',
        parameters : [position.coords.latitude+','+position.coords.longitude,'dead battery']
    };

    WL.Client.invokeProcedure(invocationData,{
        onSuccess : loadHTTPQuerySuccess,
        onFailure : loadHTTPQueryFailure
    });

}

function loadHTTPQuerySuccess(result){

    WL.Logger.debug("Retrieve success" +  JSON.stringify(result));
    displayFeeds(result);

}

function loadHTTPQueryFailure(result){

    WL.Logger.error("Retrieve failure");
}

function displayFeeds(items){
    alert("In displayFeeds");

    // Get the size of an object

    var ul = $('#itemsList');
    alert("before for loop");

    alert(items.size);
    for (var i = 0; i < items.size; i++) {
        alert("inside for loop 1");
        for(var j=0;j<i;j++){
            alert("in for loop 2");
            var li = $('<li/>').html(items[i].name);

            li.append($('<hr>'));
            ul.append(li);
        }
    }
}

Please suggest what I am doing wrong.

my sample json response

{
 "html_attributions": [
 ],
  "isSuccessful": true,
  "responseHeaders": {
   "Alternate-Protocol": "443:quic",
   "Cache-Control": "public, max-age=300",
   "Content-Type": "application\/json; charset=UTF-8",
   "Date": "Tue, 11 Feb 2014 12:04:13 GMT",
   "Expires": "Tue, 11 Feb 2014 12:09:13 GMT",
   "Server": "mafe",
   "Transfer-Encoding": "chunked",
   "Vary": "Accept-Language",
   "X-Frame-Options": "SAMEORIGIN",
    "X-XSS-Protection": "1; mode=block"
 },
   "responseTime": 236,
  "results": [
      {
       "geometry": {
        "location": {
           "lat": 52.057049,
           "lng": 1.153298
        }
     },
     "icon": "http:\/\/maps.gstatic.com\/mapfiles\/place_api\/icons\/cafe-71.png",
     "id": "ec0955fb06fd95d639c89d12475624627250abac",
     "name": "Costa Coffee",
     "opening_hours": {
        "open_now": true
     },
     "price_level": 2,
     "rating": 3.9,
     "reference": "CnRuAAAABmdY6kIxRQZw68hqjZ_wwBE29sdSgYuOkXf2IvZTe77aG-AgoCaahu1c9cddHA0Z1D2EdELAEuDyl38xV1G5YcvP3pOm2p0IwVkuvYIJSA1IKAGLIQym21SpXvhUSqBxrpHKBvgTNnUg69lHROaMyxIQvvP8SeCG_dzKi_JgrdrgRRoUQXqH4UkDtA-58bCbdRzUCdXTRVU",
     "types": [
        "cafe",
        "food",
        "establishment"
     ],
     "vicinity": "1-5 Queen St, Ipswich"
  },
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ravi
  • 35
  • 1
  • 10
  • do you get the response while invoking the adapter procedure using **Invoke Worklight Procedure** – Kawinesh S K Feb 11 '14 at 04:54
  • Yes i got response when i invoke procedure but the problem is when i try to access an array results,it says 'undefined' – ravi Feb 11 '14 at 08:47
  • can you provide the sample result in your question because i think you are traversing the json in a wrong way. – Kawinesh S K Feb 11 '14 at 08:58

1 Answers1

2

If i understood you question properly, through items.size you are trying to get the length of results. If you want to get the length of results you should use items.invocationResult.results.length which will give you the total number of results, where items is the response coming from the adapter and invocationResult contains the results and other paramaters, from which you will have to access the results for accessing only the particular output.

If i didn't understand your question properly, please tell me exactly what you are trying to get through items.size

Idan Adar
  • 44,156
  • 13
  • 50
  • 89
sasi
  • 512
  • 4
  • 27
  • i have a question ,should i use JSON.stringify to convert json object into string and then use items.invocationResult.results – ravi Feb 11 '14 at 12:36
  • Yes you can use. Both will give you the same result, but if you want to access the result length, you can directly use item.invocationResult.results. – sasi Feb 11 '14 at 12:57
  • Thanks sasi,i got it and one more doubt.in the same response i want to access price_level of each result,but when i run it it says undefined – ravi Feb 11 '14 at 13:01
  • If you notice your response, the results is in array format, so if you want to access the price_level, you have mention in which array index you are accessing, for example for this output, you can access like, items.invocationResult.results[0].price_level – sasi Feb 11 '14 at 13:03
  • i tried that and writen a console.log to see what it prints,i got 'undefiened' for price_level – ravi Feb 11 '14 at 13:14