the_title()
およびthe_content()
は PHP 関数であるため、Java 自体から呼び出すことはできません。これらの値を JSON として返す Web サービスを記述し、それを Android アプリで使用できます。
// define hook for ajax functions
function core_add_ajax_hook() {
// don't run on admin
if ( !is_admin() ){
do_action( 'wp_ajax_' . $_REQUEST['action'] );
}
}
add_action( 'init', 'core_add_ajax_hook' );
// function to return latest title and content as JSON
function latest_post() {
// array for values
$json = array();
// get values for JSON array
if (have_posts()) : the_post();
// put values into JSON array
$json['the_title'] = get_the_title();
$json['the_content'] = get_the_content();
endif;
// encode to JSON and return
echo htmlentities(json_encode($json), ENT_NOQUOTES, 'UTF-8');
}
// hook function on request for action=latest_post
add_action('wp_ajax_latest_post', 'latest_post');
この情報は、次のようにリクエストすることで取得できます。
http://yoursite.com/wp-load.php?action=latest_post