无法将我从在线.js文件中获取的数据写入本地文件。钛

时间:2014-04-23 09:07:43

标签: javascript titanium

我是新手,希望我的应用从在线.Js文件中获取数据。它给了我一个错误。 这是我的代码,

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);

当我打开应用程序并打开表格(应该填写从在线.js文件中提取的文本的那个)时,它仍然是空的。

我会不断收到错误:

[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.

任何人都可以帮助我:/?

2 个答案:

答案 0 :(得分:1)

只需替换

Ti.include('mobile_db.js');

Ti.include(f.nativePath);

答案 1 :(得分:0)

您可以检查文件系统对象是否存在该文件....

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

然后当然你需要使用路径而不是只使用文件名..

像...

Ti.include(f.nativePath);

并且无需为文件系统创建2个不同的对象。你也可以用它来写......

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();