我有两份申请,一份我寄得很长。和纬度坐标到 php 文件,其他应用程序检索 long。和纬度。坐标。为了测试,看看我是否能得到第一个工作,我创建了一个函数,我发布了纬度和经度。协调两个 php 服务,我将它们放回同一个应用程序中。我把它们放在吐司里,看看它是否有效。我什至实现了位置监听器来上传坐标并在同一个应用程序中检索它们以进行测试,然后再尝试在另一个应用程序中接收它们。它工作正常。但是当我尝试在其他应用程序中使用相同的代码来接收坐标时,我会收到空白坐标。我调试了它,它只是空白,好像当我从另一个应用程序调用服务器时,它会删除 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();
}