[英]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.