Question

I am new to this and want my app to get his data from a online .Js file. It gives me an error. This is my code,

var xhr = Titanium.Network.createHTTPClient();
xhr.open("GET","http://rstandaert.createandlearn.eu/mobile_db.js");
xhr.onload = function(){
  // check status of connection to server
statusCode = xhr.status;
    //check the response code returned
    if(statusCode == 200)
    {
        var doc = this.responseText;
//write data from downloaded text file to local text file
        var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'mobile_db.js');
        f.write(doc);
    }

    };
xhr.send();
//-----------------------------------------------------------------------
//---------------------local file read---------------------------------------
var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, 'mobile_db.js');


// read the file into contents var
//var contents = f.read();

// print out contents of file 
//Ti.API.info('contents = ' + contents.text);

//-----------------------------------------------------------------------
//--------------------------end of file read--------------------------
//var content2 = f.read();
Ti.include('mobile_db.js');


//var together = new Array();

//for ( i = 0; i < niveauArray.length; i++){

    var togetherh = Titanium.UI.createLabel({

    text : niveauArray[0].niveau_title,
    font : {
        fontSize : 24,
        fontFamily : 'Helvetica Neue',
        fontWeight : 'bold'
    },
    color : 'black',
    top : '10',
    width : '100%',
    textAlign : 'center',
    height : 'auto',
    left : 'auto',
    touchEnabled : false

});

var together = Titanium.UI.createLabel({

    text : niveauArray[0].niveau_id + '  ' + niveauArray[0].niveau_desc,
    font : {
        fontSize : 12,
        fontFamily : 'Helvetica Neue'
    },
    color : 'black',
    top : '50',
    width : '85%',
    textAlign : 'left',
    height : 'auto',
    left : 18,
    touchEnabled : false

});

//}

//var viewqq = Ti.UI.createView();

var viewqq = Ti.UI.createView({
 // backgroundColor:'#336699',
  borderRadius: 10,
  top: 10,
  height: 2000,
  width: 'auto'
});

//viewqq.add(lblTitle);
//viewqq.add(together);
//viewqq.add(togetherh);

Ti.UI.currentWindow.add(viewqq);



var scrollView = Ti.UI.createScrollView({
  contentWidth: 'auto',
  contentHeight: 'auto',
  showVerticalScrollIndicator: true,
  showHorizontalScrollIndicator: false,
  height: '100%',
  width: '100%'
});


scrollView.add(viewqq);
scrollView.add(together);
scrollView.add(togetherh);
Ti.UI.currentWindow.add(scrollView);

Ti.UI.currentWindow.add(viewqq);

When i open the app and open the table(the one wich is supposed to be filled with text drawn out of the online .js file) it just remains empty.

I'll keep getting errors like:

[ERROR][dalvikvm( 1016)] Could not find class 'ti.modules.titanium.ui.widget.searchview.TiUISearchView', referenced from method ti.modules.titanium.ui.widget.TiUITableView.processProperties

and

[ERROR][InputDispatcher(   62)] channel '40796bd0 com.Innovisionsolutions.com/com.Innovisionsolutions.com.MboAppPdhActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x8
[ERROR][InputDispatcher(   62)] channel '40796bd0 com.Innovisionsolutions.com/com.Innovisionsolutions.com.MboAppPdhActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
[INFO][WindowManager(   62)] WIN DEATH: Window{40796bd0 com.Innovisionsolutions.com/com.Innovisionsolutions.com.MboAppPdhActivity paused=true}
[INFO][WindowManager(   62)] WIN DEATH: Window{407ea478 com.Innovisionsolutions.com/org.appcelerator.titanium.TiActivity paused=false}
[ERROR][InputDispatcher(   62)] Received spurious receive callback for unknown input channel.  fd=165, events=0x8

and

[ERROR][TiFastDev(  952)] (KrollRuntimeThread) [4971,3410852] Broken pipe
[ERROR][TiFastDev(  952)] java.net.SocketException: Broken pipe
[ERROR][TiFastDev(  952)]   at org.apache.harmony.luni.platform.OSNetworkSystem.write(Native Method)
[ERROR][TiFastDev(  952)]   at dalvik.system.BlockGuard$WrappedNetworkSystem.write(BlockGuard.java:284)
[ERROR][TiFastDev(  952)]   at org.apache.harmony.luni.net.PlainSocketImpl.write(PlainSocketImpl.java:472)
[ERROR][TiFastDev(  952)]   at org.apache.harmony.luni.net.SocketOutputStream.write(SocketOutputStream.java:48)
[ERROR][TiFastDev(  952)]   at org.appcelerator.kroll.common.TiFastDev$Session.sendTokens(TiFastDev.java:484)
[ERROR][TiFastDev(  952)]   at org.appcelerator.kroll.common.TiFastDev.openInputStream(TiFastDev.java:212)
[ERROR][TiFastDev(  952)]   at org.appcelerator.kroll.util.KrollAssetHelper.readAsset(KrollAssetHelper.java:53)
[ERROR][TiFastDev(  952)]   at org.appcelerator.kroll.runtime.v8.V8Object.nativeCallProperty(Native Method)
[ERROR][TiFastDev(  952)]   at org.appcelerator.kroll.runtime.v8.V8Object.callProperty(V8Object.java:69)
[ERROR][TiFastDev(  952)]   at org.appcelerator.kroll.KrollProxy.handleMessage(KrollProxy.java:1106)
[ERROR][TiFastDev(  952)]   at org.appcelerator.titanium.proxy.TiViewProxy.handleMessage(TiViewProxy.java:316)
[ERROR][TiFastDev(  952)]   at org.appcelerator.titanium.proxy.TiWindowProxy.handleMessage(TiWindowProxy.java:100)
[ERROR][TiFastDev(  952)]   at ti.modules.titanium.ui.WindowProxy.handleMessage(WindowProxy.java:408)
[ERROR][TiFastDev(  952)]   at android.os.Handler.dispatchMessage(Handler.java:95)
[ERROR][TiFastDev(  952)]   at android.os.Looper.loop(Looper.java:130)
[ERROR][TiFastDev(  952)]   at org.appcelerator.kroll.KrollRuntime$KrollRuntimeThread.run(KrollRuntime.java:112)
[ERROR][AssetsModule(  952)] Failed to load resource.
[WARN][System.err(  952)] java.lang.NullPointerException
[WARN][System.err(  952)]   at org.appcelerator.kroll.util.KrollAssetHelper.readAsset(KrollAssetHelper.java:55)
[WARN][System.err(  952)]   at org.appcelerator.kroll.runtime.v8.V8Object.nativeCallProperty(Native Method)
[WARN][System.err(  952)]   at org.appcelerator.kroll.runtime.v8.V8Object.callProperty(V8Object.java:69)
[WARN][System.err(  952)]   at org.appcelerator.kroll.KrollProxy.handleMessage(KrollProxy.java:1106)
[WARN][System.err(  952)]   at org.appcelerator.titanium.proxy.TiViewProxy.handleMessage(TiViewProxy.java:316)
[WARN][System.err(  952)]   at org.appcelerator.titanium.proxy.TiWindowProxy.handleMessage(TiWindowProxy.java:100)
[WARN][System.err(  952)]   at ti.modules.titanium.ui.WindowProxy.handleMessage(WindowProxy.java:408)
[WARN][System.err(  952)]   at android.os.Handler.dispatchMessage(Handler.java:95)
[WARN][System.err(  952)]   at android.os.Looper.loop(Looper.java:130)
[WARN][System.err(  952)]   at org.appcelerator.kroll.KrollRuntime$KrollRuntimeThread.run(KrollRuntime.java:112)
[ERROR][V8Exception(  952)] Exception occurred at ti:/window.js:158: Uncaught Error: Failed to load resource, Java exception was thrown.

Can anyone help me out :/?

Was it helpful?

Solution 2

you can check with filesystem object that if that file exists or not....

var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, 'mobile_db.js');
if(f.exists()){
   f.deleteFile();
}

then of course you need to use path instead of just use file name..

like...

Ti.include(f.nativePath);

and no need to create 2 different objects for filesystem. you can use same to write also......

var xhr = Titanium.Network.createHTTPClient();
xhr.open("GET","http://rstandaert.createandlearn.eu/mobile_db.js");
xhr.onload = function(){
  // check status of connection to server
statusCode = xhr.status;
    //check the response code returned
    if(statusCode == 200)
    {
        var doc = this.responseText;
//write data from downloaded text file to local text file
        f.write(doc);
    }

    };
xhr.send();

OTHER TIPS

Just replace

Ti.include('mobile_db.js');

with

Ti.include(f.nativePath);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top