2017-10-03 7 views
2

私はアンドロイドでmongoDBをサポートするアプリを開発中です。 JavaコードからMongoIDを作成し、それを私のサーバーに送信したいと思います。アンドロイドコードでMongoIDを作成するには?

は、ここで私は、このJSON

{ 
"_id" : ObjectId("59b7bcdf92e706382b00009f"), 
"user_id" : "6bb82a99-bccd-4868-a799-55e7d28f969c", 
"is_active" : false, 
"_slugs" : [ 
    "aaa" 
], 
"facility_name" : "aaa", 
"industry_id" : 1, 
"old_industry_id" : 1, 
"established_date" : "1994-06-01", 
"summary" : "this is test", 
"facility_website" : "www.xxx.com", 
"contact" : { 
    "_id" : ObjectId("4637gdff92jhsgd378364y"), 
    "info_type" : "PROFILE_INFO", 
    "name" : "xxxxxxxxxx", 
    "mobileNumber" : "xxxxxxxxx", 
    "email" : "[email protected]", 
    "shown_on_profile" : true 
}} 

を作成したい私のコード

あるしかし、私は私のJavaコードからこのJSONを取得しています。

{ 
"_id" : ObjectId("59b7bcdf92e706382b00009f"), 
"user_id" : "6bb82a99-bccd-4868-a799-55e7d28f969c", 
"is_active" : false, 
"_slugs" : [ 
    "aaa" 
], 
"facility_name" : "aaa", 
"industry_id" : 1, 
"old_industry_id" : 1, 
"established_date" : "1994-06-01", 
"summary" : "this is test", 
"facility_website" : "www.xxx.com", 
"contact" : { 
    "_id" : "{}", 
    "info_type" : "PROFILE_INFO", 
    "name" : "xxxxxxxxxx", 
    "mobileNumber" : "xxxxxxxxx", 
    "email" : "[email protected]", 
    "shown_on_profile" : true 
}} 

問題は

はここ

String _id = new JsonObject().toString(); // this is Gson.JsonObject() object 
JSONObject contact = new JSONObject(); 
contact.put("_id", _id); 
contact.put("info_type", "PROFILE_INFO"); 
contact.put("name", "User-1"); 
contact.put("mobileNumber", "xxxxxxxxxx"); 
contact.put("email", "[email protected]"); 
contact.put("shown_on_profile", true); 

答えて

0

@fandroによると、私は私の問題を解決しました。

Serverが、私はこのコードを使用してこのデータにアクセスすることができます私は、この

{"_id":{"$id":"59b7bcdf92e706382b00009f"}} 

のようなMongoDBのデータを与え、 は、ここで私が空JSONObject作成した新しいIDを生成するための私のAndroidのコード

ID = jsonData.getJSONObject("_id).getString("$id"); 

でありますアンドロイドと私のサーバーに渡されます。私のサーバー上で

_id = new JSONObject().put("$id", ""); 

、私はIDを確認し、それが空であるならば、私は、このコードの新しいMongoIDフォームを生成

<?php 
$_id = $_POST['_id']; 
if ($_id['$id'] == "") 
$_id = new MongoID(); 
?> 
1

MongoIDが自動的にサーバーによって生成された私のJavaコードである "接触" オブジェクトです。あなたがしなければならないことは、MongoIDなしであなたの連絡先を送ることです。サーバーはデータベースにデータを挿入し、作成したばかりのこの要素のMongoIDを返します。連絡先全体またはMongoIDだけをAndroidアプリに返して使用することができます。

+0

しかし、私の場合には、別のJsonObject内の多くのjsonObject.like JsonObjectがあります。 – Mehul

+0

それで、あなたは複数の時間にそれを行う必要があります:オブジェクトを挿入し、objectIDを取得し、前のIDを持つ2番目のオブジェクトを送信し、2番目のオブジェクトIDを取得します... – fandro

+0

@Mehul – fandro

関連する問題