こんにちは、Android デバイスに gps デバイスの通信機能を実装したいのですが、これらのデバイスが、これらのデバイスのデータを使用するサーバーとどのように通信し、これらのデータをサーバーに保存するのかわかりません。これらのデバイスについて質問する必要があり
ます。データを取得してサーバーにデータを保存しますか? または、これらのデバイスはサーバーに接続し、データをサーバーに送信します。
AndroidデバイスでGPSデバイス機能をシミュレートするAndroidデバイス用のアプリケーションを作成したいので、この質問は私にとって重要です! 
2:サーバーからandroid端末への接続方法を調べてmqttの情報を取得!mqtt を使用してサーバーから Android デバイスに接続できますか?
Androidデバイスでこれらのデバイス機能をシミュレートするために、サーバーまたはデバイスのどちらが他に接続してデータを送信するかを知る必要がありますか?
			
			1591 次
		
1 に答える
            1        
        
		
まず、デバイス上の位置を取得し、それをサーバーに送信して、この情報を表示できるようにする必要があります。コードを実用的に使用するには、次のような方法でデバイス上の場所を取得する必要があります。
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates("gps", 60000, 0, locationListener);
private final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            // Here you have both location.getLatitude() and location.getLongitude()
        }
        public void onProviderDisabled(String provider){}   
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
};
Android の場所に関する詳細については、ユーザーの場所に関する公式ドキュメントを参照してください。
場所の部分が完了したら、サーバーへの送信を開始できます。これには JSON の使用を検討してください。
「緯度経度」を含む文字列行があるとします。最初に JSON オブジェクトを構築する必要があります。
public JSONObject buildJSONObject(String line) {
        String[] toJson = line.split(" ");
        JSONObject object = new JSONObject();
        try {
            object.put("latitude", toJson[0]);
            object.put("longitude", toJson[1]);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return object;
}
そして、次のようなものでサーバーに送信します。
public boolean sendTraceLineToServer(JSONObject line) {
    // The mock server IP is 10.0.2.2, just for testing purposes
    // This server receives a JSON with format {"location":{"latitude":xx.xx, "longitude":yy.yy}}
    HttpPost httpPost = new HttpPost("http://10.0.2.2:3000/locations");
    DefaultHttpClient client = new DefaultHttpClient();
    JSONObject holder = new JSONObject();
    boolean sent = false; 
    try {
        holder.put("location", line);
        StringEntity se = new StringEntity(holder.toString());
        httpPost.setEntity(se);
        httpPost.setHeader("Content-Type","application/json");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    HttpResponse response = null;
    try {
        response = client.execute(httpPost);
        sent = true;
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        Log.e("ClientProtocol",""+e);
    } catch (IOException e) {
        e.printStackTrace();
        Log.e("IO",""+e);
    }
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        try {
            entity.consumeContent();
        } catch (IOException e) {
            Log.e("IO E",""+e);
            e.printStackTrace();
        }
    }
    return sent;
}
ここでは、JSON をサーバーにポストする方法の例をさらに示します。
サーバーでは、私の場合はRailsで記述し、JSON を受け取るメソッドを次のように簡単に作成します。
# POST /locations
# POST /locations.xml
def create
    @location = Location.new(params[:location])
    respond_to do |format|
      if @location.save
        format.json { render :json => @location, :status => :created, :location => @location }
      else
        format.json { render :json => @location.errors, :status => :unprocessable_entity }
      end
    end
  end
これで、デバイス上の場所、JSON を使用した HTTP を使用して送信し、Rails サーバーの例で受信することができました。
于 2012-04-23T08:52:09.297   に答える