我的问题是我们如何存储数据从网上使用java的PHP文件 (我可以查看PHP文件,但不能存储到我的数组变量)。这可能有利于他人。下面如何存储数据检索从Java文件的PHP文件
//http://sampleonly.com.my/getInfo.php //this url is not exist. just for example
<?php
echo("Ridzuan");
echo("split");
echo("Malaysia");
echo("split");
?>
// i want to get the echo "Ridzuan" and "Malaysia". i dont want echo "split".
是我当前的代码
URL connectURL = new URL("http://sampleonly.com.my/getInfo.php");
BufferedReader in = new BufferedReader(
new InputStreamReader(connectURL.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
//array below should store input from .php file after i thrown "split" text
String[] strArray2 = inputLine.split(Pattern.quote("split"));
in.close();
错误输出:
Exception in thread "main" java.lang.NullPointerException
我已经参考了这个问题,但Retrieving info from a file混淆理解代码。 perhap任何好的人在这里可以提供有效的代码,如何将来自php文件的回声数据存储到我的java数组变量。
提前感谢民间。
ANSWER信贷JJPA
URL connectURL = new URL("http://vmalloc.in/so.php");
BufferedReader in = new BufferedReader(
new InputStreamReader(connectURL.openStream()));
String inputLine;
StringBuilder sb = new StringBuilder();
while ((inputLine = in.readLine()) != null){
System.out.println(inputLine);
sb.append(inputLine);
}
String[] strArray2 = sb.toString().split(Pattern.quote("split"));
System.out.println(strArray2[0]);
System.out.println(strArray2[1]);
in.close();
输出结果:
Ridzuan
Malaysia
就像刚才那
感谢JJPA,它的工作原理,我感谢你的努力。我现在知道inputLine返回null并导致错误。我也把我的工作代码放在我的问题下,以便其他人可以受益。感谢其他也有帮助的人。 – Learner