English 中文(简体)
• 如何利用与json的电话来纠正这一方案
原标题:How to rectify this program using phonegap with json
  • 时间:2012-01-12 09:46:58
  •  标签:
  • ajax
  • cordova

I am using phonegap with json .here i got a error
index/html

<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />

    <script src="phonegap-1.2.0.js"></script>
    <script src="js/jquery-1.5.min.js"></script>
    <script src="js/load-json.js"></script>

    <script>
        function onBodyLoad() {     
            document.addEventListener("deviceready",onDeviceReady,false);
        }


    </script>

</head>
<body onload="onBodyLoad()">
    <div id="output"></div>
</body>
</html>

Json.js



$(document).ready(function(){
    $(document).bind( deviceready , function(){
        var output = $( #output );

        $.ajax({
            url:  http:192.168.1.3:8000/index ,
            dataType:  jsonp ,
            jsonp:  jsoncallback ,
            timeout: 5000,
            success: function(data, status){
                $.each(data, function(i,item){ 
                    var landmark =  <h1> +item.name+ </h1> 
                    +  <p> +item.latitude+ <br> 
                    + item.longitude+ </p> ;

                    output.append(landmark);
                });
            },
            error: function(){
               output.text( There was an error loading the data. );
            }
        });
    });
});



 01-12 15:08:52.050: I/System.out(338): loadUrl(file:///android_asset/www/index.html)
    01-12 15:08:52.050: I/System.out(338): url=file:///android_asset/www/index.html baseUrl=file:///android_asset/www
    01-12 15:08:52.230: W/System.err(338): java.net.SocketException: Permission denied
    01-12 15:08:52.230: W/System.err(338):  at org.apache.harmony.luni.platform.OSNetworkSystem.socket(Native Method)
    01-12 15:08:52.230: W/System.err(338):  at dalvik.system.BlockGuard$WrappedNetworkSystem.socket(BlockGuard.java:335)
    01-12 15:08:52.230: W/System.err(338):  at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:216)
    01-12 15:08:52.230: W/System.err(338):  at org.apache.harmony.luni.net.PlainServerSocketImpl.create(PlainServerSocketImpl.java:40)
    01-12 15:08:52.240: W/System.err(338):  at java.net.ServerSocket.<init>(ServerSocket.java:120)
    01-12 15:08:52.240: W/System.err(338):  at java.net.ServerSocket.<init>(ServerSocket.java:74)
    01-12 15:08:52.240: W/System.err(338):  at com.phonegap.CallbackServer.run(CallbackServer.java:177)
    01-12 15:08:52.240: W/System.err(338):  at java.lang.Thread.run(Thread.java:1019)
    01-12 15:08:53.299: D/szipinf(338): Initializing inflate state
    01-12 15:08:53.299: D/szipinf(338): Initializing zlib to inflate
    01-12 15:08:53.349: D/szipinf(338): Initializing inflate state
    01-12 15:08:53.349: D/szipinf(338): Initializing zlib to inflate
    01-12 15:08:54.819: D/PhoneGapLog(338): file:///android_asset/www/index.html: Line 13 : ReferenceError: Can t find variable: onDeviceReady
    01-12 15:08:54.819: E/Web Console(338): ReferenceError: Can t find variable: onDeviceReady at file:///android_asset/www/index.html:13
    01-12 15:09:15.029: D/dalvikvm(338): GC_CONCURRENT freed 361K, 52% free 2806K/5767K, external 884K/1038K, paused 6ms+4ms
    01-12 15:09:45.860: I/System.out(377): loadUrl(file:///android_asset/www/index.html)
    01-12 15:09:45.909: I/System.out(377): url=file:///android_asset/www/index.html baseUrl=file:///android_asset/www
    01-12 15:09:46.299: W/System.err(377): java.net.SocketException: Permission denied
    01-12 15:09:46.299: W/System.err(377):  at org.apache.harmony.luni.platform.OSNetworkSystem.socket(Native Method)
    01-12 15:09:46.299: W/System.err(377):  at dalvik.system.BlockGuard$WrappedNetworkSystem.socket(BlockGuard.java:335)
    01-12 15:09:46.299: W/System.err(377):  at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:216)
    01-12 15:09:46.299: W/System.err(377):  at org.apache.harmony.luni.net.PlainServerSocketImpl.create(PlainServerSocketImpl.java:40)
    01-12 15:09:46.299: W/System.err(377):  at java.net.ServerSocket.<init>(ServerSocket.java:120)
    01-12 15:09:46.299: W/System.err(377):  at java.net.ServerSocket.<init>(ServerSocket.java:74)
    01-12 15:09:46.299: W/System.err(377):  at com.phonegap.CallbackServer.run(CallbackServer.java:177)
    01-12 15:09:46.299: W/System.err(377):  at java.lang.Thread.run(Thread.java:1019)
    01-12 15:09:46.849: D/szipinf(377): Initializing inflate state
    01-12 15:09:46.849: D/szipinf(377): Initializing zlib to inflate
    01-12 15:09:46.869: D/szipinf(377): Initializing inflate state
    01-12 15:09:46.869: D/szipinf(377): Initializing zlib to inflate
    01-12 15:09:48.100: D/PhoneGapLog(377): file:///android_asset/www/index.html: Line 13 : ReferenceError: Can t find variable: onDeviceReady
    01-12 15:09:48.100: E/Web Console(377): ReferenceError: Can t find variable: onDeviceReady at file:///android_asset/www/index.html:13
    01-12 15:10:10.170: I/System.out(416): loadUrl(file:///android_asset/www/index.html)
    01-12 15:10:10.249: I/System.out(416): url=file:///android_asset/www/index.html baseUrl=file:///android_asset/www
    01-12 15:10:10.559: W/System.err(416): java.net.SocketException: Permission denied
    01-12 15:10:10.559: W/System.err(416):  at org.apache.harmony.luni.platform.OSNetworkSystem.socket(Native Method)
    01-12 15:10:10.559: W/System.err(416):  at dalvik.system.BlockGuard$WrappedNetworkSystem.socket(BlockGuard.java:335)
    01-12 15:10:10.569: W/System.err(416):  at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:216)
    01-12 15:10:10.569: W/System.err(416):  at org.apache.harmony.luni.net.PlainServerSocketImpl.create(PlainServerSocketImpl.java:40)
    01-12 15:10:10.569: W/System.err(416):  at java.net.ServerSocket.<init>(ServerSocket.java:120)
    01-12 15:10:10.569: W/System.err(416):  at java.net.ServerSocket.<init>(ServerSocket.java:74)
    01-12 15:10:10.569: W/System.err(416):  at com.phonegap.CallbackServer.run(CallbackServer.java:177)
    01-12 15:10:10.569: W/System.err(416):  at java.lang.Thread.run(Thread.java:1019)
    01-12 15:10:11.080: D/szipinf(416): Initializing inflate state
    01-12 15:10:11.080: D/szipinf(416): Initializing zlib to inflate
    01-12 15:10:11.100: D/szipinf(416): Initializing inflate state
    01-12 15:10:11.100: D/szipinf(416): Initializing zlib to inflate
    01-12 15:10:12.359: D/PhoneGapLog(416): file:///android_asset/www/index.html: Line 13 : ReferenceError: Can t find variable: onDeviceReady
    01-12 15:10:12.359: E/Web Console(416): ReferenceError: Can t find variable: onDeviceReady at file:///android_asset/www/index.html:13
    01-12 15:10:32.759: D/dalvikvm(416): GC_CONCURRENT freed 362K, 52% free 2806K/5767K, external 884K/1038K, paused 6ms+4ms
    01-12 15:11:42.329: D/dalvikvm(416): GC_CONCURRENT freed 489K, 54% free 2738K/5895K, external 884K/1038K, paused 6ms+3ms
    01-12 15:12:45.579: D/dalvikvm(416): GC_CONCURRENT freed 426K, 55% free 2696K/5895K, external 884K/1038K, paused 6ms+3ms
最佳回答
问题回答

你的问题列在错误名单上:

    01-12 15:10:12.359: E/Web Console(416): ReferenceError: Can t find variable:
        onDeviceReady at file:///android_asset/www/index.html:13

TelephoneGap建议采用一种与标准OM <稍有不同的模式,即载荷/加特;办法。 您与装置准备就绪的活动有:

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

Now you need to implement the onDeviceReady function, for example, between the <script> tags:

function onDeviceReady() {
    alert("I m ready!");
}

这将消除你的第一个问题。 或许还有:

    01-12 15:08:52.230: W/System.err(338): java.net.SocketException:
        Permission denied

这是因为,你重新装货的网页正试图在另一个服务器上达到URL(http:192.168.1.3:8000/index):这是否是你重新上台的同一地址?

数据Typejson jsonp之间的区别在于:json将按JSON.parse分类的jsonp退还jsonp的数据;jsonpjsonp之间的区别是:>>jsonp:>>>之间的差别在于,>>将按格式填写的json/index_p>,如果您有的话,将检索并填写。

因此,在使用jsonp时,服务器的数据I fetch将照此办理(请上testp.js):

jsonpCallback(
     [
        { "name": "First", "latitude": 100, "longitude": 120 },
        { "name": "Second", "latitude": -23, "longitude": -20 },
        { "name": "Third", "latitude": 40, "longitude": 120 }
     ]
);

www.un.org/Depts/DGACM/index_french.htm

  <html>
    <head>
      <script src="js/jquery.js" ></script>
      <script language="javascript">
      $(function() {
        var output = $( #output );

        $.ajax( {
              url: "testp.js",
              timeout: 5000,
              dataType: "jsonp",
              jsonpCallback:  jsonpCallback ,
              success: function(data, textStatus, jqXHR) {
                 alert( In success method );
              },
              error: function(err, textStatus, errorThrown){
                 output.text("There was an error [" + textStatus + "] loading the data.");
                 console.log(err);
                 console.log(errorThrown);
              }      
        });
      });

      function jsonpCallback(data) {
        var output = $( #output );
         $.each(data, function(i,item){ 
             var landmark =  <h1> +item.name+ </h1> 
             +  <p> +item.latitude+ <br> 
             + item.longitude+ </p> ;

             output.append(landmark);
         });
     }
      </script>
    </head>
  <body>
  <div id="output"></div>
  </body>
  </html>

有两个重要要点:

  1. 在使用jsonp时,我提供了我要求获得检索数据的职能的名称。 j Query将此列入questback para amount,作为请求的一部分,因此申请的浏览器是http:// localhost/testp.js?questback=jsonpCallback&_=1326369112478。 我的试卷.js显然不应硬化这一背书,而应使用规定参数来形容适当的退约方法。

  2. While my jsonpCallback function gets called, so does the success method. Logically, though, the proper place for your processing code is in the jsonpCallback method, per my example.

<代码>jsonp> > 的优点是,jsonp可要求从接收该请求的网页的不同服务器中提取一个字母。 这允许交叉处方。





相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...