Getting response from HTTP Adapter from Worklight

青春壹個敷衍的年華 提交于 2019-12-24 10:58:04

问题


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"
  },

回答1:


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



来源:https://stackoverflow.com/questions/21677311/getting-response-from-http-adapter-from-worklight

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!