Object XML mapping in Android

2019-07-21 09:22发布

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.

2条回答
爷、活的狠高调
2楼-- · 2019-07-21 09:38

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

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

查看更多
Rolldiameter
3楼-- · 2019-07-21 09:50

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).

查看更多
登录 后发表回答