gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:16:33 26 4
gpt4 key购买 nike

我的问题是我们如何使用java在网络上存储来自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".

下面是我当前的代码

    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但对理解代码感到困惑。也许这里的任何好人都可以为我提供有关如何将 php 文件中的 echo 数据存储到我的 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

正如我所愿

最佳答案

是的,您应该在 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();

关于java - 如何使用java存储从php文件检索的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21397422/

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