[英]How to Execute a PHP Script on a Host from a Java Desktop Application
I have written a Java 2D game. 我已经写了一个Java 2D游戏。 It has local and global scores. 它具有本地和全球分数。 Local scores were handled, but for global scores, I need to connect to a PHP script. 处理了本地分数,但是对于全局分数,我需要连接到PHP脚本。
Script is simply like this: 脚本就是这样的:
http://myserver.com/highscore.php?action=submit&name=NAME&score=SCORE&
I can read scores with stream readers, but I can't send new scores to server. 我可以使用流阅读器阅读分数,但是无法将新分数发送到服务器。 I've been looking into outputstreams, but I couldn't handle it. 我一直在研究输出流,但无法处理。
I also checked this link: http://www.iismarconi.net/inside/materiale/java/tutorial/networking/urls/readingWriting.html 我还检查了此链接: http : //www.iismarconi.net/inside/materiale/java/tutorial/networking/urls/readingWriting.html
But no dice. 但是没有骰子。
I've solved the issue, and if anyone needs a solution, I'm writing it here: 我已经解决了这个问题,如果有人需要解决方案,请在这里写:
HttpURLConnection http = (HttpURLConnection) new URL(highscoreGlobal).openConnection();
http.setRequestMethod("GET"); //You don't need to set this because HttpURLConnection works with GET in default, but anyway
http.setReadTimeout( 15*1000); //Timeout, 15 seconds.
http.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader( http.getInputStream()));
And reader.nextLine()
will give you the output. 然后reader.nextLine()
将为您提供输出。 In my case, sql query returns 1 if insertion is successful. 就我而言,如果插入成功,则sql查询返回1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.