0

My question is that, if it is possible to get a variable from a .properties or .txt file. I need that in order to save the endPoint, the server IP address. That way, every time that the IP address is changed, all you need to do is change the IP on the file, instead of compile the code again and build the apk, and install it again on the device. Any tips will be welcome.

UPDATE I've been trying to use this plugin :File , but with no sucess. I want to use readAsText(path,file) but all i get is undefined

import { File } from '@ionic-native/file/ngx';

constructor(
    ....
    private file : File,
    ....
  ) { }

async test(){
this.promise = this.file.readAsText('file:///data/','ipAddress.txt'); 
await this.promise.then(value => {
console.log(value)
}).catch(error=> console.log("nao existe"))
}

And i'm getting the following error:

ERROR Error: Uncaught (in promise): FileError: {"code":1,"message":"NOT_FOUND_ERR"}

UPDATE Problem Solved. I was trying to acess to the internal storage, and the document was on the external storage. Make sure you use URI, path on android are different from computer. You need URI.

file:///storage/emulated/0/Android/data/

I called the method right on, when device's ready on app.component.ts

  ngOnInit() {  
  ..... 
  this.initializeApp();
  }

initializeApp() {
this.platform.ready().then(() => {  
this.api.getURL();
.....
});
}

Hope i can help the next with the same problem I had.

jcmendes98
  • 148
  • 1
  • 10
  • Where you want to save the .txt file ? – Swayangjit Oct 17 '19 at 11:41
  • https://nodejs.org/api/fs.html#fs_file_system That seems like a suitable solution. Copied from https://stackoverflow.com/questions/33643107/read-and-write-a-text-file-in-typescript – mortom123 Oct 17 '19 at 11:41
  • @Swayangjit the file is to be stored on the android device. Probably close to apk. – jcmendes98 Oct 17 '19 at 11:50
  • You can have a component `save-ip.component` that ask about the IP. Use `localStorage` https://developer.mozilla.org/es/docs/Web/API/Window/localStorage to save the IP. So, use a guard https://angular.io/guide/router#milestone-5-route-guards. in the guard, ask about localStorage.getItem('IP'). If has no value, redirect to the `save-ip.component` else save in a variable of one service (or each tiem you need ask about localStorage.getItem('IP'). Using this aproach, only need clear the data of the .apk when the IP was changed – Eliseo Oct 17 '19 at 13:16
  • @Swayangji , can you check the code that i put? – jcmendes98 Oct 17 '19 at 18:28
  • @mortom123 , can you check the code that i put? – jcmendes98 Oct 17 '19 at 18:28
  • @Eliseo, can you check the code that i put? – jcmendes98 Oct 17 '19 at 18:28
  • @jcmendes98 you cant read a file from your system . why don't you use a class which will store all your configs and read all your configs. – Swayangjit Oct 18 '19 at 05:39
  • this go to the client. If he decides to change the IP address, we need to change the variable, and build the apk again and install on all devices.... This way all he has to do is change it on the .txt file. @Swayangjit – jcmendes98 Oct 18 '19 at 07:29

0 Answers0