我有一个HttpRequestException:400错误请求尝试在google-api-java-client的帮助下向谷歌联系人插入联系人,我花了几个小时找到错误,也许有人可以帮忙?这是我的插入联系方式:
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”,但这不起作用。 有什么建议吗? 感谢。