从Worklight获取HTTP适配器的响应

时间:2014-02-10 12:30:32

标签: javascript ibm-mobilefirst worklight-adapters

我是新手工作灯。我试图从http适配器获取json响应, 但我无法让它显示在设备上。 我在我的javascript代码中添加了一些警报,发现它将json对象的大小返回为`undefined``。

这是我的适配器javascript文件:

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);
}

我的main.js文件:

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);
        }
    }
}

请建议我做错了什么。

我的样本json回复

{
 "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 个答案:

答案 0 :(得分:2)

如果我理解你的问题,通过items.size你试图获得结果的长度。如果您想获得结果的长度,您应该使用items.invocationResult.results.length,它将为您提供结果总数,其中项目是来自适配器的响应,invocationResult包含结果和其他参数,来自您必须访问结果才能访问特定输出。

如果我没有正确理解您的问题,请告诉我您正在尝试通过items.size