2013-08-06 159 views
1

我已经创建了一个http发布请求,用于连接android中的PHP服务器。 但在服务器侧我不能从阵列从Android获取发布数据的PHP

提取数据,这是用于制造http请求

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

在我loged蚀的代码的所有values.it正在打印 和I调试“对”将打印使用

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

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

在PHP中我试图让这个数据

但iam没有获得价值。有什么问题?有没有人可以帮助我..请回复.. Thanx提前:)

+0

@Nirmal其实我问题是 - 从android发送的数据是一个数组位置[] [] == xxxx 如何从该数组中提取此数据。那是我的问题 – SibinF

+0

试图跟随桑德斯的答案。你应该将你的参数转换为JSON。 – Nirmal

+0

您可以对提交的数据执行var_dump($ _ POST)并向我们显示结果。我不认为你可以发送这种类型的数据,而不需要先序列化它 – Drew

回答

3

您可以尝试将您的参数转换为JSON,然后将它们发布到PHP。

+0

我们如何将它转化为json?你能举一个例子吗? – SibinF

0

我做了这样的事情,我猜你知道如何通过PHP将数据发送到服务器, 试试这个,在你的PHP,

  $lat=$_POST['latitude']; 
      $long=$_POST['longitude']; 
      $speed=$_POST['speed']; 
      $time=$_POST['time']; 
+0

我试着相同的代码yar ..但问题是在PHP它的数据获取数组的格式。 [位置[0] [时间] = 1375788271891, 位置[0] [纬度] = 12.966116, 位置[0] [经度] = 77.638493, 位置[0] [速度] = 0.0]然后 – SibinF

+1

尝试获得你的值首先在一个变量中,然后通过它。 –

+0

你的权利..我们如何获得所有的PHP数据?你懂?之后我们可以提取每个值。 – SibinF

0

把自己的价值观数组和回声他们从PHP端。

$arr = array('Name'=> 'Values'); 
echo json_encode($arr); 

您可以尝试针对Android边下面的代码中,“结果”是包含JSON格式结果的字符串,可以使用的JSONObject和JSONArray类解析:

HttpPost httppost = new HttpPost(KEY); 
      HttpParams httpParameters = new BasicHttpParams(); 

      if(nameValuePairs!=null) 
      { 
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      } 

      // Set the timeout in milliseconds until a connection is established. 
      // The default value is zero, that means the timeout is not used. 
      int timeoutConnection = 8000; 
      HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); 
      // Set the default socket timeout (SO_TIMEOUT) 
      // in milliseconds which is the timeout for waiting for data. 
      int timeoutSocket = 8000; 
      HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); 

      //Still dont know the reason why this code was implemented, will have to find out that!! 
      HttpClient httpclient = new DefaultHttpClient(httpParameters); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      is = entity.getContent(); 

      //Reads the PHP output of JSON and writes it into a String using StringBuilder 
      BufferedReader buffer = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
      StringBuilder sb = new StringBuilder(); 

      while ((line = buffer.readLine()) != null) 
      { 
       sb.append(line + "\n"); 
      } 

      result = sb.toString();