Object XML mapping in Android

寵の児 提交于 2019-12-24 03:29:31

问题


I am developing a client-server Model-based application in which client is in Android and server in PHP.

I want to transfer product information for example Name,Price,Description, from client to server. I have read that through marshaling/unmarshaling or serialization it can be achieved but all tutorials and example are in Java. But I need in Android. Please guide me to implement in Android. Or there is any other way to implement? Any example will b appreciated. Thanks.


回答1:


Have a look on json. Google provides a nice Library called "Gson" for that.

To stick to your example, a json representation send from your server via http could be:

{"name":"foo", "price":"1000", "description":"this is an item description"}

In your app, you have a class MyObject.class

public class MyObject {
    private String name;
    private double price;
    private String description;

// all your other methods
}

Then you can just do:

MyObject obj = new Gson().fromJson(jsonString, MyObject.class)

and voila, made an object out of the string in one line. Just be sure that the variables have the same name in the json representation and the class, then Gson does all the work for you. You can also make a String representation out of the object with String jsonString = new Gson().toJson(obj).




回答2:


I'm using SimpleXML http://simple.sourceforge.net/

And I'm happy with it, it's a light jaxb like!



来源:https://stackoverflow.com/questions/16567925/object-xml-mapping-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!