У меня HttpRequestException: 400 Плохой запрос при попытке вставить контакт в контакты Google с помощью 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", но это не работает.
Любое предложение?
Спасибо.