在 google-api-java-client 的帮助下尝试将联系人插入谷歌联系人时,我有一个 HttpRequestException:400 Bad request,我花了几个小时来找出问题所在,也许有人可以帮忙?这是我插入联系人的方法:
private static ContactEntry insertContact(ContactEntry contact,String auth) throws IOException
{
String url = "https://www.google.com/m8/feeds/contacts/default/full";
AtomContent atomContent = AtomContent.forEntry(ClientUtils.createNamespaceDictionary(), contact);
HttpRequest request = ClientUtils.createBareRequestFactory(auth).buildPostRequest(new GoogleUrl(url), atomContent);
return request.execute().parseAs(ContactEntry.class);
}
这是我的联系人条目的 xml 表示模型:
public class ContactEntry
{
@Key
private Category category = new Category();
@Key("gd:name")
public Name name;
@Key("link")
public List<Link> links;
@Key
public String title;
@Key
public Content content;
@Key("gd:phoneNumber")
public PhoneNumber phoneNumber;
}
我创建这样的联系人:
private static ContactEntry createContact()
{
ContactEntry contact = new ContactEntry();
contact.content = new Content("text","SOME INFORMATION");
contact.title = "Vasya B";
contact.phoneNumber = PhoneNumber.setPhoneParameters("work","true" , "555-555-555");
contact.name = new Name("Vasya", "B");
return contact;
}
这是我的 Util 类:
public class ClientUtils
{
public static XmlNamespaceDictionary createNamespaceDictionary()
{
return new XmlNamespaceDictionary()
.set("", "http://www.w3.org/2005/Atom")
.set("gd", "http://schemas.google.com/g/2005")
.set("xml", "http://www.w3.org/XML/1998/namespace");
}
public static HttpRequestFactory createBareRequestFactory(final String auth) throws IOException
{
final HttpTransport transport = AndroidHttp.newCompatibleTransport();
return transport.createRequestFactory(new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) {
GoogleHeaders headers = new GoogleHeaders();
headers.setApplicationName("Google-Contacts/1.0");
headers.setGoogleLogin(auth);
headers.gdataVersion = "3";
request.setHeaders(headers);
request.addParser(new AtomParser(ClientUtils.createNamespaceDictionary()));
}
});
}
}
我也尝试将 gdataVersion 更改为“2”,但这不起作用。有什么建议吗?谢谢。