my question is how do we store data from php file at web using java (i can view the php file but cant store it into my array variable). may this benefit other.
//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".
below is my current code
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();
error output:
Exception in thread "main" java.lang.NullPointerException
i have refer to this question, Retrieving info from a file but confuse to understand the code. perhap any good people here can provide me with valid code on how to store echo data from php file to my java array variable.
thanks in advance folk.
ANSWER credit to 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();
output result:
Ridzuan
Malaysia
just like what i wanted