3

[稍后编辑:正如我发现的,问题与 Android 版本有关,而不是设备类型。所以我的代码在 4.0 之前非常适合 Android,而不是更高版本。修复在答案中。]

我在这个问题上浪费了至少 2 天。我很少有网页打包为 Android 应用程序。在浏览器和我的 Android 设备上完美运行,包括 Galaxy Tab 2。但在 Nexus 上却不行。我没有它,所以我一直在制作 APK 和一个朋友测试。错误出现在 AJAX 上。相同的代码对我有用,对他不起作用(还有少数人,我不知道他们的设备)。

下面是我使用的小测试。如您所见,它没有错误(这是我的猜测)。为什么不能在所有 Android 设备上运行?我提到我已经用 Eclipse 和 Build.PhoneGap.com编译了这段代码(其他引用的文件在这里http://jumpshare.com/b/57O6tH )。然而,同样的结果:我得到的 APK 在某些设备上运行,而不是在其他设备上运行。使用 *file:///android_asset/www/import.html* 对我没有帮助。错误是 404,因为文件不存在。但它是!

错误在哪里?它让我疯狂 :)。为什么此代码在我的 Galaxy Tab 2(和 Samsung Gio)上的浏览器和 APK 中运行良好,但在 Nexus(和其他设备)上却不行?

<!DOCTYPE html>
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Test</title> 
    <meta name="viewport" content="width=device-width, initial-scale=1">    
    <link href="jquery.mobile-1.2.0.min.css" rel="stylesheet"/>
    <script src="jquery-1.8.3.min.js" type='text/javascript'></script>
    <script src="jquery.mobile-1.2.0.min.js" type='text/javascript'></script>   
    <script type='text/javascript'>
    //$(document).ready(function() {
    $(document).bind("pageinit", function(){
        $("#buton").bind('click',function(){
            $.mobile.showPageLoadingMsg();
            $.ajax({
                url:'import.html',
                datatype:'html',
                type: 'GET',
                success:function(html){
                    $.mobile.hidePageLoadingMsg();
                    $("#result").html(html);
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    $("#result").html("ERRORS:"+errorThrown+"<hr>"+textStatus+"<hr>"+JSON.stringify(jqXHR))
                    $.mobile.hidePageLoadingMsg();
                    alert('Not working!!!');
                }
            })
        });
    });
    </script>
</head> 
<body> 
    <!-- Pagina de start -->
    <div data-role="page" id="start">
        <div data-role="header" data-theme="b">
            <h1>Test</h1>
        </div>
        <div data-role="content">
            <button id="buton">AJAX!</button>
            <div id="result"></div>
        </div>
    </div>
</body>
</html>
4

2 回答 2

5

我找到了我需要的东西。Android 4.1 和 4.2 引入了这个新方法:getAllowUniversalAccessFromFileURLs

由于它不适用于低于 16 的 API,因此该解决方案需要多几行,以确保这种不存在的方法不会在以前的 API 中导致错误。

public class MainActivity extends Activity {
/** Called when the activity is first created. */
WebView webView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    webView = (WebView) findViewById(R.id.webView);
    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    webView.getSettings().setJavaScriptEnabled(true);
    int currentapiVersion = android.os.Build.VERSION.SDK_INT;
    if (currentapiVersion >= android.os.Build.VERSION_CODES.JELLY_BEAN){
        fixNewAndroid(webView);
    }
    webView.setWebChromeClient(new WebChromeClient());
    webView.loadUrl("file:///android_asset/www/index.html");
}

@TargetApi(16)
protected void fixNewAndroid(WebView webView) {
    try {
        webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
    } catch(NullPointerException e) {
    }
}

}

于 2013-01-12T12:52:14.060 回答
0

你检查过你的ajax GET返回什么吗?它是返回 DOM 文档!而你试图为你的#result html 设置文档!如果你看一下http://api.jquery.com/jQuery.get/你可以看到你的成功函数有 3 个参数:

success:function(data, textStatus, jqXHR){}

现在您可以选择如何获取您的 html 并将其显示为$("#result").

$("#result").html(jqXHR.responseText);
于 2013-01-17T22:41:17.280 回答