2014-01-20 67 views

回答

7

謝謝Gregra幫助我。我找到了解決方案。 我安裝的WordPress網站WordPress插件JSON API和Android中提到這個鏈接http://www.learn2crack.com/2013/10/android-json-parsing-url-example.html代碼應用

+0

嗨,我需要從wordpress中獲取帖子列表。什麼是從WordPress中檢索帖子的json網址。 – Kirthiga

+0

您需要在wordpress中安裝任何JSON API插件。我安裝了http://wordpress.org/plugins/json-api/。你會得到所有json網址從插件設置檢索wordpress內容 – Arun

+0

如何從WordPress博客檢索帖子? – Kirthiga

7

你想要做的是從你的WordPress創建一種REST API來返回對你的Android HTTP請求的JSON響應。要做到這一點,首先爲Android,您可以參考這篇文章:

Make an HTTP request with android

然後,服務器端(你的WordPress),你將需要添加一個插件來處理您的API請求。要做到這一點,創建一個名爲API-endpoint.php您的wp-content /插件內部文件,並使用這樣的:

<?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(); 

然後通過你的WordPress管理界面啓用API_Endpoint插件,不要忘了衝你的永久鏈接。

之後,你就可以做出API請求:

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

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

編輯

要獲得WordPress的類別,例如參考這裏 - http://codex.wordpress.org/Function_Reference/get_categories

+0

嗨格雷格拉,謝謝你的迴應。我對android很陌生,而且我沒有太多編程技能可以這樣做。你可以請分享一個示例代碼,以顯示如何從WordPress的帖子檢索郵政類別? – Arun

+0

這是否有幫助 - http://codex.wordpress.org/Function_Reference/get_categories? – Gregra

+0

我一直在尋找這個代碼的高低,非常感謝。 –

2

在使用JSON API插件是非常糟糕的從WordPress的發回數據,Android應用程序,的情況下,雖然它給預期結果,但你真的看到了什麼是json查詢的結果?

就檢查它,請在瀏覽器:www.yourwebsite.com/api/get_posts/

,並檢查你會得到,這會查詢設置爲默認的所有職位或職位的數目你的WordPress的儀表板,並會發送所有關於他們的數據作爲一個JSON字符串,我試圖查詢10個帖子和JSON字符串的大小約爲150KB想象一下,只需10個帖子,用戶將不得不下載所有的每一個只有10個職位。

解決方案:你應該查詢服務器端的職位,併發送回Android應用只有你要使用的數據,例如:標題,縮略圖,摘錄....

怎麼辦呢?

1 - 做一個PHP文件在WordPress目錄,並使其接近

2 - 在它發佈的值可以從Android(這將由您在android系統設置知道你想要查詢的類型,比如帖子數量和帖子類型...)使用根據查詢的entites WordPress的功能部分-2

4-

3-查詢職位只與您要使用的數據生成自己的JSON字符串。

5呼應JSON字符串回至Android

現在,如果我只想要冠軍和10個職位的縮略圖的鏈接,JSON字符串大小將左右2KB

有差別:-)

您可以使用JSON API Auth註冊並登錄用戶很容易/快速的實施和使用。

0

我認爲這是更好的,因爲使用WordPress的休息API,您需要使用Wordpress 4.7或更高版本,或在以前的版本中安裝Rest Api plugin。然後,您需要在wordpress中配置固定鏈接,這將使其他端點能夠正常工作。

對於小型化以及customice您可以安裝Rest api filter fields plugin看到波紋管例如輸出JSON:

抓取後

指定數量爲了獲取職位的指定號碼,您可以使用每個後頁面過濾器。以下網址只會抓取3個帖子。 http://your-blog-url/wp-json/wp/v2/posts?filter[posts_per_page]=3

擷取特定職位

您可以通過獲取其ID任何特定的職位。 http://your-blog-url/wp-json/wp/v2/posts/67

這裏67是帖子的ID。

過濾領域

正如你在上面的JSON數據有,我們並不需要幾個領域都看到了。因此,藉助REST API - Filter Fields插件,您可以過濾幾個字段。例如,您只想獲取帖子的ID和標題,然後可以使用以下URL完成。 http://your-blog-url/wp-json/wp/v2/posts?fields=id,title

+0

如何使用WP-JSON API獲取縮略圖? –

0

首先,您必須在wordpress上安裝WordPress Rest API v2。您可以通過以下網址獲取有關博客上所有帖子的信息。它將返回一個JSON響應,其中包含有關您的博客的所有信息。

http://your-blog-url/wp-json/wp/v2/posts 

for example http://www.blueappsoftware.in/android/wp-json/wp/v2/posts 

現在,您可以使用retrofit/volley/httpconnection從android中調用此URL。我會建議你使用改造。您可以在android上創建自己的自定義UI設計以顯示博客文章。你可以從這裏得到參考 - http://www.blueappsoftware.in/android/blog/get-wordpress-post-in-android-app/

相關問題