4

This is a weird problem. Here is my code

 String reply = listen.executeUrl("http://localhost:8080/JavaBridge/reply.php); 

executeUrl returns as String object whatever is returned by the reply.php file. Now comes the problem. In reply.php I am returning an PHP array and reply is a String.

When I do

System.out.println("Reply = "+reply);  

I get

Reply =       array(2) {  [0]=>  string(14) "Dushyant Arora"  [1]=>  string(19
) "@dushyantarora13 hi"}

But reply is still a String. How do I convert it into a String array or an Array.

4 Answers 4

8

You might want to try returning a JSON object in reply.php and then importing that into Java using the JSON libraries.

http://www.json.org/java/

reply.php:

<?
...
echo json_encode($yourArray);

In your Java code:

...
JSONArray reply = new JSONArray(listen.executeUrl("http://localhost:8080/JavaBridge/reply.php"));
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for JSON! Standard format supported in libraries by both languages!
2

There's nothing weird about it at all. You have declared String reply, so of course it's a string. The standard way of splitting a String into a String[] is to use String.split, but I'd seriously consider changing the format of the reply string rather than trying to figure out the regex for the current format, because it's not all that friendly as it is.

Comments

1

You may want to change the behaviour of reply.php, and return a string instead of an array.

Maybe something like

// ...
return implode(" ", $your_reply_array) ;

2 Comments

I need an array on Java side. How do I get that?
Forget that, like thetaiko said JSON is what you need :)
1

Parsing a PHP array with Java is not the cleanest solution, but I can never resist a good regex problem.

public static void main(String[] args) {
    Pattern p = Pattern.compile("\\[\\d+\\]=>  string\\(\\d+\\) \"([^\"]*)\"");
    String input = "      array(2) {  [0]=>  string(14) \"Dushyant Arora\"  [1]=>  string(19" +
            ") \"@dushyantarora13 hi\"}";
    ArrayList<String> list = new ArrayList<String>();
    Matcher m = p.matcher(input);
    while (m.find()) {
        list.add(m.group(1));
    }
    System.out.println(list);
}

[Dushyant Arora, @dushyantarora13 hi]

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.