开发者

Getting an Array from the Server with an Android PHP Web Service

开发者 https://www.devze.com 2023-04-03 13:03 出处:网络
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 ar

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, yc开发者_如何学Pythonoords 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.


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);
0

精彩评论

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