I need to save user-entered data like name and age to a text file on Android, in order that I can then save it to MySQL. How can I do this? Any help please 开发者_JAVA技巧thank you.
Filesystems of Android phone/emulator and of your computer are separate. Even if it's the emulator, there's no directory that both Python code and Android Java can read.
EDIT based on comment:
So when the game goes live, are you still going to use your (or player's) PC as a server? What you're facing is a very common app design; the Python code should reside on a Web service, you should use HTTP on Android to communicate to it, and forget about text files.
Read up on Android's HttpClient class, and on setting up Python on a Web server.
Use FileWriter: http://developer.android.com/reference/java/io/FileWriter.html
See example like this one: http://blog.mynotiz.de/programmieren/java-text-in-eine-datei-schreiben-450/
Presumably he wants to how to load/save the data to the phone's SD card:
Android how to use Environment.getExternalStorageDirectory()
With that you can read the text file that is placed on the SD card by the user. Python and MySQL don't run on android (not on a stock phone anywyay) so if you want to store the data from the text file into Android's built in database, you need to use SQLite - which is provided by the framework:
http://developer.android.com/guide/topics/data/data-storage.html (SQL info is at the bottom)
A good example of how to use the DB is here:
http://www.screaming-penguin.com/node/7742
Your quesiton wasn't really that specific -- but if you need to send the data from the phone to an external database (ie on some server) then it's a whole different ball game. In that case, it is probably easiest to use some kind of URL based call on the server. (ie. REST)
-- Dan
I would add some sort of "web-interface" in front of the MySQL database that takes some content e.g. encoded in XML or JSON. On the Android side, after the user has entered the data, open a HttpConnection to the server and transmit that data.
精彩评论