Tiago
Tiago

Reputation: 1116

How to return PHP array and receive it with Java

I have this on my webservice:

 function listar($username)
{
    $result = mysql_query("SELECT Name FROM APKs WHERE Tipo=0");
    //$registo = mysql_fetch_array($result);

    $numero = 0;
    while($registo = mysql_fetch_array($result))
    {
        $regs[$numero] = $registo['Name'];
        $numero++;
    }

    return $regs;

    //return mysql_fetch_array($result);

}

in Java, after the SOAP call (not relevant now) I read it this way:

Object response = envelope.getResponse();
String x = response.toString();

I need to access which one of those fields (selected from the database) so I thought, why not split the array into strings?

I tried two methods:

String[] arr=x.split(" ");
        System.out.println("Array :"+arr.length);
        for(int i=0;i<arr.length;i++)
        {
            ..
        }

        StringTokenizer stArr=new StringTokenizer(x," ");
        while(stArr.hasMoreTokens())
        {
            ...
        }

But none of them worked, whick make me believe I'm returning badly the array in first place.

Any help?

UPDATE:

How can I extract those dynamic values and assign them to String[]?

Upvotes: 1

Views: 2553

Answers (2)

armandomiani
armandomiani

Reputation: 730

USE:

json_encode($array); in PHP

and

JSONObject in Java to read.

See this example: http://www.androidcompetencycenter.com/2009/10/json-parsing-in-android/

UPDATE

Change the line:

$regs[$numero] = $registo['Name'];

to:

$regs[] = array('name' => $registo["name"]);

If you need to get the ID, you also can do:

$regs[] = array('name' => $registo["name"], 'id' => $registo["id"]);

FORGET:

The mysql_fetch_array returns a real array not a string. It's not needed to split a string.

  1. Be sure that your PHP Web Service is returning a xsd:array
  2. Generate a new proxy using some generator like http://www.soapui.org. Just use the proxy. Nothing else.

Upvotes: 2

Sergii Zagriichuk
Sergii Zagriichuk

Reputation: 5399

Try to use JSON and you can esy build easy object and read it.

Upvotes: 0

Related Questions