Can you load a Json file from your C:Drive PLEASE HELP

You must Login before you can answer or comment on any questions.

Hi All i just want to know is it possible to load a Json file from your C:drive if you saved it there as a .txt for testing or can you maybe see the error in my code

var url = "http://WF7_API/api/transaction/inbox/johnny";    
var json , Inbox , Task , i , row , TaskName;
 
var table = Ti.UI.createTableView({ top:'12%'});
var tableData = [];
 
var xhr = Ti.Network.createHTTPClient({});
    xhr.open("GET", url);
        xhr.onload =  function() {
        json = JSON.parse(this.responseText);
        for ( i = 0; i < json.Inbox.length ; i++){
            Task = json.Inbox[i];
            row = Ti.UI.createTableViewRow(
            {
                top:'12%',
                height:60,
            });
            lblTaskName = Ti.UI.createLabel(
            {
                text:Task.Transaction,
                font:{fontSize:'24dp'},
                height:'auto',
                left:'10dp',
                top:'50dp',
                color:'#000',
                touchEnabled:true,  
            });
        row.add(lblTaskName);
        tableData.push(row);
    };
        table.setData(tableData);
 
    }
    xhr.onerror =  function(e) {
        Ti.API.debug("STATUS: " + this.status);
        Ti.API.debug("TEXT: " + this.responseText);
        Ti.API.debug("ERROR: " + e.error);
        alert('There was an error retrieving the remote data. Try again.');
       }
    timeout = 5000
    xhr.send();
    winMail.add(table);
Please help

2 Answers

Stiaan, you running this from a Win machine? If so, I don't think the emulator or device will see the local hostname. Instead, try by IP address:

http://192.xxx.xxx.xxx/api/transaction/inbox/johnny
// replace ^^^^^^^^^^ with actual local IP
I don't have experience working on a Win PC, so let me know if this works.

HTH. Bob

— answered 7 months ago by Bob Sims
answer permalink
3 Comments
  • I have tried that now with the local host's IP but still nothing is there a way I can use a Json file on my C: drive just for testing and then after testing just change the URL to the live one ?

    — commented 7 months ago by Stiaan Maas

  • Stiaan, the file would need to be served by an http server... does the URL display JSON in the browser on the desktop? How about browser from mobile device on same network? If not, wouldn't work from Ti.Network.createHTTPClient either.

    — commented 7 months ago by Bob Sims

  • Stiaan, simple way to reference it is to assign the JSON object to a variable name, then expose it as a CommonJS module, example here.

    Or, you can drop a text file containing only the JSON object into your /Resources directory, and read it using Ti.Filesystem, good example here.

    HTH. Bob

    — commented 7 months ago by Bob Sims

As long as your txt file is located within your Resources directory, you should have no problem reading it for testing.

Your Answer

Think you can help? Login to answer this question!