在我的 android 应用程序中从 WordPress 获取帖子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21231814/
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-08-20 04:11:01  来源:igfitidea点击:

Getting posts from WordPress in my android app

androidwordpress

提问by Arun

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.

我是 Android 开发的新手,我正在尝试制作一个应用程序,它只会显示来自 WordPress 网站的帖子类别和帖子。任何人都可以帮助我,请。

采纳答案by Arun

Thanks Gregra for helping me. I found a solution. I installed WordPress plugin JSON APIin wordpress site and referred this link http://www.learn2crack.com/2013/10/android-json-parsing-url-example.htmlto code in Android App

感谢格雷格拉帮助我。我找到了解决方案。我在 wordpress 站点中安装了 WordPress 插件JSON API并引用此链接http://www.learn2crack.com/2013/10/android-json-parsing-url-example.html在 Android 应用程序中编码

回答by Gregra

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:

您想要做的是从您的 WordPress 创建某种 REST API,以将 JSON 响应返回给您的 Android HTTP 请求。为此,首先对于 Android,您可以参考这篇文章:

Make an HTTP request with android

使用 android 发出 HTTP 请求

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:

然后,对于服务器端(您的 WordPress),您必须添加一个插件来处理您的 API 请求。为此,请在 wp-content/plugins 中创建一个名为 api-endpoint.php 的文件,并使用以下内容:

<?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 hiHyman 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.

然后通过您的 WordPress 管理界面启用 API_Endpoint 插件,不要忘记刷新您的永久链接。

After that you'll be able to make API requests to:

之后,您将能够向以下对象发出 API 请求:

http://example.com/api/videos/12

http://example.com/api/videos/12

or

或者

http://example.com/api/product/4

http://example.com/api/product/4

Edit

编辑

To get WordPress categories for example reference here - http://codex.wordpress.org/Function_Reference/get_categories

要在此处获取 WordPress 类别示例参考 - http://codex.wordpress.org/Function_Reference/get_categories

回答by Salam El-Banna

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??

如果将数据从 wordpress 发送回 android 应用程序,使用 JSON APi 插件是非常糟糕的,虽然它给出了预期的结果但是你真的看到 json 查询的结果是什么吗?

Just check it, enter in your browser: www.yourwebsite.com/api/get_posts/

只需检查一下,在浏览器中输入: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 150KBimagine, just 10 posts, the user will have to download all them every time to get just 10 posts.

并检查您将得到什么,这将查询您在 wordpress 仪表板中设置为默认的所有帖子或帖子数量,并将有关它们的所有数据作为 json 字符串发送,我尝试查询 10 个帖子和 json 的大小想象一下,字符串大约为150KB,只有 10 个帖子,用户每次都必须下载所有这些帖子才能获得 10 个帖子。

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

1- 在您的 wordpress 目录中创建一个 php 文件并使其可访问

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

2-在其中接收来自android的发布值(这些将由您在android中设置以了解您想要的查询类型,例如帖子数量和帖子类型......)

3- query posts using wordpress functions according to the query entites in part-2

3- 根据第 2 部分中的查询实体使用 wordpress 函数查询帖子

4- Generate your own json string with only the data you want to use.

4- 仅使用您要使用的数据生成您自己的 json 字符串。

5- echo the json string back to android

5- 将 json 字符串回显给 android

Now if I want only the title and the thumbnail link of 10 posts, the json string size will be about 2KB

现在,如果我只想要 10 个帖子的标题和缩略图链接,则 json 字符串大小将约为2KB

that makes a difference :-)

这有区别:-)

You can use the JSON API Authto register and login user it is easy/fast to implement and use.

您可以使用JSON API Auth来注册和登录用户,它易于/快速实施和使用。

回答by Lorenzo Lopez

I think this is better, for using wordpress rest api you need to use Wordpress 4.7 or higher, or install the Rest Api pluginin previous versions. Then you need to configure Permalinks in wordpress, this will make rest api endpoints to work.

我认为这样更好,要使用 wordpress rest api,您需要使用 Wordpress 4.7 或更高版本,或者在以前的版本中安装Rest Api 插件。然后你需要在 wordpress 中配置 Permalinks,这将使 rest api 端点工作。

For reduce the size and customice the output json you may install the Rest api filter fields pluginsee the bellow example:

为了减小大小和自定义输出 json,您可以安装Rest api 过滤器字段插件,请参阅以下示例:

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

要获取指定数量的帖子,您可以使用 post-per-page 过滤器。以下 URL 将仅获取 3 个帖子。 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

您可以通过其 id 获取任何特定的帖子。 http://your-blog-url/wp-json/wp/v2/posts/67

Here 67 is the id of the post.

这里 67 是帖子的 id。

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

正如您在上面的 JSON 数据中看到的,有几个我们不需要的字段。因此,借助 REST API – 过滤字段插件,您可以过滤一些字段。例如,您只想获取帖子的 id 和标题,然后可以使用以下 URL 来完成。 http://your-blog-url/wp-json/wp/v2/posts?fields=id,title

回答by Kamal Bunkar

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.

首先,您必须在 wordpress 上安装 WordPress Rest API v2。您可以通过以下 URL 获取有关您博客上所有帖子的信息。它将返回一个 JSON 响应,其中包含有关您博客的所有信息。

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/

现在您可以使用改造 / volley / httpconnection 从 android 调用此 url。我会建议你使用改造。您可以在 android 上创建自己的自定义 UI 设计以显示博客文章。您可以从这里获得参考 - http://www.blueappsoftware.in/android/blog/get-wordpress-post-in-android-app/