PhoneGap read XML with FileReader? - android

I have an XML file on the SDCard, that is downloaded from a server. I am trying to open and read in that XML in order to use it within my app. I open and read it fine, but JQuery is freezing on parsing it.
Once I get the file I do like so:
var reader = new FileReader();
reader.onloadend = function (evt) {
parseXML(evt.target.result);
};
reader.readAsText(file);
And then I begin parsing like so:
function parseXML(xml){
$(xml).find('recipe').each(function(){
And it stops there. If I load the in-app XML using JQuery's ajax method it works fine. So, is there anything special about loading XML data using the FileReader in PhoneGap?
UPDATE:
OK, with much time and help from another dev, the issue appears to be a bug in PhoneGap. Specifically the FileTRansfer object.
I was doing this:
var ft = new FileTransfer();
var dlPath = dataDir.fullPath + "/recipes.xml";
ft.download("http://design.mydomain.com/dave/humana/recipes/recipes.xml", dlPath, addedNewRecipes, addError);
And I'd get a trace within addedNewRecipes that the download succeeded. And it did... just not all of it. The file was truncated and that's what was causing the parseXML to fail. The entire XML file is about 90KB (~1600 lines) it was being truncated at maybe 50 lines.
Replaced FileTransfer with a FileWriter object and it works now.
PhoneGap/Cordova really is still beta isn't it.

Related

Air for android ignores external swf files

I am working on some app that loads external swfs from the application directory
the problem is that some swf files get loaded correctly and others give ioerror url not found
i put the paths in an array and use a loader to load the path
var arr:Array = ["Games/1.swf", "Games/2.swf"];
var loader:Loader = new Loader();
loader.load(new URLRequest(arr[0]));
this is just example and it is the same in loading all the files but does not work on all files.
what would be the problem?
Firstly, whenever you load something, listen for the appropriate error events so you know what's going on:
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
When using mobile, you should use the app:// shorthand as a reference to the application directory (which I assume is where your Game folder is. Relative paths do not usually work.
So it should look like this:
var arr:Array = ["app://Games/1.swf", "app://Games/2.swf"];
For more information/options, you can look at my answer to this question

How to create a blob in Android HTML5 app

My server spits out the source of a PDF. With XHR2 I request that code using xhr.responseType = 'blob'; For iOS I then just use
blob = new Blob([this.response], {type: "application/pdf"});
and then save it using Phonegap's Filewriter. When I then open the saved file, I have a perfect PDF. I simply can't get anything similar to work on Android. My last attempt was:
if (!window.BlobBuilder && window.WebKitBlobBuilder){
window.BlobBuilder = window.WebKitBlobBuilder;
var bb = new window.BlobBuilder();
bb.append(this.response);
blob = bb.getBlob("application/pdf");}
since Android coughed over just plain 'BlobBuilder'. That duly creates a PDF which FileWriter then saves. The files structure is presumably OK as a PDF since PDF readers open it without complaint. Unfortunately, the PDF is completely blank.
I just don't seem to be able to get my elderly head around whatever is required by Android.

not able to read input file content using phonegap

i am using phone gap to read input file content as follows
<input type="file" id="sendFile" class="ui-input-text ui-body-c">
in my java script i am reading it as follows , this function gets called once the
device is ready
function readImage() {
var fileInput = document.getElementById('sendFile');
var tfile = fileInput.files[0];
var FR= new FileReader();
FR.onloadend = function(e) {
console.log("success");
}
FR.readAsDataURL(tfile);
}
and i am getting following error , i have no idea why its there and how to resolve it
file:///android_asset/www/cordova.js: Line 1064 : processMessage
failed: Error: TypeError: Result of expression
'this._realReader.readAsDataURL' [undefined] is not a function.
i am using android 2.2.1 version
note**
it works with fileEntry object but it doesnt work with input tag , i hope anyone has a solution for this
I think that the problem is that the browser in android 2.x does not fully implement the html5 file api.
If you look at the attributes of the file object you get from the file input on android 2, some are missing like lastModifiedDate and most of the functions are also not there.
Running the same code in an android 4.2.2 device works perfectly :(
Unfortunatly, to get a file object from the file input, you do not rely on a phonegap function but on the support of the html5 file api of the default browser on your device.
It seems that on android the file api is only supported starting with version 3.x
If you only want to support images, you could use the
Phonegap camera plugin and set Camera.sourceType to Camera.PictureSourceType.PHOTOLIBRARY to select a picture from the album.

Flex Air Mobile File Download

I am building an Adobe Air Mobile application currently targeting Android (and eventually iOS). I'm using Flash Builder 4.6 (which is Flex).
I have a requirement to download a zip file from a url, save it to local storage and unzip the contents.
There seem to be a few example on how to do a download in Flex/Air, but not many in Flex/Air/Mobile. Additionally, the mix of examples seem to be all over the place with namespace versions, etc.
Could someone please provide a succinct example on how to do this in an Air Mobile app?
Thank you!
Edit: What I've tried:
So, from the examples on the web, the first thing I did was add namespace in Flex declarations: xmlns:net="flash.net.*"
Then I added the following component: <net:URLLoader id="urlLoader" />
Then, in the button click event, I called it: urlLoader.load(new URLRequest(downloadUrl));
I get a run time error: Error #2044: Unhandled ioError:. text=Error #2032: Stream Error. URL: http://...
The problem is I don't know where to go from here since I'm not sure what's missing.
this code works for me on IOS:
public function download():void{
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(IOErrorEvent.IO_ERROR,function(e:IOErrorEvent):void{
Alert.show('error IO');
});
loader.addEventListener(Event.COMPLETE,downloadComplete);
loader.load(new URLRequest(YOUR_URL));
}
private function downloadComplete(event:Event):void{
try{
var file:File=File.applicationStorageDirectory.resolvePath(YOUR_LOCAL_PATH);
var ba:ByteArray = event.target.data as ByteArray;
var fileStream:FileStream = new FileStream();
fileStream.addEventListener(IOErrorEvent.IO_ERROR,function(e:IOErrorEvent):void{
Alert.show('error IO');
});
fileStream.open(file, FileMode.WRITE);
fileStream.writeBytes(ba);
fileStream.addEventListener(Event.CLOSE, fileClosed);
fileStream.close();
Alert.show("File downloaded succesfully")
}
catch(eeee){
Alert.show("error")
}
}

AS3 Air On Android - Load SWF as its own Main()

I'm using a wrapper app to download a remote SWF and save it to a Xoom Tablet. The download and save works fine but using something like
var file:File = File.applicationStorageDirectory.resolvePath("app.swf");
var loader:Loader = new Loader();
loader.load(new URLRequest(file.url)/*,context needed?*/);
_main.addChild(loader);
Screws with the targeting in the loaded swf so that in my loaded swf if I try and call "stage" from "Main" its null. I thought since loaded SWF's had there own sandbox it would load and run just like it I installed it by its self, does not seem to be the case. If this was a desktop app I could use NativeWindow but its not supported on mobile devices yet. Are there any know options where I can load and run it in its own sandbox where paths and roots are intact? Possibly a command it launch it as a separate swf from my wrapper swf?
I ran into more issues but finaly found the correct way, you cant acceess "File" but this works just fine.
function getSWF():void
{
var request:URLRequest = new URLRequest("child.swf");
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadHandler);
loader.load(request);
_main.addChild(loader);// _main is my display sprite
}
function loadHandler(event:Event):void
{
var childSwf:Object = event.target.content;
childSwf.init( PARAMS );// in my case I needed to pass XML but this is any method in the Main of the child swf, note that "stage" is read only, so your parent swf needs to set any stage vars you need
}

Categories

Resources