400错误请求:通过google api java客户端向google联系人添加联系人
我有一个HttpRequestException:在google api java客户端的帮助下尝试将联系人插入google联系人时,有400个错误请求,我花了几个小时来找出问题所在,也许有人可以帮助我?这是我的插入联系人方法:400错误请求:通过google api java客户端向google联系人添加联系人,java,android,google-api-java-client,Java,Android,Google Api Java Client,我有一个HttpRequestException:在google api java客户端的帮助下尝试将联系人插入google联系人时,有400个错误请求,我花了几个小时来找出问题所在,也许有人可以帮助我?这是我的插入联系人方法: private static ContactEntry insertContact(ContactEntry contact,String auth) throws IOException { String url = "https://www.google.c
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;
}
这是我的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”,但这不起作用。
有什么建议吗?
谢谢。似乎没有人尝试将android联系人添加到谷歌
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()));
}
});
}