ajax 在某些 Android 设备上工作,而不是在其他设备上 [英] ajax working on some Android devices, not in other

查看:20
本文介绍了ajax 在某些 Android 设备上工作,而不是在其他设备上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[后期据我所知,问题与 Android 版本有关,与设备类型无关.所以我的代码在 4.0 之前非常适合 Android,而不是更高版本.答案就在答案中.]

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

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

错在哪里?它让我疯狂 :).为什么此代码可以在浏览器中正常运行,并且作为 APK 在我的 Galaxy Tab 2(和三星 Gio)上运行良好,但在 Nexus(和其他设备)上却不能运行?

<头><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>测试</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',数据类型:'html',类型:'获取',成功:功能(html){$.mobile.hidePageLoadingMsg();$("#result").html(html);},错误:函数(jqXHR,textStatus,errorThrown){$("#result").html("ERRORS:"+errorThrown+"<hr>"+textStatus+"<hr>"+JSON.stringify(jqXHR))$.mobile.hidePageLoadingMsg();alert('不工作!!!');}})});});<身体><!-- 开始页面--><div data-role="page" id="start"><div data-role="header" data-theme="b"><h1>测试</h1>

<div data-role="内容"><button id="button">AJAX!</button><div id="结果"></div>

解决方案

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

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

public class MainActivity extends Activity {/** 在第一次创建活动时调用.*/WebView webView;@覆盖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) {尝试 {webView.getSettings().setAllowUniversalAccessFromFileURLs(true);} catch(NullPointerException e) {}}

}

[LATER EDIT: As I found, the issue is related to Android version, not device type. So my code was perfect for Android till 4.0, not above. The fix is in the answer.]

I have wasted at least 2 days with this problem. I have few webpages packed as an Android application. And working perfectly on browser, and on my Android devices, including Galaxy Tab 2. But not on Nexus. I don't have it, so I kept making APK and a friend tested. The error was at AJAX. The same code work for me, do not work for him (and few others, I don't know their devices).

Below is the small test I use. As you can see, it's error free (this is my guess). Why is not working on all Android devices? I mention that I've compiled this code (the other refered files are here http://jumpshare.com/b/57O6tH) with Eclipse and also with Build.PhoneGap.com. Yet, the same result: the APK I get is working on some devices, not on others. Using *file:///android_asset/www/import.html* did not help me. The error is 404, as the file is not there. But it is!

Where is the mistake? It drives me crazy :). Why this code works fine in browser and as APK on my Galaxy Tab 2 (and on Samsung Gio), but not on Nexus (and other devices)?

<!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>

解决方案

I found what I needed. Android 4.1 and 4.2 introduce this new method: getAllowUniversalAccessFromFileURLs

Since it's not working on API below 16 the solution needs some few more lines, to assure that this inexistent method do not cause errors in previous 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) {
    }
}

}

这篇关于ajax 在某些 Android 设备上工作,而不是在其他设备上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
移动开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆