私は2つのアプリケーションを持っています。 phpファイルへの緯度座標、もう一方のアプリケーションはlongを取得します。と緯度。 coords。私が機能を作成する最初の作業を得ることができるかどうかをテストして確認するために、私は緯度と経度を投稿しました。 coords 2つのPHPサービスと私は同じアプリケーションでそれらを戻しました。私はそれがうまくいくかどうかを見るためにトーストに入れました。私は、他のアプリケーションでそれらを受け取ろうとする前に、座標をアップロードして同じアプリケーションで取り出すためにロケーションリスナーを実装しました。それはうまく動作します。しかし、座標を受け取るために他のアプリケーションで同じコードを使用しようとすると、空白の座標が返されます。私はそれをちょうど空白のままデバッグしました。他のアプリケーションからサーバーへの呼び出しを行うと、phpサービスの現在の値が消去されるようです。Android:PHPのWebページから空の文字列を取得する
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();
}
を「... /アンドロイド/ serverFile.phpは、」私はなるように、私はこのサイトに省略記号を入れて – lenik
私には、有効なURLのようには見えません。私のファイルがホストされている場所を共有しません... URLが機能します。最初のアプリケーションの投稿と取得その2番目は私が空白になる場所です。 – inspired
あなたのデスクトップブラウザでそのURLにヒットしようとしたら、ページにあるものを参照してください – Akram