java 在 Android 应用程序中实现客户端 <-> 服务器 <-> 数据库架构的最佳方式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2256082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 20:19:35  来源:igfitidea点击:

Best way to implement Client <-> Server <-> Database architecture in an Android application?

javamysqlandroid

提问by user245120

I am making an Android application. Since it is so simple, I first thought I could simply eliminate the need for Java application on the server which acts as a middleware. I tried directly connecting to the database using the JDBC driver for MySQL but my program is crashing so I'm not sure if Android "supports" the JDBC driver for MySQL.

我正在制作一个 Android 应用程序。由于它是如此简单,我首先想到我可以简单地消除作为中间件的服务器上的 Java 应用程序的需要。我尝试使用 MySQL 的 JDBC 驱动程序直接连接到数据库,但我的程序崩溃了,所以我不确定 Android 是否“支持”MySQL 的 JDBC 驱动程序。

So I am thinking of how to implement the application. Basically the application writes some data from a remote MySQL database and retrieves some data from a remote MySQL database.

所以我正在考虑如何实现应用程序。基本上,应用程序从远程 MySQL 数据库写入一些数据并从远程 MySQL 数据库检索一些数据。

Do I connect to a Java server program using sockets (or some other method of communication)? Or could I implement a direct connection to the MySQL database from the client application?

我是否使用套接字(或某种其他通信方法)连接到 Java 服务器程序?或者我可以实现从客户端应用程序到 MySQL 数据库的直接连接吗?

回答by CommonsWare

I tried directly connecting to the database using the JDBC driver for MySQL but my program is crashing so I'm not sure if Android "supports" the JDBC driver for MySQL.

我尝试使用 MySQL 的 JDBC 驱动程序直接连接到数据库,但我的程序崩溃了,所以我不确定 Android 是否“支持”MySQL 的 JDBC 驱动程序。

Never never never use a database driver across an Internet connection, for any database, for any platform, for any client, anywhere. That goes double for mobile. Database drivers are designed for LAN operations and are not designed for flaky/intermittent connections or high latency.

永远永远永远永远不要通过 Internet 连接、任何数据库、任何平台、任何客户端、任何地方使用数据库驱动程序。这对移动设备来说翻了一番。数据库驱动程序专为 LAN 操作而设计,不适用于不稳定/间歇性连接或高延迟。

Do I connect to a Java server program using sockets (or some other method of communication)?

我是否使用套接字(或某种其他通信方法)连接到 Java 服务器程序?

It doesn't have to be Java. It just has to be something designed for use over the Internet. As Mr. King's comment suggests, Web services have been used for this for much of the past decade. For Android, REST Web services are probably the easiest to consume, since there is no built-in support for SOAP or XML-RPC. But whether the Web service is implemented in Java, or PHP, or Perl, or SNOBOL, is up to you.

它不一定是Java。它必须是专为在 Internet 上使用而设计的东西。正如金先生的评论所暗示的那样,Web 服务在过去十年的大部分时间里都被用于此目的。对于 Android,REST Web 服务可能是最容易使用的,因为没有对 SOAP 或 XML-RPC 的内置支持。但是,Web 服务是用 Java、PHP、Perl 还是 SNOBOL 实现的,则取决于您。

Well, OK, perhaps SNOBOL won't be a viable option. :-)

好吧,也许 SNOBOL 不是一个可行的选择。:-)

回答by Cedric

I know this might be a little late but as I ran into the same problem with a project at school I wanted to share my solution with you as you might profit out of my experiences.

我知道这可能有点晚了,但是当我在学校的一个项目中遇到同样的问题时,我想与您分享我的解决方案,因为您可能会从我的经验中受益。

Android is bad for Database-Operations so creating a normal Database-Controller wasn't a thing. Instead I created a Server in Java which handles all Database-related stuff and can also be extended (in my case I used a Feedback-function, too).

Android 对数据库操作不利,因此创建一个普通的数据库控制器不是一回事。相反,我用 Java 创建了一个服务器,它处理所有与数据库相关的东西,也可以扩展(在我的情况下,我也使用了反馈功能)。

The Github-REPO is: https://github.com/Cedced-Bro/Public-ServerYou can check it out and this is open-source so you can use and contribute to it if you have more ideas to it.

Github-REPO 是:https: //github.com/Cedced-Bro/Public-Server你可以查看它,这是开源的,所以如果你有更多的想法,你可以使用它并为它做出贡献。

To answer your question more properly: I would strongly suggest to NOT grant all users direct access to your DB as you can run into security issues with malicious users. This was the reason why I created this controller in the first place instead of just a PHP "forwarding"-server.

为了更正确地回答您的问题:我强烈建议不要授予所有用户直接访问您的数据库的权限,因为您可能会遇到恶意用户的安全问题。这就是为什么我首先创建这个控制器而不是一个 PHP“转发”服务器的原因。