0

私には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();
            }
4

1 に答える 1

1

ブラウザでURLにアクセスし、PHPスクリプトで次の行にエラーが表示されないことを確認してください(2番目のアプリケーションでJSON入力を指定していないため)。

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

また、 lat / lonをサーバーに保存して、呼び出し間で保持されるようにするにはどうすればよいかを聞いていただければ幸いです。

たとえば、次の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
于 2012-05-03T01:08:15.817 に答える