2

Phonegap/Apache Cordova を使用してハイブリッド Android アプリを構築しています。アプリは Web API からデータを取得する必要があります。JSON を使用してデータを APP に提供しています。だから私は次のコードを得ました:

function init() { 
document.addEventListener('deviceready', onDeviceReady, false);
var url = "http://23.21.128.153:3000/regions.json";var jsonresults;
    $.getJSON(url,function(data){
        jsonresults = data;
          $.each(jsonresults, function(i,v){
          $('#main-content').append('<li>'+jsonresults[i].name+'</li>');
        });
    });
}

また、html の本文には、main-content という div があります。Everythings は Eclipse ブラウザーでは正常に動作しますが、Android Emulator では動作しません。JSON を使用して Web API からデータを取得し、データを取得した後に動的に HMTL 要素を作成する別の方法があるかどうかはわかりません。

https://gist.github.com/2956660

4

1 に答える 1

1

Web API からデータを取得するのは非常に簡単です。このコードを試してください。それはあなたの問題を解決します、

jquery mobile を使用すると非常に簡単です。通常の Ajax と少しの JSON を使用して作業を行うことができます。私はphpサーバーでやった。ここにサンプルコード、

jqueryを介してサーバーにリクエストを送信します。

$.ajax({
          url: <your Server URL>,
          dataType: 'jsonp',
          jsonp: 'jsoncallback',
          timeout: 5000,
          success: function(data, status){
                             /*Process your response here*/

      jsonresults = data;
      $.each(jsonresults, function(i,v){
      $('#main-content').append('<li>'+jsonresults[i].name+'</li>');
    });

 $.mobile.changePage($('#index')); /*page navigate the particular where data shown*/
 $("#index").trigger("pagecreate"); /*This is like a page refresh and load the injected data in jquery*/
                        }
       });

PHPサーバーファイルで、

<?php
$data="I am sending response to you";

/*you have to mention this callback compulsory*/

echo $_GET['jsoncallback'] . '(' . json_encode($data) . ');';
?>
于 2012-06-20T03:11:01.847 に答える