2013-08-26 40 views
7

我在Phonegap上編寫我的第一個應用程序,我一直在Android設備上測試它。我相信我已經批准了相應的權限:Phonegap Android互聯網不能正常工作

<uses-permission android:name="android.permission.INTERNET" /> 

但是地圖沒有顯示出來。我試圖從網絡服務獲取經緯度,並使用它在Google地圖上放置標誌。我相信這個問題實際上是通過互聯網訪問的,就像我通過Visual Studio運行這個網站一樣,服務返回座標並顯示地圖。但是,當應用程序編譯並且該網站在應用程序webview中運行時,不會返回座標,也不會顯示地圖。

我還添加了一個表格來顯示返回的座標,它們再次顯示在瀏覽器中,但在運行的應用程序中是空白的。有誰知道爲什麼webview無法連接到互聯網並返回所需的數據?我已經刪除了Web服務的實際地址,但我知道這很好,因爲它在瀏覽器中工作。

HTML頁面:

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <title></title> 
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp&amp;sensor=true"></script> 
    <link rel="stylesheet" type="text/css" href="css/index.css" /> 
    </head> 
    <body> 
    <div class="app2"> 
     <div id="map" style="width: 200px; height: 150px"></div> 
     <script type="text/javascript"> 
     var xmlhttp = new XMLHttpRequest(); 
     xmlhttp.open("GET", "http://www.xxxxxxxx.com/mobile/iphone/xml/postal.asp?postal=33173", false); 
     xmlhttp.send(); 
     xmlDoc = xmlhttp.responseXML; 
     var myOptions = { 
      zoom: 10, 
      mapTypeId: google.maps.MapTypeId.ROADMAP, 
     }; 
     var map = new google.maps.Map(document.getElementById("map"), myOptions); 
     var lat = x[i].getElementsByTagName("latitude")[0].childNodes[0].nodeValue; 
     var long = x[i].getElementsByTagName("longitude")[0].childNodes[0].nodeValue; 
     var pos = new google.maps.LatLng(lat, long); 
     var infowindow = new google.maps.InfoWindow({ 
      map: map, 
      position: pos, 
      content: "Postcode 33173(Lat:" + lat + ", Long:" + long + ")" 
     }); 
     map.setCenter(pos); 
     </script> 
     <script> 
     document.write("<table border='1'>"); 
     document.write("<tr><td>Postcode</td><td>latitude</td><td>longitude</td></tr>"); 
     var x = xmlDoc.getElementsByTagName("position"); 
     for (i = 0; i < x.length; i++) { 
      document.write("<tr><td>33173</td><td>"); 
      document.write(x[i].getElementsByTagName("latitude")[0].childNodes[0].nodeValue); 
      document.write("</td><td>"); 
      document.write(x[i].getElementsByTagName("longitude")[0].childNodes[0].nodeValue); 
      document.write("</td></tr>"); 
     } 
     document.write("</table>"); 
     </script> 
    </div> 
    </body> 
</html> 

清單:

<?xml version='1.0' encoding='utf-8'?> 
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="1.0.0" android:windowSoftInputMode="adjustPan" package="dig.phonegap.loa_app" xmlns:android="http://schemas.android.com/apk/res/android"> 
    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" /> 
    <uses-permission android:name="android.permission.CAMERA" /> 
    <uses-permission android:name="android.permission.VIBRATE" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.RECEIVE_SMS" /> 
    <uses-permission android:name="android.permission.RECORD_AUDIO" /> 
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> 
    <uses-permission android:name="android.permission.READ_CONTACTS" /> 
    <uses-permission android:name="android.permission.WRITE_CONTACTS" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
    <uses-permission android:name="android.permission.BROADCAST_STICKY" /> 
    <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="18" /> 
    <application android:allowBackup="false" 
    android:hardwareAccelerated="true" 
    android:icon="@drawable/icon" 
    android:label="@string/app_name"> 
    <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" 
    android:label="@string/app_name" 
    android:name="HelloWorld" 
    android:theme="@android:style/Theme.Black.NoTitleBar"> 
    <intent-filter> 
    <action android:name="android.intent.action.MAIN" /> 
    <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</activity> 
</application> 
</manifest> 
+1

你在config.xml上允許access-origin * –

+0

這樣做的竅門,謝謝。既然你是第一個,如果你將它添加爲答案,我會將其標記爲正確的。謝謝。 – collusionbdbh

回答

8

試試這個上​​3210 :

者平臺 - > Android的 - >水庫 - > XML - > config.xml中

者平臺 - > IOS - > config.xml中

者平臺 - > WP7 - > config.xml中

者平臺 - > WP8 - > config.xml

<access origin="*" /> 
+0

我因爲沒有從互聯網上獲取數據而自殺,我無法獲得1個月的解決方案!非常感謝!!! – rafi

3

確保您有以下在res/XML/config.xml中設置:

<access origin="*" /> 
+0

我爲了不從互聯網獲取數據而自殺,我無法獲得1個月的解決方案!非常感謝!!! – rafi

+0

我在'www/res'裏沒有名爲'xml'的文件夾。我在www文件夾裏有一個'config.xml'。我應該在那裏添加它..?如果是的話,在什麼下面..? – shashwat

6

我有同樣的問題,並且必須添加以下內容到​​3210。

<access origin="*" /> 
<plugin name="cordova-plugin-whitelist" version="1" /> 
<allow-intent href="http://*/*" /> 
<allow-intent href="https://*/*" /> 
<allow-intent href="tel:*" /> 
<allow-intent href="sms:*" /> 
<allow-intent href="mailto:*" /> 
<allow-intent href="geo:*" /> 
<platform name="android"> 
    <allow-intent href="market:*" /> 
</platform> 
<platform name="ios"> 
    <allow-intent href="itms:*" /> 
    <allow-intent href="itms-apps:*" /> 
</platform> 
+0

你能解釋爲什麼這會幫助collusionbdbh與他們的問題? –

+4

新的Cordova Android項目默認設置了白名單插件。當您升級到新的phonegap版本(和新的cordova版本)時,您的config.xml保持不變,但cordova-plugin-whitelist被添加到您的插件並拒絕互聯網訪問,因爲您沒有whiltelist設置。 –

+0

解決了我的問題!謝謝。我到處尋找解決方案,而不是隻添加