简体   繁体   中英

Local Ajax request / Sencha / PhoneGap on Android

I am trying to load some content in a PhoneGap App via a local Ajax request. It seems to work fine on iPhone, but Android wont find the file.

It goes like this:

Ext.Ajax.request({
        url: 'file:///android_asset/webapp/content/file.html',
        success: function(response, opts) {
            console.log('response ' + response.responseText);
        }
        failure: function(response, opts) {
            console.log('response ' + response.responseText);
        }
    });

I already adjusted the Manifest and did set the Internet Permission, still no success.

Do you have any idea?

I'm not sure this totally meets your requirements but have a look at this Phonegap Android plugin collection. It includes plugins for uploading and downloading content for PhoneGap on Android.

The first thing I do in situations like this is to get rid of the dependency on third party libraries like Sencha or jQuery and go straight to use XHR. There is an issue where making requests from file protocol sometimes returns a status of 0 when it actually is 200. Most libraries have this fixed but you never know. Try this XHR code which should work just great on Android:

var request = new XMLHttpRequest();
    request.open("GET", "file:///android_asset/webapp/content/file.html", true);
    request.onreadystatechange = function(){
        if (request.readyState == 4) {
            if (request.status == 200 || request.status == 0) {
                console.log("response " + request.responseText);
            }
        }
    }
    request.send();

Which version of Sencha Touch are you using? As Simon pointed out, many libs report a failure when encountering a response code of 0, Sencha Touch fixed this as of 2.0.0

http://www.sencha.com/forum/showthread.php?173577-Ext.data.Connection-fails-when-evaluating-a-file-protocol-s-status

Second, try removing the file:///android_asset/webapp/ part of the ULR, a relative url to your content directory should work just fine, and alleviate any OS detection switching you might be doing to arrive at that path.

Ext.Ajax.request({
    url: 'content/file.html',
    success: function(response, opts) {
        console.log('response ' + response.responseText);
    }
    failure: function(response, opts) {
        console.log('response ' + response.responseText);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM