I am new in Android development and I am trying to make an app that will simply display the post categories and posts from a WordPress website. Can any one help me, please.
问题:
回答1:
What you want to do is to create some kind of REST API from your WordPress to return JSON responses to your Android HTTP requests. To do that, first for the Android, you may refer to this post:
Make an HTTP request with android
Then, for the server side (your WordPress) you will have to add a plugin to handle your API requests. To do so, create a file called api-endpoint.php inside your wp-content/plugins and use something like this:
<?php
class API_Endpoint{
/** Hook WordPress
* @return void
*/
public function __construct(){
//Ensure the $wp_rewrite global is loaded
add_filter('query_vars', array($this, 'add_query_vars'), 0);
add_action('parse_request', array($this, 'sniff_requests'), 0);
add_action('init', array($this, 'add_endpoints'), 0);
}
/**
* Add public query vars
* @param array $vars List of current public query vars
* @return array $vars
*/
public function add_query_vars($vars){
$vars[] = '__api';
return $vars;
}
/**
* Add API Endpoints
* Regex for rewrites - these are all your endpoints
* @return void
*/
public function add_endpoints(){
//Get videos by category - as an example
add_rewrite_rule('^api/videos/?([0-9]+)?/?','index.php?__api=1&videos_cat=$matches[1]','top');
//Get products - as an example
add_rewrite_rule('^api/product/?([0-9]+)?/?','index.php?__api=1&product=$matches[1]','top');
}
/** Sniff Requests
* This is where we hijack all API requests
* If $_GET['__api'] is set, we kill WP and serve up rss
* @return die if API request
*/
public function sniff_requests(){
global $wp;
if(isset($wp->query_vars['__api'])){
$this->handle_api_request();
exit;
}
}
/** Handle API Requests
* This is where we handle off API requests
* and return proper responses
* @return void
*/
protected function handle_api_request(){
global $wp;
/**
*
* Do your magic here ie: fetch from DB etc
* then get your $result
*/
$this->send_response($result);
}
/** Response Handler
* This sends a JSON response to the browser
*/
protected function send_response(array $data){
header('content-type: application/json; charset=utf-8');
echo json_encode($data);
exit;
}
}
new API_Endpoint();
Then enable the API_Endpoint plugin through your WordPress admin interface and don't forget to flush your permalinks.
After that you'll be able to make API requests to:
http://example.com/api/videos/12
or
http://example.com/api/product/4
Edit
To get WordPress categories for example reference here - http://codex.wordpress.org/Function_Reference/get_categories
回答2:
Thanks Gregra for helping me. I found a solution. I installed WordPress plugin JSON API in wordpress site and referred this link http://www.learn2crack.com/2013/10/android-json-parsing-url-example.html to code in Android App
回答3:
In case of sending back data from wordpress to android app, using the JSON APi Plugin is very bad, although it give expected results but did you actually see what is the result of the json query??
Just check it, enter in your browser: www.yourwebsite.com/api/get_posts/
and check what you will get, this will query all posts or the number of posts you set to default in your wordpress dashboard, and will send all data about them as a json string, I tried to query 10 posts and the size of the json string was about 150KB imagine, just 10 posts, the user will have to download all them every time to get just 10 posts.
Solution: You should query the posts on server side and send back to android app only the data you are going to use, ex: title, thumbnail, excerpt ....
How to do so?
1- Make a php file in your wordpress dir and make it accessible
2- receive in it the posted values from android (these will be set by you in android to know the type of query you want, like number of posts and what post type ...)
3- query posts using wordpress functions according to the query entites in part-2
4- Generate your own json string with only the data you want to use.
5- echo the json string back to android
Now if I want only the title and the thumbnail link of 10 posts, the json string size will be about 2KB
that makes a difference :-)
You can use the JSON API Auth to register and login user it is easy/fast to implement and use.
回答4:
I think this is better, for using wordpress rest api you need to use Wordpress 4.7 or higher, or install the Rest Api plugin in previous versions. Then you need to configure Permalinks in wordpress, this will make rest api endpoints to work.
For reduce the size and customice the output json you may install the Rest api filter fields plugin see the bellow example:
Fetching Specified Number of Post
For fetching specified number of posts you can use post-per-page filter. The below URL will fetch only 3 posts. http://your-blog-url/wp-json/wp/v2/posts?filter[posts_per_page]=3
Fetching Particular Post
You can fetch any particular post by its id. http://your-blog-url/wp-json/wp/v2/posts/67
Here 67 is the id of the post.
Filtering Fields
As you have seen in above JSON data that there are several fields that we don’t require. So with the help of REST API – Filter Fields plugin you can filter few fields. For example you want to fetch only post’s id and title then it can be done by using following URL. http://your-blog-url/wp-json/wp/v2/posts?fields=id,title
回答5:
First, you have to install WordPress Rest API v2 on your wordpress. You can fetch information about all the post on your blog by the following URL. It will return a JSON response that contains all the information about your blog.
http://your-blog-url/wp-json/wp/v2/posts
for example http://www.blueappsoftware.in/android/wp-json/wp/v2/posts
Now you can call this url from android using retrofit / volley / httpconnection. I will suggest you to use retrofit. You can create you own Custom UI design on android to show blog post. You can get reference from here- http://www.blueappsoftware.in/android/blog/get-wordpress-post-in-android-app/