我想從java類的值傳遞到服務器上的PHP文件來測試這個值並回復到java。它返回空值的問題。如何將值從java傳遞給PHP?
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";
}
?>
究竟返回null? –
我已經修復了這個問題,謝謝。它正在返回一個空間給java。這個空間來自於'<?php'標籤之前的'HTML',因爲在我的php文件中它是'<?php'標籤之前的空格,我對它進行了修復。 @ k5_ – Martin