如何通过Android发送复杂对象通过Loopback REST?

时间:2015-06-01 15:04:49

标签: java android json rest loopbackjs

我在服务器上使用带有Android客户端的环回。

我想使用RESTfull API从Android(使用Loopback SDK)发送Android中这样的对象:

    private static IEdmModel GetODataEdmModel()
    {
        IEdmModel edmModel = null;
        string FullUrl = "http://localhost:4684/BDBWcfService.svc/$metadata/";


        var request = WebRequest.CreateHttp(FullUrl);
        var metadataMessage =
            new ClientHttpResponseMessage((HttpWebResponse)request.GetResponse());
        using (var messageReader = new ODataMessageReader(metadataMessage))
        {
            edmModel = messageReader.ReadMetadataDocument();
        }
        return edmModel;
    }

    public ActionResult ODataGetProperties(string TableName)
    {
        DataModel = (DataModel == null) ? GetODataEdmModel() : DataModel;

        //gets a mapping of the tables names
        var TableNames = DataModel.SchemaElements.OfType<IEdmEntityContainer>().Single().Elements.OfType<IEdmEntitySet>().ToDictionary(k => k.Name, v => v.ElementType.Name);

        string TypeName = string.Empty;
        if (TableNames.TryGetValue(TableName, out TypeName))
        {
            //uses the type name to look up the properties
            var data = DataModel.SchemaElements.OfType<IEdmEntityType>().Where(k => k.Name == TypeName).Select(v => new
            {
                NavigationProperties = v.NavigationProperties().Select(p => p.Name).ToList(),
                Properties = v.Properties().Select(p => p.Name).ToList(),
            }).FirstOrDefault();

            var JSSerializer = new JavaScriptSerializer();
            var json = JSSerializer.Serialize(data);
            return Json(data, JsonRequestBehavior.AllowGet);

        }
        return new HttpStatusCodeResult(HttpStatusCode.NotFound, "The Requested Table was not found please refine your query");
    }

现在在Loopback中,我定义了一个这样的模型:

public class MyRESTObject extends Model {

private String name;
private String date;
private HashMap<String, MySubObject> content;


private static void run(Activity activity) {
    final MySubObject myContent = new MySubObject();
    myContent.setValues(new HashMap<String, Integer>() {{
                            put("A", 1);
                            put("B", 2);
                        }}
    );

    myContent.setTexts(new HashMap<String, String>() {{
                           put("Q", "Bonjour");
                           put("S", "Aurevoir");
                       }}
    );

    RestAdapter adapter = new RestAdapter(activity.getApplicationContext(), "https://x.herokuapp.com:443/api");
    ObjectRepository repository = adapter.createRepository(ObjectRepository.class);

    MyRESTObject myObject = repository.createObject(ImmutableMap.of("date", new Date()));
    myObject.name = "hello";
    myObject.content = new HashMap<String, MySubObject>() {{
        put("1", myContent);
        put("2", myContent);
    }};
    myObject.save(new VoidCallback() {
        @Override
        public void onSuccess() {

        }

        @Override
        public void onError(Throwable t) {

        }
    });
}

public class ObjectRepository extends ModelRepository<MyRESTObject> {

    public ObjectRepository() {
        super("Tasting", MyRESTObject.class);
    }
}
}

public class MySubObject {
    public HashMap<String, Integer> values;

    public HashMap<String, String> getTexts() {
        return texts;
    }

    public void setTexts(HashMap<String, String> texts) {
        this.texts = texts;
    }

    public HashMap<String, Integer> getValues() {
        return values;
    }

    public void setValues(HashMap<String, Integer> values) {
        this.values = values;
    }

    public HashMap<String, String> texts;
}

当我在服务器级别获得post请求时,我得到了以下JSON

"properties": {
    "date": {
      "type": "date"
    },
    "name": {
      "type": "string"
    },
    "content": {
      "type": "object"
    }
  },

我希望更多:

{
  "name":"myName",
  "date":"2015-06-01",
  "content":{
    "name1":"javaObjectRef1",
    "name2":"javaObjectRef2",
    "name3":"javaObjectRef3"
  }
}

如何获取完整的内容?

0 个答案:

没有答案