开发者

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:

  • S开发者_运维百科o I'm using again xsd:string;

  • Now I have on my webservice return json_encode($regs);

  • To convert the object from the response I'm using a specific google api http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/

    Object response = envelope.getResponse(); Gson gson = new Gson(); String jstring = gson.toJson(response);

  • But I'm with difficulty parsing the "jstring" because it's format: "[\"SOMETHING\",\"SOMETHING\",\"SOMETHING\",\"SOMETHING\",.....]". I have not any identifier to get those values.

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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜