应用程序如何保存数据 [英] How do apps save data

查看:93
本文介绍了应用程序如何保存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,也可能是重复的问题.但是,我真的不知道,也根本无法找到解决方案.我的问题是,应用程序如何保存数据.我正在使用LibGDX和Java开发适用于Android设备的应用程序,只是对此进行了深思.例如,当我关闭Candy Crush并在第二天重新打开它时,即使应用程序重置了,它也能记住我在哪里吗?如果确实是重复的,我将非常感谢其他论坛,并为重复而深表歉意.我只是找不到一个.预先感谢!

This might be a stupid question, and this might also be a duplicate. But, I really do not know and have not been able to find the solution to it, at all. My question is, how do apps save data. I'm making an app with LibGDX and Java for Android devices, and am just stuck pondering on this. How is it that, for example, when I close down Candy Crush and reopen it the day after, it remembers where I'm at even though the app resets? If this does happen to be a duplicate, I would strongly appreciate the other forum, and apologize for duplicating. I just haven't been able to find one. Thanks in advance!

推荐答案

这是一个广泛的问题,但我尝试介绍该主题的视图元素.如果您不注册帐户或将帐户与Facebook之类的内容关联,然后重新安装该应用程序,则Candy Crush Sage会重置自身.他们很可能会使用数据库来存储内容.如果您未注册,则内容可能只会存储在本地.

This is a broad question but I try to cover a view elements on the topic. Candy Crush Sage resets itself if you do not register or associate your account with something like Facebook and reinstall the app. They most likely use a database to store things in. If you are not registered things might just be stored locally.

LibGDX提供了一个方便的功能,可以在本地存储数据.它称为偏好设置",您只需对其进行写操作,然后在下次打开应用程序时,它仍然存在.

LibGDX offers a handy feature to store data locally. It's called Preferences and you simply write to it and the next time you open your app it persists.

Preferences pref = Gdx.app.getPreferences("somefile");
pref.putString("name", "Menyo");
pref.putInteger("level", 20);

但是,当您卸载该数据时,它也随之而来,对于喜欢调查"的玩家来说,这也不是很安全.除此之外,您还可以将自己创建的数据存储在某个地方.如果将其存储在内部应用程序外部,即使删除该应用程序后,它仍然存在,只要数据保留并且重新安装后您的应用程序在相同位置,您应该能够检索数据.但是正如您可能期望的那样,这也不是防故障的.

However when you uninstall this data goes with it and it's neither very secure for players that like to "investigate". Other then that you can store your own created data in some place. If you store it outside the internal app it persists even after deleting the app and as long the data stays and your app looks in the same location after a reinstall you should be able to retrieve the data. But as you might expect this is not fail-proof either.

确保唯一的真正方法是拥有某种登录机制.最可行的方法是数据库.我现在有一个类似的系统正在运行,但是客户端和数据库之间有一个服务器.您可以将任何数据存储在数据库中,并通过其ID将其链接到播放器.

The only real way to make sure is to have somekind of login mechanism. The most viable way is a database. I have a similar system running now but there is a server in between the client and the database. You can store any data in the database and link it to the player by his ID.

  • 新玩家可以选择以客人身份玩.这将创建一个以"Guest" + id作为名称的新帐户.当玩家将其帐户链接到Facebook或电子邮件地址之类的帐户时,该帐户将保留下来,链接到该帐户的数据也将保留.

  • New players can opt to play as a guest. This creates a new account with "Guest" + id as a name. When a player links it's account to something like Facebook or a email address the account will persist and so will the data linked to it.

现有播放器将在安装您的应用程序时登录,您可以在其中存储登录信息,以便他们在存在该数据且有效的情况下自动登录.

Existing players will login when they install your app and you can store there login information on there device so they automatically login if that data exists and is valid.

要设置数据库,我强烈建议不要通过客户端设备直接连接到数据库.拥有Web服务,或者好像我当前正在使用KryoNet服务器.原因是您需要清理来自客户端的数据.您可以从客户端设备执行此操作,但是您不能完全控制该客户端设备.始终在服务器端清理用户输入.

To setup a database I strongly advice to never connect directly to the database through the clients device. Either have a webservice or like I am currently doing a KryoNet server. The reason here is that you need to sanitize data coming from clients. You could do this from a clients device but you do not have full control over a that. Do your sanitizing of user input always on the server side.

如果您了解一些PHP和mysql,创建一个通过数据库连接的Web服务非常容易.您只需要向该php页面发送HttpRequest并让其完成工作即可.

Creating a webservice to connect trough a database is pretty easy if you know a bit of PHP and mysql. All you need is sending a HttpRequest to that php page and let it do the work.

public class WebTest {
    HttpRequestBuilder builder;
    Net.HttpRequest request;

    public WebTest()
    {
        builder = new HttpRequestBuilder();
        request = builder.newRequest().method(Net.HttpMethods.GET).url("http://yourdomain.com/script.php").build();
        request.setHeader("Content-Type", "application/x-www-form-urlencoded");


        final long start = System.nanoTime();
        Gdx.net.sendHttpRequest(request, new Net.HttpResponseListener() {
            @Override
            public void handleHttpResponse(Net.HttpResponse httpResponse) {
                Gdx.app.log("Test", "HTTP Response code: " + httpResponse.getStatus().getStatusCode());
                Gdx.app.log("Test", "HTTP Response code: " + httpResponse.getResultAsString());
                Gdx.app.log("Test", "Response time: " + ((System.nanoTime() - start) / 1000000) + "ms");
            }

            @Override
            public void failed(Throwable t) {
                Gdx.app.log("Test", "HTTP request failed");
            }

            @Override
            public void cancelled() {
                Gdx.app.log("Test", "HTTP request cancelled");
            }
        });
    }
}

yourdomain.com中放置一个名为script.php的php脚本,并在其中回显某些内容.然后这段代码应该会在您的应用程序内收到回声.

Put a php script called script.php in yourdomain.com and echo something out in it. Then this code should receive that echo inside your app.

您还可以创建xml并使用您喜欢的解析器对其进行解析.但是您必须设置请求的标头才能正确接收xml.

You can also create xml and parse that with your favorite parser. But you have to set the header of the request to receive xml properly.

request.setHeader("Content-Type", "text/xml");

这是一个PHP MySQL连接,它以XML发送反馈.

Here is a PHP MySQL connection that sends feedback in XML.

<?php
$link = mysqli_connect("127.0.0.1", "root", "", "chatapp");
$xml = new SimpleXMLElement("<?xml version=\"1.0\" encoding=\"utf-8\" ?><mysql></mysql>");
if (!$link)
{
    $xml->addChild("error", "Error: Unable to connect to MySQL.");
    $xml->addChild("error", "Debugging errno: " . mysqli_connect_errno());
    $xml->addChild("error", "Debugging error: " . mysqli_connect_error());
    exit;
}

$xml->addChild("success", "Success: A proper connection to MySQL was made!");
$xml->addChild("success", "Host information: " . mysqli_get_host_info($link));

//Sends the xml to whoever requested this
print($xml->asXML());

mysqli_close($link);
?>

现在,您可以像使用常规网页一样简单地使用PHP将用户登录到数据库中.有很多涵盖该主题的php教程.当然,您可以为此使用任何服务器端脚本.

Now you can simply use PHP to login a user into the database like you would do with a regular web page. There are plenty of php tutorials that cover this subject. Of course you can use any server side scripting for this.

您自己的服务器也可以连接数据库,但需要做很多工作.但是KryoNet需要您做很多工作.您自己的服务器确实意味着您可以将数据存储在服务器上,并通过TCP或UDP连接非常快速地交换数据.您基本上可以控制要交换的内容.

Your own server to connect to a database is possible too but requires a lot more work. But KryoNet takes a lot of work from you. Your own server does mean you can store data on the server and exchange those very fast through a TCP or UDP connection. You basically have much more control over what is being exchanged.

这篇关于应用程序如何保存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆