java 如何从 Android 应用程序中的 WordPress 博客中检索帖子?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12024811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:19:54  来源:igfitidea点击:

How to retrieve posts from a WordPress Blog in an Android App?

javaandroidxmlwordpress

提问by Argus9

I'm trying to develop an Android app for browsing a Wordpress-powered blog I own. I'm trying to figure out how to retrieve posts and other information from the blog to display in the app. I've looked all over but I feel completely lost. Is this something that can be done entirely in Java/XML? If so, how?

我正在尝试开发一个 Android 应用程序,用于浏览我拥有的由 Wordpress 驱动的博客。我想弄清楚如何从博客中检索帖子和其他信息以在应用程序中显示。我已经看遍了,但我觉得完全迷失了。这是完全可以在 Java/XML 中完成的事情吗?如果是这样,如何?

Thank you!

谢谢!

回答by Integrating Stuff

Yes, it can be done.

是的,这是可以做到的。

One way is to use the xml-rpc api. Wordpress blogs have an xml-rpc api(which you need to enable on the Wordpress blog under "Settings - Writing"). You will also need to create a user on the blog, which you give at least read access, and for which you include the credentials in your app. From then on, you can do xml-rpc calls to your Wordpress blog(s).

一种方法是使用 xml-rpc api。Wordpress 博客有一个 xml-rpc api(您需要在 Wordpress 博客的“设置 - 写作”下启用)。您还需要在博客上创建一个用户,您至少授予该用户读取权限,并在您的应用程序中包含该用户的凭据。从那时起,您可以对 Wordpress 博客进行 xml-rpc 调用。

If using this xml-rpc api is an option, take a look at this Java lib: http://code.google.com/p/wordpress-java/

如果使用此 xml-rpc api 是一个选项,请查看此 Java 库:http: //code.google.com/p/wordpress-java/

You can get the blogposts using this lib like this:

你可以像这样使用这个库来获取博客文章:

String username = args[0];
String password = args[1];
String xmlRpcUrl = args[2];
Wordpress wp = new Wordpress(username, password, xmlRpcUrl);
List<Page> recentPosts = wp.getRecentPosts(10);

Also, the official Wordpress Android app is open source. Instructions to get it are at: http://android.wordpress.org/development/You could use this source code as a starting point and adapt it to your needs.

此外,官方的 Wordpress Android 应用程序是开源的。获取说明位于:http: //android.wordpress.org/development/您可以使用此源代码作为起点并根据您的需要进行调整。

Note that you can only use the xml-rpc api when you have a user with read access. If you do not have the credentials of a user with read access, you can't get the posts using the xml-rpc api. Fetching the rss feed and parsing the rss feed with some java lib would probably be your best bet then(check http://www.vogella.com/articles/RSSFeed/article.htmlon how to read an rss feed using Java).

请注意,当您拥有具有读取访问权限的用户时,您只能使用 xml-rpc api。如果您没有具有读取访问权限的用户的凭据,则无法使用 xml-rpc api 获取帖子。获取 rss 提要并使用一些 java lib 解析 rss 提要可能是您最好的选择(查看http://www.vogella.com/articles/RSSFeed/article.html关于如何使用 Java 读取 rss 提要)。

回答by Achraf Amil

As Integrating Stuffsaid, the 'net.bican:jwordpress:0.6.4' is what you need. Still, the example he gave is now deprecated. There is no more getRecentPosts(int) but getPosts(FilterPost).

正如Integrating Stuff所说,'net.bican:jwordpress:0.6.4' 正是您所需要的。尽管如此,他给出的例子现在已被弃用。不再有 getRecentPosts(int) 而是 getPosts(FilterPost)。

So now the correct code is :

所以现在正确的代码是:

String username = args[0];
String password = args[1];
String xmlRpcUrl = args[2];
Wordpress wp = new Wordpress(username, password, xmlRpcUrl);
FilterPost filter = new FilterPost() ;
filter.setNumber(10);
List<Post> recentPosts = wp.getPosts(filter);

to know more check the example : https://github.com/canbican/wordpress-java/blob/bb4b60a008ee6d280aedd9174df4a657bff683ac/src/net/bican/wordpress/example/Main.java

要了解更多信息,请查看示例:https: //github.com/canbican/wordpress-java/blob/bb4b60a008ee6d280aedd9174df4a657bff683ac/src/net/bican/wordpress/example/Main.java

Also, if you're using Gradle, check this dependencies problem you may face : https://github.com/canbican/wordpress-java/issues/54

此外,如果您使用 Gradle,请检查您可能面临的依赖关系问题:https: //github.com/canbican/wordpress-java/issues/54

回答by Addi.Star

There is an alternative way also , and its working good,

还有一种替代方法,它的工作原理很好,

you can install json plugin in your word press and you can retrieve all the post by requesting the url ... and parsing the response json in your android views will be working .

你可以在你的 wordpress 中安装 json 插件,你可以通过请求 url 来检索所有帖子......并且在你的 android 视图中解析响应 json 将起作用。