简体   繁体   English

如何从 Android 应用程序连接 MySQL 数据库?

[英]How do I connect with MySQL database from Android application?

I have been researching answers to this question on various platforms but can't quite put the pieces together.我一直在各种平台上研究这个问题的答案,但无法将各个部分放在一起。

Currently, I have a device hardcoded in C++ that connects to a server and uses a PHP script to input data into a database.目前,我有一个用 C++ 硬编码的设备,它连接到服务器并使用 PHP 脚本将数据输入到数据库中。 From there, I have a PHP script that then connects to the database and taskes the data, creating an XML file.从那里,我有一个 PHP 脚本,然后连接到数据库和任务数据,创建一个 XML 文件。 I then have a Google API that takes the XML file and creates markers from the XML files data.然后我有一个 Google API,它接受 XML 文件并从 XML 文件数据创建标记。

I am now trying to build a Java version of this in my android application.我现在正在尝试在我的 android 应用程序中构建一个 Java 版本。 Since the PHP scrips are already on the LAMP server, and the database is already in the tables on the server.由于 PHP 脚本已经在 LAMP 服务器上,并且数据库已经在服务器上的表中。

How do I go about connecting to the database from the android studio?如何从 android studio 连接到数据库? Is there a way to connect to the server and use my PHP script?有没有办法连接到服务器并使用我的 PHP 脚本?

I have never used JSON, and would like to avoid it and stick to PHP where possible.我从未使用过 JSON,并希望避免使用它并尽可能坚持使用 PHP。

Thanks!谢谢!

If you are using PHP at server side then the only way to connect with database is to use php webservices aka APIs.如果您在服务器端使用 PHP,那么连接数据库的唯一方法是使用 php webservices aka API。 As you stated you have not used json but thats the only way.正如您所说,您没有使用 json,但这是唯一的方法。 To simplify the networking calls, you can use various libraries which will simplify the http requests for you.为了简化网络调用,您可以使用各种库来简化 http 请求。 For android, volley and okhttp are mostly used libraries.对于 android,volley 和 okhttp 是最常用的库。 I personally suggest you to use volley for android.我个人建议您使用 volley for android。 Json parsing using volley is just a piece of cake使用 volley 解析 Json 只是小菜一碟

您需要在这里使用 webservice。为了简单起见,在 android 中使用改造库,它将负责您的 json 输入/输出解析、webservice 调用和处理来自它的响应。

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

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