1

google-api-java-client を使用して Google の連絡先に連絡先を挿入しようとすると 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」に変更しようとしましたが、これは機能しません。なにか提案を?ありがとう。

4

0 に答える 0