Send data from php to java
I've found quite a few other posts on this topic, but none of them have the answer I need.
I wrote a Bukkit plugin for Minecraft that can send post data to a PHP page and receive a page return.
Now the only thing I can't figure out. I would like the page to have a button and when the button is clicked send the data to the Java plugin and send the plugin a message.
I've seen a thing or two about sockets. But after reading about them, I can't figure out how to set them up.
At any time you can click a button and send data to a Java plugin and I can use that data but I like it.
Does anyone know how I can get the Java plugin to constantly wait for data from the page?
My current code:
(This sends the player's name to the website.)
String re = "";
URL url = new URL("address here");
URLConnection con = url.openConnection();
con.setDoOutput(true);
PrintStream ps = new PrintStream(con.getOutputStream());
ps.print("player=" + player.getName());
con.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
re += line + "\n";
}
rd.close();
ps.close();
And my php just returns whatever data it receives.
It works great, but I would like to listen in my java plugin for data from a php page.
source to share
You can use database
or configure a json/xml
api on the end PHP
and access database
or access json/xml
from Java with this example code to open the url.
URL url = new URL("site.com/api/foo.json");
try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"))) {
for (String line; (line = reader.readLine()) != null;) {
System.out.println(line);
}
}
You can watch this tutorial for parsing JSON from Java.
source to share