Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用java存储从php文件检索的数据_Java_Php_Arrays - Fatal编程技术网

如何用java存储从php文件检索的数据

如何用java存储从php文件检索的数据,java,php,arrays,Java,Php,Arrays,我的问题是,我们如何使用java在web上存储php文件中的数据 (我可以查看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 th

我的问题是,我们如何使用java在web上存储php文件中的数据 (我可以查看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".
错误输出:

Exception in thread "main" java.lang.NullPointerException
我已经提到了这个问题,但是理解代码有点混乱。也许这里的任何好人都能为我提供关于如何将php文件中的回音数据存储到java数组变量的有效代码

提前谢谢大家

回答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  

就像我想要的一样

在while block中使用花卉底座。否则,您将在while块之后使用
null
inputLine
。这是因为当
inputLine
为空时,您将离开循环。因此,当尝试使用相同的方法时,它抛出了一个
NullPointerException

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"));
   // do whatever you want with this array
} // while

是的,您应该在
inputLine
中获得该异常。要知道,我建议您调试代码

作为解决方案,请尝试以下代码

 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);
    }
    // array below should store input from .php file after i thrown "split"
    // text
    String[] strArray2 = sb.toString().split("split");
    System.out.println(strArray2);
    in.close();

您正在获取NullPointerException,因为您的inputLine为NULL。运行循环直到inputLine为NULL,然后在循环终止后,使用该NULL变量获得php结果。相反,根据需要将其存储在临时变量中,可以是字符串或数组

例如,如果需要将其存储在字符串中,可以按如下方式执行

String inputLine, temp="";
while ((inputLine = in.readLine()) != null){
   temp.concat(inputLine);
   System.out.println(inputLine);
}

然后使用变量temp访问结果。

谢谢JJPA,它可以工作,我感谢您的努力。我现在知道inputLine返回null并导致错误。我还把我的工作代码放在我的问题下面,这样其他人可以从中受益。多亏了其他人的帮助。
String inputLine, temp="";
while ((inputLine = in.readLine()) != null){
   temp.concat(inputLine);
   System.out.println(inputLine);
}