1

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:

  • So 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[]?

2 Answers 2

2

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

12 Comments

I edited the function listar because it was return a multi dimensional array. I was returning xsd:string indeed. Corrected. Use proxy? Why? And how? I've no idea what you are suggesting.
Proxy it's a design pattern. Some proxy generator will generate Java classes that will the dirt work for you and return a clean Array.
I appreciate your help, but I have no idea how to apply this in my java android client... much because that tutorial is to create a web client.
Ok, you didn't mentioned that you were using Android, so I think the best way it's use a simpler approach like JSON.
|
0

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

1 Comment

One part was specified by @armandomiani and what about android you can use developer.android.com/reference/org/json/JSONTokener.html for this case

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.