15

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?

Thank you!

Argus9
  • 1,863
  • 4
  • 26
  • 40

3 Answers3

22

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).

If using this xml-rpc api is an option, take a look at this Java lib: 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.

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.html on how to read an rss feed using Java).

Integrating Stuff
  • 5,253
  • 2
  • 33
  • 39
  • Thanks for a HUGELY thorough answer! I think the API will work best for me, but I'll look into modifying the Wordpress app too. Thanks again! – Argus9 Aug 19 '12 at 17:31
  • 1
    the library isn't working for me. It is in both library project and dependent project but logcat says "noclassdeffounderror" with the "net.bican.wordpress.Wordpress" class. help please! – aldo.roman.nurena Sep 22 '12 at 00:14
  • You need to create a folder called libs in your project and copy the jar file there and add it to your build path. There are other posts on this, I point you to http://stackoverflow.com/questions/1334802/how-can-i-use-external-jars-in-an-android-project –  Oct 16 '12 at 18:44
  • This helped a lot. Was trying to use their JSON API, but it doesn't allow anonymous access (this doesn't either, but I can have a user with limited privileges and handle the login transparently) – Adam Shiemke Jun 07 '13 at 15:01
  • xml-rpc Wordpress API does not works with a read-only access user. I tried both wordpress android app and implementing myself. With administrator rights it is working, but I would like to read blog posts without hardcoding administrator credentials (obviously for security reasons). So I think that the only possibilities are using RSS feed parsing or REST HTML API. – MrMoog Dec 21 '13 at 10:02
  • Hello All, I am also searching for something to login user into the wordpress account through APIs and then to create, edit & delete posts. Can anyone help me with that as I am unable to understand the above explanation. – Salman Khan Feb 11 '15 at 06:04
2

As Integrating Stuff said, 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).

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

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

Achraf Amil
  • 1,275
  • 16
  • 20
0

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 .

Addi.Star
  • 475
  • 2
  • 15
  • 36