2012-05-02 48 views
0

我有两个应用程序,一个发送很长。和纬度坐标到一个PHP文件,另一个应用程序检索长。和拉特。 COORDS。为了测试并看看我能否第一次工作,我创建了一个函数,我发布了纬度和长度。协调两个PHP服务,并在同一个应用程序中找回它们。我把它们放在烤面包里,看它是否有效。我甚至实现了位置监听器来上传坐标并在同一应用程序中检索它们,然后再尝试在另一个应用程序中接收它们。它工作正常。但是当我尝试在另一个应用程序中使用相同的代码来接收坐标时,我会收到空​​白坐标。我调试了它,它只是空白,好像当我从另一个应用程序调用服务器时,它将清除php服务中的当前值。Android:从PHP获取空字符串网页

代码放置坐标应用之一:

public void postData(String longCord, String latCord) throws JSONException{ 
    // Create a new HttpClient and Post Header 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost(".../android/serverFile.php"); 
    JSONObject json = new JSONObject(); 

    try { 
     // JSON data: 
     json.put("longitude", longCord); 
     json.put("latitude", latCord); 
     JSONArray postjson=new JSONArray(); 
     postjson.put(json); 

     // Post the data: 
     httppost.setHeader("json",json.toString()); 
     httppost.getParams().setParameter("jsonpost",postjson); 
     // Execute HTTP Post Request 
     //System.out.print(json); 
     HttpResponse response = httpclient.execute(httppost); 

     // for JSON: 
     if(response != null) 
     { 

      InputStream is = response.getEntity().getContent(); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      try { 
       while ((line = reader.readLine()) != null) { 
        sb.append(line + "\n"); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } finally { 
       try { 
        is.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
            JSONObject jsonObj = new JSONObject(jsonStr); 
       // grabbing the menu object 
       String longitudecord = jsonObj.getString("lon"); 
       String latitudecord = jsonObj.getString("lat"); 

       Toast.makeText(getApplicationContext(),longitudecord,Toast.LENGTH_SHORT).show(); 
       Toast.makeText(getApplicationContext(),latitudecord,Toast.LENGTH_SHORT).show(); 
     } 

    }catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
    } 
} 

php文件:

<?php 
$json = $_SERVER['HTTP_JSON']; 
$data = json_decode($json); 
$lon = $data->longitude; 
$lat = $data->latitude; 
$variable = array('lon' => "$lon", 'lat' => "$lat"); 
// One JSON for both variables 
echo json_encode($variable); 

>

现在,当我运行其他应用程序的代码...它?与上面相同减去张贴坐标......我得到lon:“”和lat:“”。有点像通过使请求以某种方式擦除其他应用程序发布的信息。是这样吗?

public void recieveData() throws JSONException{ 
     // Create a new HttpClient and Post Header 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(".../android/serverFile.php"); 
     try { 
      HttpResponse response = httpclient.execute(httppost); 
      // for JSON: 
      if(response != null) 
      { 
       InputStream is = response.getEntity().getContent(); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
       StringBuilder sb = new StringBuilder(); 
       String line = null; 
       try { 
        while ((line = reader.readLine()) != null) { 
         sb.append(line + "\n"); 
        } 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } finally { 
        try { 
         is.close(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 
       String jsonStr = sb.toString(); 
       JSONObject jsonObj = new JSONObject(jsonStr); 
       // grabbing the menu object 
       String longitudecord = jsonObj.getString("lon"); 
       String latitudecord = jsonObj.getString("lat"); 

       Toast.makeText(getApplicationContext(),longitudecord,Toast.LENGTH_SHORT).show(); 
       Toast.makeText(getApplicationContext(),jsonStr,Toast.LENGTH_SHORT).show(); 
      } 
+0

“... /安卓/ serverFile.php”看起来不像一个有效的URL给我 – lenik

+0

我把省略号在这个网站让我将不会分享我的文件托管的位置...该网址的作品。发布和获取第一个应用程序的作品。它是我得到空白的第二个。 – inspired

+0

尝试打你的桌面浏览器中的网址,看看页面 – Akram

回答

1

尝试访问您的网址在浏览器,并确保你的PHP脚本不会在以下行给出任何错误(因为你没有第二个应用程序提供任何JSON输入):

$data = json_decode($json); 
$lon = $data->longitude; 
$lat = $data->latitude; 

此外,它会很高兴听到,你怎么样保存你的纬度/经度在服务器上,所以他们坚持在通话之间。

例如,你可以使用这个PHP代码:

$db_host = "localhost"; 
$db_name = "********"; 
$db_user = "********"; 
$db_pass = "********"; 

// connect to MySQL 
$db = mysql_connect($db_host, $db_user, $db_pass); 
if(!$db) { 
    die('Unable to connect to server : ' . mysql_error()); 
} 

// select the DB 
if(!mysql_select_db($db_name)) { 
    die('Unable to select DB : ' . mysql_error()); 
} 

// create table, if not exists 
if(!mysql_query("CREATE TABLE IF NOT EXISTS `locations` (
    `id` int(11) NOT NULL auto_increment, 
    `latitude` double NOT NULL COMMENT 'latitude', 
    `longitude` double NOT NULL COMMENT 'longitude', 
    PRIMARY KEY (`id`) 
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;" 
)) { 
    die('Unable to create table : ' . mysql_error()); 
} 

// save values 
mysql_query("INSERT INTO locations (`latitude`, `longitude`) VALUES ('$lat', '$lon')"); 

// read them back 
$track = array() 
$result = mysql_query("SELECT * FROM locations"); 
while($data = mysql_fetch_assoc($result)) { 
    $track[] = array(
     'latitude' => $data['latitude'], 
     'longitude' => $data['longitude']); 
} 

// here you may convert `track` into JSON/XML and send coordinates list to your application 
+0

哈哈..我认为这是问题。没有错误。我认为我在第一个应用程序中提供的JSON输入将保存在php文件的变量$ lon和$ lat中。我怎样才能在服务器上保存纬度/经度,以便它们可以保持?我必须使用数据库吗? – inspired

+0

将MySQL代码添加到答案 – lenik

+0

非常感谢!这很好。随着应用程序#1更新lon/lat,它会创建新的行。我想逐个发送坐标到应用程序#2。如何按行解析表格行...每次运行检索代码时只会递增。这是做这件事的最好方法吗? – inspired