1

I have created a http post request for connecting with Php server in android. but in server side i cant extract data from the array

this is the code for making http request

      List<NameValuePair> pairs = new ArrayList<NameValuePair>(2);
    pairs.add(new BasicNameValuePair("time",
        String.valueOf(location.getTime())));
 pairs.add(new BasicNameValuePair("latitude", new DecimalFormat("#.######").format(location.getLatitude())));
    pairs.add(new BasicNameValuePair("longitude",
            new DecimalFormat("#.######").format(location.getLongitude())));
    pairs.add(new BasicNameValuePair("speed",
        String.valueOf(location.getSpeed())));

   HttpPost post = new HttpPost(endpoint);
   post.setEntity(new UrlEncodedFormEntity(pairs));

In the eclipse i loged all the values.it is printing and i debug "pairs" it will print an array

      [locations[0][time]=1375788271891,
       locations[0][latitude]=12.966116, 
       locations[0][longitude]=77.638493,
       locations[0][speed]=0.0]

In php i tried to get this data using

              $lat=$_POST["latitude"];
              $long=$_POST["longitude"];
              $speed=$_POST["speed"];
              $time=$_POST["time"];

But iam not getting the values. whats the problem? is there aybody can help me..please reply.. Thanx in Advance :)

SibinF
  • 385
  • 1
  • 7
  • 25
  • @Nirmal actually my problem is - the data sending from android is a array location[][]==xxxx how to extract this data from that array. thats my problem – SibinF Aug 06 '13 at 11:39
  • try to follow sanders answer. you should convert your parameters to JSON. – Nirmal Aug 06 '13 at 11:42
  • Can you do a var_dump($_POST) of the data your submitting and show us the results. I don't think you can send that type of data without serializing it first – Drew Aug 06 '13 at 11:44

2 Answers2

3

You could try and convert your parameters to JSON and then post them to PHP.

sanders
  • 10,794
  • 27
  • 85
  • 127
0

I have done something like that, I am guessing you know how to send the data to server through php, Try this, in your php,

          $lat=$_POST['latitude'];
          $long=$_POST['longitude'];
          $speed=$_POST['speed'];
          $time=$_POST['time'];
hemantsb
  • 2,049
  • 2
  • 20
  • 29
  • i tried the same code yar..but the problem is in php it the data is getting in the format of an array. [locations[0][time]=1375788271891, locations[0][latitude]=12.966116, locations[0][longitude]=77.638493, locations[0][speed]=0.0] – SibinF Aug 06 '13 at 12:08
  • 1
    then try to get your values first in a variable, then pass it. – hemantsb Aug 06 '13 at 12:23
  • your right.. how we get all the post data in php ? you know? after that we can extract each values .right? – SibinF Aug 06 '13 at 12:25
  • 1
    Or you can extract your data in java instead, and then pass it to php, Then POST data willnot be the issue. – hemantsb Aug 06 '13 at 12:29