AJAX开发一些Android设备,而不是在其他 [英] ajax working on some Android devices, not in other

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

问题描述

[以后编辑:我发现,这个问题涉及到Android版本,而不是设备类型。所以,我的code是完美的机器人,直到4.0,不高于。解决方法是在答案。]

我已经浪费了至少2天这个问题。我包装成一个Android应用程序几个网页。 ,可以正常使用的浏览器,并在我的Andr​​oid设备,包括Galaxy Tab的2,但是没有在Nexus。我没有,所以我就继续做APK和朋友进行测试。错误是在阿贾克斯。对我来说,同样的code的工作,不要为他工作(和其他几个人,我不知道他们的设备)。

下面是小的测试我使用。正如你所看到的,它是免费的错误(这是我的猜测)。为什么不工作在所有Android设备?我提到我编译这个code(其它refered文件在这里 http://jumpshare.com/ B / 57O6tH )与Eclipse,并与Build.PhoneGap.com。然而,同样的结果:在APK我得到的是工作在某些设备上,而不是其他人。使用*文件:///android_asset/www/import.html*没有帮助我。误差是404,作为文件是不存在。但它是!

在哪里错了吗?这让我发疯:)。为什么这样code工作正常,在浏览器的APK(三星吉奥和)在我的Galaxy Tab 2,但不是在Nexus(和其他设备)?

 <!DOCTYPE HTML>
< HTML>
< HEAD>
    < META HTTP-当量=Content-Type的CONTENT =text / html的;字符集= UTF-8>
    <冠军>测试< /标题>
    < META NAME =视口内容=WIDTH =装置宽度,初始规模= 1>
    <链接HREF =jquery.mobile-1.2.0.min.css相对=样式/>
    <脚本SRC =jQuery的-1.8.3.min.js类型=文/ JavaScript的'>< / SCRIPT>
    <脚本SRC =jquery.mobile-1.2.0.min.js类型=文/ JavaScript的'>< / SCRIPT>
    <脚本类型=文/ JavaScript的'>
    // $(文件)。就绪(函数(){
    (文档)$ .bind(pageinit,函数(){
        $(#布顿)。绑定(点击,函数(){
            $ .mobile.showPageLoadingMsg();
            $阿贾克斯({
                网址:import.html,
                数据类型:HTML,
                键入:GET,
                成功:函数(HTML){
                    $ .mobile.hidePageLoadingMsg();
                    $(#结果)HTML(HTML);
                },
                错误:函数(jqXHR,textStatus,errorThrown){
                    $(#结果)HTML(错误:+ errorThrown +< HR>中+ textStatus +< HR>+ JSON.stringify(jqXHR))。
                    $ .mobile.hidePageLoadingMsg();
                    警报(不工作!');
                }
            })
        });
    });
    < / SCRIPT>
< /头>
<身体GT;
    <! -  Pagina日开始 - >
    < D​​IV数据角色=页面ID =开始>
        < D​​IV数据角色=头的数据主题=B>
            < H1>测试< / H1>
        < / DIV>
        < D​​IV数据角色=内容>
            <按钮ID =布顿> AJAX<!/按钮>
            < D​​IV ID =结果>< / DIV>
        < / DIV>
    < / DIV>
< /身体GT;
< / HTML>
 

解决方案

我发现我需要的东西。 Android的4.1和4.2引入这种新方法:getAllowUniversalAccessFromFileURLs

因为它不是工作在低于16 API的解决需要一定的几行,以确保不存在这种方法不会引起previous API错误。

 公共类MainActivity延伸活动{
/ **第一次创建活动时调用。 * /
的WebView web视图;

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    web视图=(web视图)findViewById(R.id.webView);
    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    webView.getSettings()setJavaScriptEnabled(真)。
    INT currentapiVersion = android.os.Build.VERSION.SDK_INT;
    如果(currentapiVersion> = android.os.Build.VERSION_ codeS.JELLY_BEAN){
        fixNewAndroid(web视图);
    }
    webView.setWebChromeClient(新WebChromeClient());
    webView.loadUrl(文件:///android_asset/www/index.html);
}

@TargetApi(16)
保护无效fixNewAndroid(的WebView web视图){
    尝试 {
        。webView.getSettings()setAllowUniversalAccessFromFileURLs(真正的);
    }赶上(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天全站免登陆