0

For single pair value, I used:

datas.add(new BasicNameValuePair("latitude", Double.toString(lastKnownLocation.getLatitude())));

Then send it to server using HttpClient:

HttpClient httpclient = CustomHttpClient.getHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(datas));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
input = entity.getContent();

At server side, I extract this information as follows:

$latitude = (_POST['latitude']);

But for an array of my own type, says Place with 3 fields:

class Place {
     int id;
     String name;
     String address;
}

How can I send this information to my php script? And within my php, how can I extract this JSON array?

roxrook
  • 13,511
  • 40
  • 107
  • 156
  • The best way is to use JSON I guess. On php side, use json_decode to retrieve data on the php side. There is plenty of example to send json objects from an Android application. – Jeremy D Jan 20 '12 at 04:03

1 Answers1

1

There are countless methods to transfer data, but one of the more common data-interchange methods is via JSON.

See this question for how to do that. In a nutshell, you

  • add JSON support to your "beans," or the elements that you want to transport,
  • convert the relevant beans to JSON, and
  • send an HTTP request to the remote server, including the JSON data.

See qklxtlx's response for how to interpret the JSON on the server side.

Community
  • 1
  • 1
cheeken
  • 33,663
  • 4
  • 35
  • 42
  • Thanks. Could you provide a minimal working example? The link you gave showed how to send data to php server, but the code didn't submit any "key-pair". So at server side, how could I retrieve this JsonArray? – roxrook Jan 20 '12 at 04:24
  • @Chan Check out the other answer in that link, authored by primpop. – cheeken Jan 20 '12 at 04:26
  • @Chan Updated answer with your 2nd question. – cheeken Jan 20 '12 at 04:28