开发者

How to do a partial update on contacts using Google API Java client?

I'm using the lightweight Google API Java client to modify contacts.

There are a few samples for the API showing how to do partial updates (i.e. via HTTP PATCH), but Google Contacts specifically doesn't seem to support PATCH . The docs also specify that contacts updates done via PUT need to include the full information about the contact since any existing data omitted in the PUT request will be removed from the contact.

So what's the easiest procedure to read a contact, modify its data and update it? The client API has this nice AtomParser that takes the response from the HTTP GET request and populates members in my class, but unless my class contains every possible field that a contact could have, I might not read in all the data and as such delete data during the HTTP PUT.

My only guess is that I'll have to skip all the cool AtomParser witchcraft and just use a plain old XML parser to read the i开发者_如何学JAVAncoming feed, extract the information I need and then do an HTTP PUT on it. But that seems a bit absurd, given all the functionality int he API client. Is there a better way?

I'm secretly hoping there is an equivalent to this:

HttpRequest request = transport.buildPatchRequest();
request.setUrl(getEditLink());
AtomPatchRelativeToOriginalContent content =
    new AtomPatchRelativeToOriginalContent();

content.namespaceDictionary = Namespace.DICTIONARY;
content.originalEntry = originalEntry;
content.patchedEntry = modifiedEntry;
request.content = content;

return RedirectHandler.execute(request).parseAs(getClass());


That's exactly what GenericXml was designed for: it stores arbitrary XML that you are not using in your application so you can safely use the PUT method. Simply extend it in your data model classes. A good example of GenericXml can be found in the sample for the Content API for Shopping. They also have a detailed guide of the XML model including a discussion of GenericXml. Let me know if it doesn't work for you.

Full disclosure: I am an owner of the google-api-java-client project.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜