2

I have successfully launched HttpGet with my android app. It connects to the database on the server and I can get query results with PHP. Now I just want to get the result, which in this case is an array, where each row contains two values. (ACtucally X,Y coordinates - I'm getting GPS coords off the server).

My PHP code is:

    ...(assume already connected to Database)

  $query  = "SELECT xcoords, ycoords FROM geo_table WHERE xcoords <> '0' AND ycoords <> '0' ";
 $result = mysql_query($query);

$rows = array();


while ($r = mysql_fetch_array($result, MYSQL_NUM))
{
//printf("X: %s  Y: %s", $r[0], $r[1]); 
$rows[]=$r;
} 

echo json_encode($rows);
 ?> 

The code works. I get my array. And in Android I do this to the HttpGet response:

        JSONObject j = new JSONObject();
        j.put("array", response);

Maybe I should have turned it into a JSONArray? I'm not sure what to do with this JSONObject now. It's really frustrating. My end goal is to make an ArrayList/List of the coords (of Integer type, I suppose - multiplying each by 1E6). But for now I am just stuck staring at this JSONObject. Any help is appreciated.

1
  • It would be helpful if you could include the JSON response from the PHP call to your question so people in SO who know Android but don't know PHP can help you out as well here. Commented Sep 9, 2011 at 7:12

1 Answer 1

1

you can use GSON (http://code.google.com/p/google-gson/) and deserialize the response to a class.

Create a class like this:

public class Geo
{
  public String longitude;
  public String latitude;

}

//use Gson to deserialise

Type collectionType = new TypeToken<Collection<Geo>>(){}.getType();
Collection<Geo> geo = gson.fromJson(response, collectionType);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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