2012-02-15 7 views
5

Nel mio other question ho scoperto che non esiste una whitelist per i telefoni Windows.Soluzione alternativa per la lista bianca mancante in PhoneGap per Windows Phone

Ora sto cercando una soluzione alternativa al codice nativo, ma non ho mai scritto una riga di codice nativo per i telefoni Windows. Quindi non è facile per me. Penso di poter scaricare una pagina come questa:

void GetAirportData() 
{ 
    var url = new Uri("http://server.example.com/data.php", UriKind.Absolute); 
    var webClient = new WebClient(); 
    webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted); 
    webClient.OpenReadAsync(url, url); 
} 

Ma come può un ottenere questi dati per la mia app javascript?

risposta

11

Ecco una soluzione. Il seguente codice è un comando PhoneGap che implementa la funzionalità Cross Domain Call.

using System; 
using System.IO; 
using System.Net; 
using System.Runtime.Serialization; 
using WP7CordovaClassLib.Cordova; 
using WP7CordovaClassLib.Cordova.Commands; 
using WP7CordovaClassLib.Cordova.JSON; 

namespace Cordova.Extension.Commands //namespace is predefined, don't change it! 
{ 
    public class Cdc : BaseCommand //Cross domain call 
    { 
     [DataContract] 
     public class CdcOptions 
     { 
      [DataMember(Name = "path")] 
      public string Path { get; set; } 
     } 

     public void Call(string args) 
     { 
      CdcOptions options = JsonHelper.Deserialize<CdcOptions>(args); 

      var url = new Uri(options.Path, UriKind.Absolute); 

      var webClient = new WebClient(); 

      webClient.OpenReadCompleted += (s, e) => 
      { 
       if (e.Error != null) 
       { 
        DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Error")); 
        return; 
       } 

       //Stream -> string 
       var sr = new StreamReader(e.Result); 
       var result = sr.ReadToEnd(); 

       DispatchCommandResult(
        new PluginResult(PluginResult.Status.OK, result)); 
      }; 

      webClient.OpenReadAsync(url, url); 

     } 
    } 
} 

test sul lato client:


 <script type="text/javascript"> 

      function cdc(path, success, fail) { 

       PhoneGap.exec(
          success, //success 
          fail, //fail 
          "Cdc", //service 
          "Call", //action 
          path //args 
          ); 
      }; 

      function onDeviceReady(e) { 

       cdc(
        { 
         path: "http://stackoverflow.com/questions/9291809/workaround-for-missing-whitelist-in-phonegap-for-windows-phone" 
        }, 
        function (arg) { 
         document.getElementById('test').innerHTML = arg; 
        }, function (arg) { 
         document.getElementById('test').innerHTML = arg; 
        }); 

      } 

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


     </script> 
    </head> 
    <body> 
     <div id="test"></div> 
    </body> 
</html> 
+0

+50! In Visual Studio ho aggiunto un nuovo file di classe 'Cdc.cs' e inserito il primo blocco di codice. Quindi ho incluso phonegap.js nel mio index.html e aggiunto il secondo blocco di codice. Ha iniziato l'app e ha funzionato perfettamente! Molte grazie. Non avrei potuto farlo senza di te! – PiTheNumber

+0

Si prega di votare questa risposta. Lui lo merita pienamente! – PiTheNumber

Problemi correlati