Author Archives: djodjo

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


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
                   .getMethod("install", File.class, long.class)
                   .invoke(null, httpCacheDir, httpCacheSize);
        catch (Exception httpResponseCacheNotAvailable) {


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

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

… and binaries(deb):

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

Activity Layout Fragment attribute class: vs android:name

In case you wonder whether to use:

<fragment class="myapp.MyFragment" ..


<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,;
if (fname == null) {
    fname = a.getString(;

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:

Some more info on permalinks and other url stuff:

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


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


Android unique device identifiers, serial number and ANDROID_ID

There has been some discussions about unique device identifiers in Android.

Basically there are 3 main options (starting from the least recommended):

MAC Address or TelephonyManager –  not recomended

The mac address from WiFi or Bluetooth, however this is the best option as some devices does not have the specific hardware(wifi or bt), or depending on its status (off/disabled) mac cannot be obtained.

ANDROID_ID (from API 3) – can change after system update

Settings.Secure.ANDROID_ID is

A 64-bit number (as a hex string) that is randomly generated on the device’s first boot and should remain constant for the lifetime of the device. (The value may change if a factory reset is performed on the device.)

Constant Value: “android_id”
It can be obtained grammatically using this:
Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID)


Serial Number (from API 9) – recommended

android.os.Build.SERIAL  –

A hardware serial number, if available. Alphanumeric only, case-insensitive.

is the serial number you see in the DDMS view in Eclipse or from the command “adb devices”. This can be obtained pragmatically from



Its simple if you do not care to identify hardware  but installation go for ANDROID_ID, otherwise use android.os.Build.SERIAL.


Here is a simple example of serial number and ANDROID_ID before and after android update.

Executed command:

adb devices

Executed code:

Log.i("TEST","android.os.Build.SERIAL: " + android.os.Build.SERIAL);
Log.i("TEST","Settings.Secure.ANDROID_ID: " + Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID));

On Build: jwr66v


List of devices attached
0039f663df121556        device


I/TAG(8122): android.os.Build.SERIAL: 0039f663df121556
I/TAG(8122): Settings.Secure.ANDROID_ID: 5b79c0300c556842

On Build: jwr66y


List of devices attached
0039f663df121556        device


I/TAG(2861): android.os.Build.SERIAL: 0039f663df121556
I/TAG(2861): Settings.Secure.ANDROID_ID: 3dcc3d9765e7f1db


New OTA JWR66V->JWR66Y and JSS15J->JSS15Q for Nexus devices

Today I received a new ota update for my GN (maguro yakju):

for takju:

This is for version JWR66V to update to JWR66Y(android-4.3_r1.1) for Galaxy Nexus, Nexus 4/7/10 and for new Nexus 7 the update is from JSS15J to JSS15Q(android-4.3_r2.2).

of course all the full images can be found at

and of course those + deodexed and insecure/debug boot at

all changelogs can be downloaded/browsed at

Get packages from IProject in eclipse using IJavaProject

When working with Eclipse and especially when developing plug-ins you will deal with the org.eclipse.core.resources.IProject interface.

This however does not give you an option to get the packages used in the project. For that you will need org.eclipse.jdt.core.IJavaProject interface which is a completely different interface and cannot just get it from a cast from IProject, however you can:

IProject proj = (IProject)selectionResult;

IJavaProject jProj = JavaCore.create(proj);

then of course you can use jProj.getPackageFragments() to get app the package elements in the project.

You can also check if project is actually java project before creating the IJavaProject:

if(JavaProject.hasJavaNature(proj)) ...



Android Eclipse @Override error

Have you ever experienced multiple errors when you import a project into a new environment and all the errors are on @Override methods smth like:

The method Xxx(zz) of type AAA.BBB must override a superclass method

Also when you remove “@Override” all goes fine?

Well this might be if you are compiling with Java 5 (or setting the compiler’s -source option to expect Java 5 source) . In Java 5 you could not use the @Override annotation with methods that implemented an interface, only with actual overrides of methods from a super class. This changed in Java 6 so that you can now use it in both contexts.

So a solution is just to compile with java 6+.

To set this in eclipse:


in “JDK Compliance” set “Compiler compliance level” to 1.6.