簡體   English   中英

帶有Web服務調用的HTML5應用程序可在模擬器上運行,但不能在設備上運行

[英]Html5 app with web service calling works on emulator but not on device

我已經使用javascript開發了HTML5應用程序。 我已經使用json調用了php web服務。 現在我正在嘗試為Android執行它。

該應用程序可與模擬器上的Web服務配合使用,但無法從設備上的服務器加載文本和圖像。

我嘗試了許多在stackoverflow和其他鏈接上找到的解決方案,例如使用主機名,檢查設備的wifi,清單文件中的INTERNET權限,使用WebView,但是它不起作用。

請幫忙。

//這是我在日食中所做的。

public class MainActivity extends DroidGap {

    WebView webview;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d("hello","start");

        webview = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webview.setWebChromeClient(new WebChromeClient());

        webview.loadUrl("file:///android_asset/www/HTML/index.html");
        Log.d("After load", "url");

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

抓取圖像和文本都是用javascript完成的。 在瀏覽器和模擬器上都沒有問題。 但是在設備上失敗。

//這是我的json調用函數。

<script>
    function jsondata(data)
    {
        setTimeout(function () {
         var parsedata = JSON.parse(JSON.stringify(data));
         var main_category = parsedata["mainCategory"];

            for (var i = 0; i < main_category.length; i++) 
            {
                var menu = main_category[i];
                menu_id=menu['mcatid'];         

                var id = document.createElement("table");   

                if((i%2)==0)
                {

                   id.innerHTML+='<br><br><br><td><a href="#" onclick="selected_index('+menu_id+')"><img src='+menu['mcatimage']+'><br><hr color=#000;>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong><font face="DEVROYE">'+menu['mcattitle']+'<hr color=#000;></a></td></strong>';
                  document.getElementById("id_left").appendChild(id);
                }
                else
                {

                   id.innerHTML+='<br><br><br><tr><td><a href="#" onclick="selected_index('+menu_id+')"><img src='+menu['mcatimage']+'><br><hr color=#000;>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong><font face="DEVROYE">'+menu['mcattitle']+'<hr color=#000;></a></td></strong>';
                   document.getElementById("id_right").appendChild(id);
                }   
            }
            }, 2000); 
     }

    /*-------------calling web service-----------*/
    jsonp("http://hostname/maincategory.php?callback=jsondata");
</script>

這是我從Web服務中獲取圖像和文本並顯示在HTML頁面上的方式。 這是什么問題?

今天,我嘗試了很多操作,例如將<access origin="*"/>更改為<access origin=".*"/><access origin="http:hostname"/>但是沒有任何效果。

我的應用程序現在也可以在設備上運行。

從android方面沒有問題。 我在JavaScript中使用ajax而不是json。 並且它也開始在設備上有效地工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM