Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
520 views
in Technique[技术] by (71.8m points)

ajax working on some Android devices, not in other

[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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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) {
    }
}

}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...