私には2つのアプリケーションがあり、1つは長く送信します。phpファイルへの緯度座標と他のアプリケーションはlongを取得します。と緯度。調整します。テストして最初に機能するかどうかを確認するために、緯度と長さを投稿した関数を作成します。2つのphpサービスを調整し、同じアプリケーションに戻しました。私はそれらをトーストに入れて、それが機能するかどうかを確認しました。他のアプリケーションで座標を受信する前に、同じアプリケーションで座標をアップロードして取得し、テストするためのロケーションリスナーも実装しました。正常に動作します。しかし、他のアプリケーションで同じコードを使用して座標を受信しようとすると、空白の座標を受信します。他のアプリケーションからサーバーを呼び出すと、phpサービスの現在の値が消去されるかのように、デバッグしました。
アプリケーション1に座標を配置するためのコード:
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();
}