Google api 当使用google api和Atom序列化/解析器时,如何设置默认名称空间或如何定义@key值
我在Atom解析/序列化方面遇到了问题——显然与名称空间和默认别名有关——但我可以;我不知道我做错了什么 我有两种方法-一种是我尝试进行GET,看看是否定义了相册,如果相册不存在,则尝试通过POST创建相册 GET我设法开始工作了——尽管在那里我也很确定我做错了什么,因为它与Picasaandroid样本不同。具体而言,如果我定义:Google api 当使用google api和Atom序列化/解析器时,如何设置默认名称空间或如何定义@key值,google-api,google-api-java-client,Google Api,Google Api Java Client,我在Atom解析/序列化方面遇到了问题——显然与名称空间和默认别名有关——但我可以;我不知道我做错了什么 我有两种方法-一种是我尝试进行GET,看看是否定义了相册,如果相册不存在,则尝试通过POST创建相册 GET我设法开始工作了——尽管在那里我也很确定我做错了什么,因为它与Picasaandroid样本不同。具体而言,如果我定义: public class EDAlbum { @Key("atom:title") public String title; @Key
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");
}
然后,提要有一个空集合——也就是说,解析器不知道这是我的猜测
我可以和安卓一起生活:在我的班级里有头衔——我不喜欢;我不明白,但它起作用了
问题是我无法将帖子发送到wok来创建相册。该代码是:
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;//我的图书馆没有;不包括谷歌运输
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.err1957:com.google.common.base.premissions.checkArgumentPremissions.java:115
W/System.err 1957:com.google.api.client.xml.XmlNamespaceDictionary.getnamespaceuriforaliashandlingunknownxmlnamnamespacedictionary.java:288
W/System.err 1957:com.google.api.client.xml.XmlNamespaceDictionary.startdocxmlnamnamespacedictionary.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
....
让GET和POST管理名称空间?看起来您正在添加重复的命令键或序列化程序无法理解的键 改用以下方法
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");
删除atom项名称空间的显式集合为我解决了这个问题。你明白了吗?我也有同样的问题。然而,我看到了一个测试用例。
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");