gpt4 book ai didi

java - 如何将值从java传递给PHP?

转载 作者:可可西里 更新时间:2023-10-31 23:16:26 26 4
gpt4 key购买 nike

我正在尝试将值从 java 类传递到服务器上的 PHP 文件以测试该值并回复 java.lang.它返回空值的问题。

Java代码

package phpJava;
import java.io.*;
import java.net.URL;
import javax.swing.JOptionPane;
public class phpbirgde {
public static void main(String[] args) {
try{
String erg = JOptionPane.showInputDialog("2+3");
BufferedReader br = new BufferedReader(new InputStreamReader(new URL("http://localhost//test.php?test="+erg).openStream()));
String b = br.readLine();
System.out.println(b); // print the string b
if(b.equalsIgnoreCase("true")){
System.out.println("It is true");
}
else {
System.out.println("False");
}

} catch(IOException e){
System.out.println("error");
}
}

}

PHP代码

 <?php
$test = $_GET['test'];
if($test =="5"){
echo "true";
}
else {
echo "false";
}
?>

最佳答案

contract :

public String readLine() throws IOException

Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

Returns:

A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached

在你的例子中,php 脚本只是将 true 字符串(没有 \n)输出到输出流中并关闭连接。

因此,在PHP端添加\n (echo "true\n";),或者使用其他方法从Java端读取响应。

关于java - 如何将值从java传递给PHP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41420944/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com