Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Apr 2012
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Saving Data with Java?

    Hello all,
    Ok im designing for Android and iOS

    im using eclipse at the moment and i need a way to save data from a calculator so when the user exits the application and comes back, the last numbers they typed in will be there, how can i go about this?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I'm afraid I haven't a clue with the filesystem structure in the android apps. I somehow doubt that they would change it so that it would be horrendously difficult though, so I would presume that its either a linux structure, or even better you may have an environment path for it to write to.

    Java in particular I find is very easy to read/write files from. Here's a quick code block I found for using the ObjectOutputStream. Depending on what this data is depends on how you want to write it; object output stream relies on serializable data, so it works great for reading and writing complete objects or even collections of objects that are serializable to file in their entire representation. The con of course its its a lot larger than just a flat file of text: http://www.javadb.com/writing-object...ctoutputstream
    Neither flat file or object stored data will hinder what you want to do. Choose whichever for simplicity (I'd go Object route since then I don't have to ATOI to type again).

  • #3
    New Coder
    Join Date
    Apr 2012
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    I'm afraid I haven't a clue with the filesystem structure in the android apps. I somehow doubt that they would change it so that it would be horrendously difficult though, so I would presume that its either a linux structure, or even better you may have an environment path for it to write to.

    Java in particular I find is very easy to read/write files from. Here's a quick code block I found for using the ObjectOutputStream. Depending on what this data is depends on how you want to write it; object output stream relies on serializable data, so it works great for reading and writing complete objects or even collections of objects that are serializable to file in their entire representation. The con of course its its a lot larger than just a flat file of text: http://www.javadb.com/writing-object...ctoutputstream
    Neither flat file or object stored data will hinder what you want to do. Choose whichever for simplicity (I'd go Object route since then I don't have to ATOI to type again).
    i can honestly say i read that and have no idea what to do
    the files in my android program are all html documents anyway

    may i show you a script i have and you can explain where i put the codes
    i never made javascript cookies before its just a foreign language to me so you think you can tell me a step by step?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I can't really help if its Javascript as I haven't written that in years. If its JS then the javascript guys can help you better. If its java then I can take a look yeah.

  • #5
    New Coder
    Join Date
    Apr 2012
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    I can't really help if its Javascript as I haven't written that in years. If its JS then the javascript guys can help you better. If its java then I can take a look yeah.
    is there a way to help me save all the info for the next time the calc reloads



    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Calc</title>

    <script type="text/javascript">

    function calculate() {
    var one = document.getElementById("one").value*1;
    var two = document.getElementById("two").value*1;
    var three = document.getElementById("three").value*1;
    document.getElementById("answer").value=((one-two)/three).toFixed(2);
    ;
    }
    function roundNumber(num, dec) {
    var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
    return result;
    }

    </script>
    </head>
    <body>
    Box 1
    <br>
    <br><input type="text" id="one" /><hr>Box 2<br><br><input type="text" id="two" /><br><br><hr>Box 3r<br><br><input type="text" id="three" /><br><br><button onclick="calculate();">Calculate</button> <br id="result"></center></br> Total <input type="text" readonly="readonly" id="answer" /><br><br></center>

    </body>
    </html>

    </td>
    </tr>
    </table>
    </center>
    </body>
    </html>

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    You need to use a cookie. By default, Android phones will accept browser cookies from web sites (although the user may of course choose to disable this). Cookies have been covered very many times in this forum - try using the search feature.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    New Coder
    Join Date
    Apr 2012
    Posts
    38
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    You need to use a cookie. By default, Android phones will accept browser cookies from web sites (although the user may of course choose to disable this). Cookies have been covered very many times in this forum - try using the search feature.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    can we use something else that you know better, ive been searching these forums, i joined about 10 code forums and asked atleast 200 people, started up atleast 100 forums, googled, youtubed, no one can seem to help

  • #8
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,742
    Thanks
    41
    Thanked 191 Times in 190 Posts
    I wouldn't use a cookie- you are banking then on hitting a service that is not nes there- the file system the android has should be able to be used- moreover I would code it so that *if there is an external chip (micro SD) then save, otherwise default to 0. you can also have it so exiting does not really exit, it just hides the activity- sleeps it and pulls the other stuff to the foreground- think of the email client; it is 'closed' but it is still open and excepting PUSH. once the program closes, any global variable is lost unless stored in the flash memory-first hit on google of 'save to android NAND Java' ... the file system in use is called NAND; the language breaks down on the stack to Java- so it is native Java

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    If its pure Javascript, that is all you have to work with. The code you have here is javascript and HTML, so you are limited to using cookies.

    Java apps can however be written for android. I don't do mobile programming, but that I know for sure. I also know that both swing and awt are not available for mobile apps, and I haven't a clue what is used (I recall looking into this when it was first released, but can't recall what it was called). The java API for ME will probably have better information on that. I used MIDP way back, but don't know if that's still used.

    So if you stick with Javascript (I don't know if this is a valid application or if its just a website being surfed on), then cookies are your option. If you create an application with Java, you have storage that can be used, and can be pushed to larger bounds like using soap to read/write from a web accessible database (don't allow direct connections of course, just use soap over tls).

  • #10
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,742
    Thanks
    41
    Thanked 191 Times in 190 Posts
    I do some android coding (have been delving into it slowly) and from my XP an html/CSS solution would not be an app on the device- it would simply be a site viewed and would be hosted somewhere- to the best of my knowledge the android cannot host and serve websites... it can process http protocols, but not actual HTML (as it is being served in an HTML environment)... I guess what I am saying is (I didn't look at the code posted until now) that the code posted will never work on an android as an 'app' it will work as being served to the android, but then you are coding to a server and saving/writing to the file-system breaks down to the OS you are running on.

    If I am understanding what it is you want to do correctly you do not want to approach this with any HTML/CSS/Javascript.... as far as you are concerned Javascript is off limits- don't confuse yourself with android : activity and Javascript scripting.... I would for the moment code it to hide itself- and then bring itself to the forefront... my approach would be that on initialization it looks for an active running process, and if it exists wake it up- and then on close hide itself... by the time you figure that out you will already know how to write to the android file structure and answer your own Q

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •