目前,我正在尝试使用 java sbt sdk api 创建和更新博客文章。
我可以阅读博客和博客文章,并且能够使用 SDK 的 .getBlogPost(bkogHandle, postId) 方法检索单个博客文章。
但是,如果我尝试更新或创建博客文章,要么遇到 NullPointerException,要么什么也没有发生。
出于测试目的,我创建了一个演示博客并尝试更新其中的博客文章:
String blogHandle = "citb";
String postId = "7973d29e-26b6-4d23-ab04-ebfb734bf512";
BlogPost post = sbcs.getBlogService().getBlogPost(blogHandle, postId);
if (null != post){
System.out.println("Found post "+post.getTitle()+" ("+post.getAlternateUrl()+")");
Map<String, Object> fieldMap = post.getFieldsMap();
System.out.println("Found "+fieldMap.keySet().size()+" Entries in the field Map");
try{
//Save the post
System.out.println("Update blog post");
post.setContent("<p dir='ltr'>blabla "+new Date()+"</p>");
System.out.println(post.getContent());
post.setTitle(post.getTitle()+" +");
sbcs.getBlogService().updateBlogPost(post, blogHandle);
}catch(Exception e){
e.printStackTrace();
}
}
此示例运行时不会引发异常,但不会更新博客。我错过了什么吗?
如果我使用post.save(blogHandle);
我会收到以下错误消息:
com.ibm.sbt.services.client.connections.blogs.BlogServiceException: error creating blog post
at com.ibm.sbt.services.client.connections.blogs.BlogService.createBlogPost(BlogService.java:627)
at com.ibm.sbt.services.client.connections.blogs.BlogPost.save(BlogPost.java:128)
Caused by: java.lang.NullPointerException
at com.ibm.sbt.services.client.connections.blogs.feedhandler.BlogsFeedHandler.createEntity(BlogsFeedHandler.java:42)
at com.ibm.sbt.services.client.connections.blogs.BlogService.createBlogPost(BlogService.java:624)
... 2 more
如果我createBlogPost(post, blogHandle)
用来创建新帖子,我会遇到同样的错误。
任何想法是什么问题或任何建议我可以做些什么来保存博客帖子?
编辑:
在 BlogService 类中,问题似乎出在以下方法中
public BlogPost createBlogPost(BlogPost post, String blogHandle) throws BlogServiceException {
if (null == post){
throw new BlogServiceException(null,"null post");
}
Response result = null;
try {
BaseBlogTransformer transformer = new BaseBlogTransformer(post);
Object payload = transformer.transform(post.getFieldsMap());
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/atom+xml");
String createPostUrl = resolveUrl(blogHandle, FilterType.CREATE_BLOG_POST, null);
**result = createData(createPostUrl, null, headers, payload);**
**post = (BlogPost) new BlogPostsFeedHandler(this).createEntity(result);**
} catch (Exception e) {
throw new BlogServiceException(e, "error creating blog post");
}
return post;
}