2014-02-10 109 views
0

我有一個MVC4 Web API應用程序,其中我有我的Api控制器和Code-First EF5數據庫以及一些JavaScript函數,用於我的應用程序的功能,包括我的Web Api的Ajax調用Service.I在MVC上完成了這個項目,因爲我在VS2012中安裝Cordova時遇到了麻煩,所以我決定使用Eclipse/Android Phonegap platform.Is有一種方法可以調用我的web api服務並能夠檢索我的數據庫數據在我的Android Phonegap應用程序中設計了EF5(MVC4),而無需再次從頭開始。我知道phonegap基本上是Html(JavaScript和Css),但我無法使用與使用MVC4相同的HTML標記調用我的服務。我是一名初學者,請告訴我,如果我正在做的事情是可能的,如果沒有,請告訴我如何去做這件事。 T * 他是我的HTML代碼 *將MVC4 Web API應用程序轉換爲Phonegap Android應用程序

<script type="text/javascript" charset="utf-8" src="phonegap-2.9.0.js"></script> 
<script type="text/javascript" charset="utf-8" src="barcodescanner.js"></script> 
<script type="text/javascript" language="javascript" src="http://api.afrigis.co.za/loadjsapi/?key=...&version=2.6"> 
</script> 
<script type="text/javascript" language="javascript"> 


// Wait for device API libraries to load 
// 
document.addEventListener("deviceready", onDeviceReady, false); 

//initialize watchID Variable 

var watchID = null; 
// device APIs are available 

function onDeviceReady() { 
    // Throw an error if no update is received every 30 seconds 
    var options = { timeout: 30000 }; 
    watchID = navigator.geolocation.watchPosition(onSuccess, onError, options); 

    // onSuccess Geolocation 
// 
function onSuccess(position) { 
    var element = document.getElementById('geolocation'); 
    element.innerHTML = 'Latitude: ' + position.coords.latitude  + '<br />' + 
         'Longitude: ' + position.coords.longitude  + '<br />' + 
         '<hr />'  + element.innerHTML; 
} 

    // onError Callback receives a PositionError object 
    // 
    function onError(error) { 
     alert('code: ' + error.code + '\n' + 
       'message: ' + error.message + '\n'); 
    } 

} 
    //declare a global map object 
    var agmap = null; 

    // declare zoom control of map 
    var zoomCtrl = null; 
    function initAGMap() { 

     agmap = new AGMap(document.getElementById("MapPanel")); 
     //TODO: must retrieve coords by device location not hard corded. 

     agmap.centreAndScale(new AGCoord(-25.7482681540537, 28.225935184269), 5); // zoom level 5 heres 
     // making zoom controls for map 
     var ctrlPos = new AGControlPosition(new AGPoint(10, 10), AGAnchor.TOP_LEFT); 
     zoomCtrl = new AGZoomControl(1); 
     agmap.addControl(zoomCtrl, ctrlPos); 
    } 

    function removeZoomCtrl() 
    { 
     zoomCtrl.remove(); 
    } 

    //function search() { 
    // var lat = $('#latitude').val(); 
    // var long = $('#longitude').val(); 

    // $.ajax({ 
    //  url: "api/Attractions/?longitude=" + long + "&latitude=" + lat, 
    //  type: "GET", 
    //  success: function (data) { 
    //   if (data == null) { 
    //    $('#attractionName').html("No attractions to search"); 
    //   } 
    //   else { 
    //    $('#attractionName').html("You should visit " + data.Name); 
    //    displayMap(data.Location.Geography.WellKnownText, data.Name); 
    //   } 

    //  } 

    // }); 

    //} 

    //function GetCoordinate() { 
     //todo: get details from cordova, currently mocking up results 
     //return { latitude: -25.5, longitude: 28.5 }; 
    } 
    function ShowCoordinate(coords) { 
     agmap.centreAndScale(new AGCoord(coords.latitude, coords.longitude), 5); // zoom level 5 here 
     var coord = new AGCoord(coords.latitude, coords.longitude); 
     var oMarker = new AGMarker(coord); 
     agmap.addOverlay(oMarker); 
     oMarker.show(); 

     //todo: create a list of places found and display with marker on AfriGIS Map. 
    } 

    function ScanProduct() 
    { 
     //todo retrieve id from cordova as mockup 
     //This is mockup barcode 
     //return "1234"; 
     //sample code using cordova barcodescanner plugin 
        var scanner = cordova.require("cordova/plugin/BarcodeScanner"); 
        scanner.scan(
        function (result) { 
        alert("We got a barcode\n" + 
        "Result: " + result.text + "\n" + 
        "Format: " + result.format + "\n" + 
        "Cancelled: " + result.cancelled); 
      }, 

     //Callback function if barcodedont exist 

      function (error) { 
       alert("Scanning failed: " + error); 
      }); 
    } 
    //Function to display Success or error in encoding. 

    function encode(type, data) { 
     window.plugins.barcodeScanner.encode(type, data, function(result) { 
       alert("encode success: " + result); 
      }, function(error) { 
       alert("encoding failed: " + error); 
      });} 



    function GetProductDetails(barcodeId,coords) 
    { 
     //Ajax Call to my web Api service 
     $.getJSON("api/products/?barcodeId=" + barcodeId + "&latitude=" + coords.latitude + "&longitude=" + coords.longitude) 
      .done(function (data) { 
       $('#result').append(data.message) 
       console.log(data) 
       var list = $("#result").append('<ul></ul>').find('ul'); 
       $.each(data.results, function (i, item) 
       { 
        if (data.results == null) { 

         $('#result').append(data.message) 
        } 

        else { 


         list.append('<li>ShopName :' + item.retailerName + '</li>'); 
         list.append('<li>Name : ' + item.productName + '</li>'); 
         list.append('<li>Rand :' + item.price + '</li>'); 
         list.append('<li>Distance in Km :' + item.Distance + '</li>'); 

         //Another Solution 

         //var ul = $("<ul></ul>") 
         //ul.append("<li> Rand" + data.results.productName + "</li>"); 
         //ul.append("<li> Rand" + data.results.Retailer.Name + "</li>"); 
         //ul.append("<li> Rand" + data.results.price + "</li>"); 
         //ul.append("<li> Rand" + data.results.Distance + "</li>"); 
         //$("#result").append(ul); 
        } 

       }); 

       $("#result").append(ul); 

      }); 
     } 

    function ShowProductDetails() 
    { 
     //todo: display product details 
     //return productdetails.barcodeId + productdetails.retailerName + ': R' + productdetails.Price + productdetails.Distance; 
    } 
     //loading javascript api 
     $(function() { 
      initAGMap(); 

      var coord = GetCoordinate(); 
      ShowCoordinate(coord); 
      var barcodeId = ScanProduct(); 
      var productdetails = GetProductDetails(barcodeId, coord); 
      ShowProductDetails(productdetails); 
     }); 
</script> 

+0

你有什麼樣的麻煩?你可以發佈一些代碼嗎? –

+0

@Anthony我編輯和粘貼我的HTML代碼,我有我的Ajax,調用我的Web API服務。 –

回答

0

看起來你是在正確的軌道上。現在顯而易見的錯誤是它使用相對URL(api/products/?barcodeId=)來調用Web API。由於HTML不再與Web API託管在同一臺服務器上(即使您可能仍在本地計算機上運行它們),這將不再適用。您需要使用絕對URL調用該服務(例如,http://localhost:8888/api/products/?barcodeId=)。

您的Web API現在在哪裏?您如何運行Cordova代碼?如果Web API在本地計算機上運行並且您的Cordova應用程序在同一臺計算機上的仿真器上運行,則應該能夠通過提供完整的本地主機路徑來調用該服務。

如果它仍然無法正常工作,您需要以某種方式調試代碼並查看錯誤是什麼。

+0

謝謝你,我嘗試過這樣做,雖然經過數週的努力,但它工作的魅力,但現在我無法訪問Android設備上的網站(摩托羅拉剃鬚刀或三星S4),我可以在任何機器從Firefox,鉻甚至IE瀏覽器,但我只能得到一個地圖時,在Android瀏覽器上點擊該網站時,我看不到產品詳細信息(我的結果)我在我的電話中使用getJson,所以我認爲這個問題可能是我稱爲阿賈克斯呼叫所以移動瀏覽器不喜歡我呼籲api請求幫助解決這個問題,我怎麼能得到這個工作在移動 –

+0

$ .getJSON (「api/products /?barcodeId = 」+ barcodeId +「&緯度=「+ coords.latitude + 」&longitude =「+ coords.longitude) .done(function(data) {('#re sult')。append(data.message) console.log(data) –

相關問題