1

我在使用 Atom 解析/序列化时遇到问题——显然与命名空间和默认别名有关——但我无法弄清楚我做错了什么。

我有两种方法 - 一种方法是我尝试执行 GET 并查看是否定义了专辑,以及尝试执行 POST 以创建专辑(如果不存在)的方法。

我设法开始工作的 GET - 虽然我也很确定我做错了什么,因为它与 PicasaAndroidSample 不同。具体来说,如果我定义:

public class EDAlbum  {

    @Key("atom:title")
    public String title;

    @Key("atom:summary")
    public String summary;

    @Key("atom:gphoto:access")
    public String access;

    @Key("atom:category")
    public EDCategory category = EDCategory.newKind("album"); 
}

那么下面的代码确实得到了所有的专辑:

PicasaUrl url = PicasaUrl.relativeToRoot("feed/api/user/default");
HttpRequest request = EDApplication.getRequest(url);
HttpResponse res = request.execute();
EDAlbumFeed feed = res.parseAs(EDAlbumFeed.class);
boolean hasEDAlbum = false;
            for (EDAlbum album : feed.items) {
                if (album.title.equals(EDApplication.ED_ALBUM_NAME)) {
                    hasEDAlbum = true;
                    break;
                }
            }

但是 - 如果我有:

public class EDAlbum  {

    @Key("title")
    public String title;

    @Key("summary")
        public String summary;

    @Key("gphoto:access")
    public String access;

    @Key("category")
        public EDCategory category = EDCategory.newKind("album"); 
}

然后提要有一个空集合 - 即解析器不知道这是 Atom(我的猜测)。

我可以在课堂上使用 android:title - 我不明白,但它有效。

问题是我无法将 POST 放入锅中(创建相册)。这段代码是:

EDAlbum a = new EDAlbum();
a.access = "public";
a.title = EDApplication.ED_ALBUM_NAME;
a.summary = c.getString(R.string.ed_album_summary);
AtomContent content = new AtomContent();
content.entry = a;  
content.namespaceDictionary = EDApplication.getNamespaceDictionary();
PicasaUrl url = PicasaUrl.relativeToRoot("feed/api/user/default");
HttpRequest request = EDApplication.postRequest(url, content);
HttpResponse res = request.execute(); 

传输和命名空间是:

私有静态最终 HttpTransport 传输 = 新 ApacheHttpTransport(); // 我的库不包括 GoogleTransport。

private static HttpRequestFactory createRequestFactory(final HttpTransport transport) {

        return transport.createRequestFactory(new HttpRequestInitializer() {
         public void initialize(HttpRequest request) {
          AtomParser parser = new AtomParser();
          parser.namespaceDictionary = getNamespaceDictionary();
          request.addParser(parser);
         }
      });
 } 

public static XmlNamespaceDictionary getNamespaceDictionary() {
        if (nsDictionary == null) {
            nsDictionary = new XmlNamespaceDictionary();
            nsDictionary.set("", "http://www.w3.org/2005/Atom");
            nsDictionary.set("atom", "http://www.w3.org/2005/Atom");
            nsDictionary.set("exif", "http://schemas.google.com/photos/exif/2007");
            nsDictionary.set("gd", "http://schemas.google.com/g/2005");
            nsDictionary.set("geo", "http://www.w3.org/2003/01/geo/wgs84_pos#");
            nsDictionary.set("georss", "http://www.georss.org/georss");
            nsDictionary.set("gml", "http://www.opengis.net/gml");
            nsDictionary.set("gphoto", "http://schemas.google.com/photos/2007");
            nsDictionary.set("media", "http://search.yahoo.com/mrss/");
            nsDictionary.set("openSearch", "http://a9.com/-/spec/opensearch/1.1/");
            nsDictionary.set("xml", "http://www.w3.org/XML/1998/namespace");
        }
        return nsDictionary;
}

如果我使用

@Key("title")
public String title;

然后我得到一个异常,它没有默认命名空间:

W/System.err(1957):java.lang.IllegalArgumentException:无法识别的别名:(默认)W/System.err(1957):在 com.google.common.base.Preconditions.checkArgument(Preconditions.java:115)W /System.err(1957):在 com.google.api.client.xml.XmlNamespaceDictionary.getNamespaceUriForAliasHandlingUnknown(XmlNamespaceDictionary.java:288)W/System.err(1957):在 com.google.api.client.xml.XmlNamespaceDictionary .startDoc(XmlNamespaceDictionary.java:224)

如果我使用

@Key("atom:title")
public String title;

然后它会序列化,但每个元素都有 atom: 前缀并且调用失败 - 当我对其进行 tcpdump 时,我看到类似

.`....<? xml vers
ion='1.0 ' encodi
ng='UTF- 8' ?><at
om:entry  xmlns:a
tom="htt p://www.
w3.org/2 005/Atom
"><atom: category
 scheme= "http://
schemas. google.c
om/g/200 5#kind" 
term="ht tp://sch
emas.goo gle.com/
photos/2 007#albu
m" /><at om:gphot
o:access >public<
/atom:gp hoto:acc
....

为了使用,我需要做些什么不同的事情

@Key("title")
public String title;

并让 GET 和 POST 都管理命名空间?

4

2 回答 2

0

看起来您正在添加重复的字典键或序列化程序无法理解的键。

请改用以下内容。

static final XmlNamespaceDictionary DICTIONARY = new XmlNamespaceDictionary()
      .set("", "http://www.w3.org/2005/Atom")
      .set("activity", "http://activitystrea.ms/spec/1.0/")
      .set("georss", "http://www.georss.org/georss")
      .set("media", "http://search.yahoo.com/mrss/")
      .set("thr", "http://purl.org/syndication/thread/1.0");
于 2012-03-09T05:49:06.660 回答
0

删除“atom”项目命名空间的显式设置为我解决了这个问题。

于 2012-06-24T22:16:01.103 回答