简体   繁体   English

如何在Android上将数据保存到文本文件?

[英]How to save data to a text file on Android?

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. 我需要将用户输入的数据(例如名称和年龄)保存到Android上的文本文件中,然后才能将其保存到MySQL。 How can I do this? 我怎样才能做到这一点? Any help please thank you. 任何帮助,请谢谢。

Filesystems of Android phone/emulator and of your computer are separate. Android手机/模拟器和计算机的文件系统是分开的。 Even if it's the emulator, there's no directory that both Python code and Android Java can read. 即使是模拟器,也没有Python代码和Android Java都可以读取的目录。

EDIT based on comment: 根据评论进行编辑:

So when the game goes live, are you still going to use your (or player's) PC as a server? 因此,当游戏上线时,您是否仍将使用(或玩家的)PC作为服务器? 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. Python代码应该驻留在Web服务上,您应该在Android上使用HTTP与之通信,而忽略文本文件。

Read up on Android's HttpClient class, and on setting up Python on a Web server. 阅读Android的HttpClient类,以及在Web服务器上设置Python的内容。

Presumably he wants to how to load/save the data to the phone's SD card: 大概他想了解如何将数据加载/保存到手机的SD卡:

Android how to use Environment.getExternalStorageDirectory() Android如何使用Environment.getExternalStorageDirectory()

With that you can read the text file that is placed on the SD card by the user. 这样,您可以读取用户放置在SD卡上的文本文件。 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: Python和MySQL不能在android上运行(无论如何不能在股票电话上运行),因此,如果要将文本文件中的数据存储到Android的内置数据库中,则需要使用SQLite-该框架提供了该功能:

http://developer.android.com/guide/topics/data/data-storage.html (SQL info is at the bottom) http://developer.android.com/guide/topics/data/data-storage.html(SQL信息在底部)

A good example of how to use the DB is here: 这里是如何使用数据库的一个很好的例子:

http://www.screaming-penguin.com/node/7742 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. 在这种情况下,在服务器上使用某种基于URL的调用可能是最简单的。 (ie. REST) (即REST)

-- Dan -丹

I would add some sort of "web-interface" in front of the MySQL database that takes some content eg encoded in XML or JSON. 我会在MySQL数据库的前面添加某种“ Web界面”,以接收一些内容,例如,以XML或JSON编码。 On the Android side, after the user has entered the data, open a HttpConnection to the server and transmit that data. 在Android方面,在用户输入数据之后,打开与服务器的HttpConnection并传输该数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM