0

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

0

3 Answers 3

2

Yes you should get that exception in inputLine. To know I recommend you to debug your code.

As a solution try the below code.

 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();
Sign up to request clarification or add additional context in comments.

1 Comment

thanks JJPA, it works and i appreciate your effort. i now know inputLine return null and cause the error. i also put my working code under my question so other can benefit. thanks to other guy who have also help.
0

Use flower bases for while block. Otherwise you are using a null inputLine after the while block. That is because you are leaving the loop when inputLine is null. And hence, when tried to use the same, it threw a 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

Comments

0

You are getting NullPointerException because your inputLine is NULL. You are running the loop until the inputLine is NULL and then after the loop is terminated, you are using that NULL variable to get the php result. Instead, store it in a temporary variable, either String or array according to your need.

For example, if you need to store it in a string, you can do it as follows

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

And then use the variable temp to access the result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.