简体   繁体   English

JAVA中的PHP脚本获取数据

[英]Get data from PHP script in JAVA

I am fairly new to Android programming and was wondering how I can get data from an SQL database in my Android app.我是 Android 编程的新手,想知道如何在我的 Android 应用程序中从 SQL 数据库获取数据。

I currently have a PHP script that pulls the data I want from the SQL table but I'm not sure how to pull the data from the PHP script into my Java. How do I do this?我目前有一个 PHP 脚本可以从 SQL 表中提取我想要的数据,但我不确定如何将 PHP 脚本中的数据提取到我的 Java 中。我该怎么做? I read something about SOAP. Is this the protocol I want to use?我读到一些关于 SOAP 的内容。这是我想要使用的协议吗?

Thanks谢谢

It depends.这取决于。 Where's the database, on the device, or on a server?数据库在哪里,在设备上还是在服务器上? If it's on the server, is the PHP code already a web app, or is it just a script?如果它在服务器上,PHP 代码是否已经是一个 web 应用程序,或者它只是一个脚本?

In general, if the database is on the device, throw out the PHP and use JDBC to grab the data directly from Java. If the data is on the server, then turn the PHP script into a web app, and access that web app from Java. SOAP is certainly one protocol you can use for this, albeit a complex one that's often overkill.一般来说,如果数据库在设备上,就把PHP丢掉,用JDBC直接从Java上抓取数据。如果数据在服务器上,那就把PHP的脚本变成web的app,从88365508685086访问那个883695885088的app Java。SOAP 当然是您可以为此使用的一种协议,尽管它是一种复杂的协议,通常有点矫枉过正。 JSON or just plain text are many times better choices. JSON 或纯文本是好很多倍的选择。

You can use the function below to get content from your PHP script您可以使用下面的 function 从您的 PHP 脚本中获取内容

    public static String get(String from) {
        try {
            HttpClient client = new DefaultHttpClient();  
            HttpGet get = new HttpGet(from);
            HttpResponse responseGet = client.execute(get);  
            HttpEntity resEntityGet = responseGet.getEntity();  
            if (resEntityGet != null) return EntityUtils.toString(resEntityGet);
        } catch (Exception e) {
            Log.e("", e.toString());
        }
        return null;
    }

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

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