问题
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