Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-29-2012, 10:01 PM   PM User | #1
ceerup
New Coder

 
Join Date: Apr 2012
Posts: 38
Thanks: 1
Thanked 0 Times in 0 Posts
ceerup is an unknown quantity at this point
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?
ceerup is offline   Reply With Quote
Old 04-30-2012, 03:41 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,659
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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).
Fou-Lu is offline   Reply With Quote
Old 04-30-2012, 03:58 AM   PM User | #3
ceerup
New Coder

 
Join Date: Apr 2012
Posts: 38
Thanks: 1
Thanked 0 Times in 0 Posts
ceerup is an unknown quantity at this point
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?
ceerup is offline   Reply With Quote
Old 04-30-2012, 04:28 AM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,659
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 04-30-2012, 05:22 AM   PM User | #5
ceerup
New Coder

 
Join Date: Apr 2012
Posts: 38
Thanks: 1
Thanked 0 Times in 0 Posts
ceerup is an unknown quantity at this point
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>
ceerup is offline   Reply With Quote
Old 04-30-2012, 07:29 AM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 04-30-2012, 09:35 AM   PM User | #7
ceerup
New Coder

 
Join Date: Apr 2012
Posts: 38
Thanks: 1
Thanked 0 Times in 0 Posts
ceerup is an unknown quantity at this point
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
ceerup is offline   Reply With Quote
Old 04-30-2012, 06:54 PM   PM User | #8
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
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
alykins is offline   Reply With Quote
Old 04-30-2012, 06:54 PM   PM User | #9
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,659
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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).
Fou-Lu is offline   Reply With Quote
Old 04-30-2012, 07:04 PM   PM User | #10
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
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
alykins is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:41 PM.


Advertisement
Log in to turn off these ads.