我在网上找不到任何帮助,所以我在这里问。我想用java类创建一个谷歌卡。所以,我想创建的JSON
是:
{
"thread":{
"name":"some url here"
},
"cards":[
{
"sections":[
{
"widgets":[
{
"textParagraph":{
"text":"bla bla"
}
},
{
"buttons":[
{
"textButton":{
"text":"reminder in 10",
"onClick":{
"openLink":{
"url":"some Method"
}
}
}
}
]
}
]
}
]
}
]
}
我试过几种方法。我甚至发现了一个生成POJO
的站点,但我看不懂。我的尝试是这样的:
Cards cards = new Cards();
Sections sections = new Sections();
Widgets widgets = new Widgets();
TextParagraph textParagraph = new TextParagraph();
Text text = new Text();
Buttons button = new Buttons();
TextButton textButton = new TextButton();
OnClick onClick = new OnClick();
OpenLink openLink = new OpenLink();
NoNameClass haha = new NoNameClass();
ThreadX threadx = new ThreadX();
threadx.name = "spaces/" + reminder.getSpaceId() + "/threads/" + reminder.getThreadId();
text.text = "<" + reminder.getSenderDisplayName() + "> " + reminder.getWhat();
openLink.url = "https://media0.giphy.com/media/QNnKbtl03OGsM/giphy.gif?cid=3640f6095c5851de3064736a2ef2345a";
onClick.openLink = openLink;
textButton.onClick = onClick;
textButton.text = "se 10";
haha.textButton = textButton;
textParagraph.textParagraph = text;
button.buttons = Lists.newArrayList(haha);
widgets.widgets = Lists.newArrayList(textParagraph, button);
sections.sections = Lists.newArrayList(widgets);
cards.thread = threadx;
cards.cards = Lists.newArrayList(sections);
我创建了一个JsonHttpContent
private String send(GenericUrl url, Cards message, String httpMethod) {
HttpContent content = new JsonHttpContent(new JacksonFactory(),message);
HttpRequest request;
try {
if (httpMethod.equals("POST")) {
request = requestFactory.buildPostRequest(url, content);
} else {
request = requestFactory.buildGetRequest(url);
}
} catch (Exception e) {
logger.error("Error creating request using url: {}", url, e);
return null;
}
String response = "";
try {
HttpResponse httpResponse = request.execute();
response = httpResponse.parseAsString();
} catch (IOException e) {
logger.error("Error creating request using url: {}", url, e);
}
return response;
}
但这根本不起作用,我有JsonIgnoreProperties
为true,但我得到了该错误
{
"error":{
"code":400,
"message":"Message cannot be empty. Discarding empty create message request in spaces/AAAADvB8eGY.",
"errors":[
{
"message":"Message cannot be empty. Discarding empty create message request in spaces/AAAADvB8eGY.",
"domain":"global",
"reason":"badRequest"
}
],
"status":"INVALID_ARGUMENT"
}
}
我已经设置了一吨的参数,但这并没有读取任何东西,所以我需要帮助。
1条答案
按热度按时间ulydmbyx1#
Google API
通常不容易构建结构化数据模型。在Card Formatting Messages页面上,我们可以看到许多不同的JSON
有效负载,用于不同类型的卡。在Github
上,我们可以找到Hangouts Chat code samples项目,它引入了CardResponseBuilder
类,提供了一些构建器方法来构建不同类型的卡。我认为,这是一个很好的方法。如果可以,你可以试着使用这个类。它依赖于javax.json
库。使用这个类,并知道Jackson
将Map
序列化为JSON Object
,将List
序列化为JSON Array
,我们可以创建非常相似的类:我刚刚用基于
Map
的ObjectBuilder
替换了JsonObjectBuilder
,用基于List
的ArrayBuilder
替换了JsonArrayBuilder
。下面是该构建器的简单用法:以上代码打印:
我没有添加处理
thread
属性的方法,因为我在文档中没有找到它,但您应该能够自己完成。