Monthly Archives: September 2013

Simple php xml-rpc client to wordpress

Quick example how to get categories:

<?php
        require("class-IXR.php");  
        $client = new IXR_Client('http://myblog.com/xmlrpc.php');

        $USER = 'admin';
        $PASS = 'mypassword';

        if (!$client->query('wp.getCategories','', $USER,$PASS))
        {  
            echo('Error occured during category request.' . $client->getErrorCode().":".$client->getErrorMessage());  
        }
        $cats = $client->getResponse();

        if(!empty($cats))
        {
            echo 'Categories:<br /><hr />';
            foreach($cats as $_cat) echo $_cat['categoryName']."<br />";
        }

?>

 adding a new post

<?php
        require("class-IXR.php");  
        $client = new IXR_Client('http://domain.com/xmlrpc.php');

        $USER = 'admin';
        $PASS = 'mypassword';

        $content['title'] = 'Test title '.mt_rand();
        $content['categories'] = array("NewCategory","Nothing");
        $content['description'] = '<p>Lorem ipsum dolor sit amet</p>';
        $content['custom_fields'] = array( array('key' => 'my_custom_fied','value'=>'yes') );
        $content['mt_keywords'] = array('foo','bar');

        if (!$client->query('metaWeblog.newPost','', $USER,$PASS, $content, true))
        {
            die( 'Error while creating a new post' . $client->getErrorCode() ." : ". $client->getErrorMessage());  
        }
        $ID =  $client->getResponse();

        if($ID)
        {
            echo 'Post published with ID:#'.$ID;
        }

?>

XML-RPC Api specs: http://codex.wordpress.org/XML-RPC_WordPress_API

Full list of method calls as declared in the code: Continue reading

Http Clients in Android

When you come to use HTTP you may wonder which class to use to send and receive data.

There are 2 main

Options:

Which one to use?

Apache http clients are big and flexible implementations, however exactly because of this reason (complexity) they are not actively supported by Android team.

HttpURLConnection on the other hand is lighter and can serve the needs of most apps. It is also continuously improved by the Android team. Since Android 4.0 (Ice Cream Sandwich) it also  includes a response cache. This is quite handy however it is include from API 13 so to enabled it without impact earlier versions reflection can be used:

try {
           File httpCacheDir = new File(context.getCacheDir(), "http");
           long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
           Class.forName("android.net.http.HttpResponseCache")
                   .getMethod("install", File.class, long.class)
                   .invoke(null, httpCacheDir, httpCacheSize);
        catch (Exception httpResponseCacheNotAvailable) {
       }}

 Finally

As also stated in this post:

Apache HTTP client has fewer bugs on Eclair and Froyo. It is the best choice for these releases.

For Gingerbread and better, HttpURLConnection is the best choice. Its simple API and small size makes it great fit for Android. Transparent compression and response caching reduce network use, improve speed and save battery. New applications should use HttpURLConnection; it is where we will be spending our energy going forward.

For more info on Http check http://web.djodjo.org/article/articles/android_articles/dev/web-and-services/http_access

Factory Images “razorg” for Nexus 7 [2013] (LTE)

The factory images for the new Nexus 7 (razorg) Android 4.3 (JLS36C) are out!

Official download link images(razorg): https://developers.google.com/android/nexus/images#razorg

… and binaries(deb): https://developers.google.com/android/nexus/drivers#razorg

Another download option + insecure boot image and deodexed system is here:

http://web.djodjo.org/article/download/android/ROM_images/factory_images_gn_razorg

Activity Layout Fragment attribute class: vs android:name

In case you wonder whether to use:

<fragment class="myapp.MyFragment" ..

or

<fragment android:name="myapp.MyFragment" ..

Well it doesn’t really matter. You can use both.

Lets see why in Activity.onCreateView source:

String fname = attrs.getAttributeValue(null, "class");
TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.Fragment);
if (fname == null) {
    fname = a.getString(com.android.internal.R.styleable.Fragment_name);
}

So it seems it first checks for ‘class’ and if not there then for ‘android:name’.

How Permalink Works

So you want to have something like /article/how_permalink_works/ instead of index.php?article=how_permalink_works, however keeping that you in your php you will have in $_GET['article'] the string "how_permalink_works". Well this is how you do it:

Put in your .htaccess:

RewriteEngine on
RewriteRule article/([0-9_a-zA-Z\-]+)/$ index.php?article=$1

Some more info on .htaccess: http://www.htaccess-guide.com/

Some more info on permalinks and other url stuff: http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/

SQLite and datetime notes

In SQLite3 there is no such datatype as DateTime or Date or Time. As noted here:

SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:

  • TEXT as ISO8601 strings (“YYYY-MM-DD HH:MM:SS.SSS”).
  • REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
  • INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.

Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions.

So a simple example:

1) To insert

insert into sometable values( null, datetime('now') );

which will save smth like: 2013-09-08 19:41:16

or

insert into sometable values( null, strftime('%s', 'now'));

which will save the UNIX time in seconds: 1378669277

Note that in Android for example you can use SQLiteDatabase.insert method and pass System.currentTimeMillis() in the ContentValues, however do not forget to trim the miliseconds.

2) To select

So depends how the date is saved you will get string or a number. I personnaly like to use the UNIX time and so save the date and integer type.

In that case simple:

select my_datetime from sometable

will return just numbers. It is useful when you need it for comparison or ordering but for the user it is better to get is as readable datetime string as:

select datetime(my_datetime, 'unixepoch') from sometable