简体   繁体   English

将php数据发送到Java桌面应用程序

[英]Sending php data to java desktop application

I would like to send data from a php script to a java desktop application. 我想将数据从php脚本发送到Java桌面应用程序。 The php data is retrieved from a database in a SELECT * FROM xxxxx. 从SELECT * FROM xxxxx的数据库中检索php数据。 I would like to pass these values to java for displaying. 我想将这些值传递给java进行显示。

I know I can receive data from php in java through the following statement 我知道我可以通过以下语句从Java中的php接收数据

String buffer; 字符串缓冲区;

    in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
    while((buffer = in.readLine()) != null){
        System.out.println(buffer);
    }

However what if I want to capture specific values like firstname , lastname, age, occupation etc etc. How can I do that in java not android. 但是,如果我想捕获特定的值,例如名字,姓氏,年龄,职业等,该怎么办。如何在Java(而非Android)中做到这一点呢?

Any help would be much appreciated! 任何帮助将非常感激!

Use JSON as a structure to transport your data. 使用JSON作为传输数据的结构。 In you PHP script have something like this, if you are using PDO 如果您使用的是PDO,则PHP脚本中应具有以下内容

$pdo=new PDO("mysql:dbname=database;host=127.0.0.1","user","password");
$statement=$pdo->prepare("SELECT * FROM table");
$statement->execute();
$results=$statement->fetchAll(PDO::FETCH_ASSOC);
$json=json_encode($results);
echo $json;

This is the way I do it in my Android apps. 这就是我在Android应用程序中执行此操作的方式。

Android has JSON libraries to help you process the JSON. Android具有JSON库来帮助您处理JSON。

Use Json to do it , first you need to get data from table 使用Json进行操作,首先需要从表中获取数据

       $dbname  = "dbname";
        $host       = "localhost";
        $user       = "root";
        $pass       = "";
        $con        = new PDO("mysql:host=$host;dbname=$dbname",$user,$pass);
        $select     = $con->prepare("select * from your_tablename");
        $execute    = $select->execute();
        foreach ($select as $selects) {
             $fname[]   = array("fname"=>$selects['fname']);
             $lname[]   = array("lname"=>$selects['lname']);
             $age[]     = array("age"=>$selects['age']);     
        }
        $new_array      = array_merge($fname,$lname);
        echo json_encode($new_array);

and in Java side you need to decode the json and use multi dimenssional array to retrieve the values 在Java端,您需要解码json并使用多维数组来检索值

  eg : array[0]['fname'].

Hope it will help you 希望对您有帮助

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

相关问题 将数据直接从浏览器发送到Java Desktop应用程序 - Sending data directly from Browser to Java Desktop application 在java中将推送通知发送到桌面应用程序 - Sending Push Notification to Desktop application in java 使用Java将数据发送到PocketSphinx for Windows桌面 - Sending data to PocketSphinx for Windows desktop in Java 从Java桌面应用程序向Google Analytics发送统计信息 - Sending statistics to Google Analytics from java desktop application 将数据从android传输到Java桌面应用程序 - Transfer data from android to java desktop application 从GPRS模块向桌面应用程序中的MySQL数据库发送数据 - Sending data to a MySQL database in a desktop application from a GPRS module PHP将数据发送到Java守护程序 - PHP sending data to Java daemon 如何在Java桌面应用程序和Web应用程序PHP之间创建通信? - How to create a communication between Java Desktop Application and Web Application PHP? 将数据从Java程序发送到油脂应用程序? - Sending data to greasemonkey application from java program? 将数据从树莓派发送到 java 应用程序? - Sending data from a raspberry pi to a java application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM