Consuming a REST service from Java (Android)

The time finally came when I need to consume a service from my Android app. As I expected this is not as easy as with JavaScript. Strict types, threads and craziness come all into play for this simple task.

The first thing I learned about making a request to a REST service was to use the apache library:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public String makeRequest(url) {
  // HttpRequestBase is the parent of HttpGet, HttpPost, HttpPut
  // and HttpDelete
  HttpRequestBase request = new HttpGet(url);
  // The BasicResponseHandler returns the response as a String
  ResponseHandler<String> handler = new BasicResponseHandler();

  String result = "";
  try {
    HttpClient httpclient = new DefaultHttpClient();
    // Pass the handler and request to httpclient
    result = httpclient.execute(request, handler);
  } catch (ClientProtocolException e) {
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  }

  return result;
}
Read More

Java code static analysis with Pmd

Pmd is a tool for running code static analysis for multiple languages. The first thing you need to use it is download it from Pmd’s website. Clicking the download button will download a zip file. Uncompress that zip and you will have all you need.

Running code static analysis

Once you have pmd on your computer you can analyse your code using this command:

1
<path to pmd>/bin/run.sh pmd -d <src folder> -l java -f <reporting format> -R <rules>
Read More

Using Android lint to check for errors in your code

Android’s lint tool allows you to find common issues in your code by running code static analysis against your project. This tool performs some Android specific checks that some other code static analysis tools can’t do for you. The lint tools comes with the Android SDK under tools/lint. In its most simple form you can use this command:

1
lint <Android project folder>

I like this form because I can easily plug it to my CI system:

1
lint <Android project folder> --exitcode
Read More

Detect when Android emulator is ready

I am writing an Android app, so as any good developer I want to have some tests in place that run continuously to make sure my app doesn’t break. The thing about Android is that I need an emulator in order to run my unit tests, so I need a way to start the emulator and detect that it is ready to be used. To do this we need to verify if the boot animation has finished using this command:

1
adb shell getprop init.svc.bootanim

Now, the only thing we need to do is call this command constantly until we get “stopped”:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/env bash

# Kill emulator
adb -s emulator-5554 emu kill

# Start the emulator
emulator -avd NexusOne -gpu on -qemu -enable-kvm &

# Don't exit until emulator is loaded
output=''
while [[ ${output:0:7} != 'stopped' ]]; do
  output=`adb shell getprop init.svc.bootanim`
  sleep 1
done
Read More

Android Logcat Unexpected value from nativeGetEnabledTags: 0

Every time I try to use logcat to debug my android App I get this message a lot:

1
W/Trace &nbsp; ( 1264): Unexpected value from nativeGetEnabledTags: 0

This apparently is some kind of bug. This makes it really hard to read the logs I really care about so I am now using this command to filter all those entries:

1
adb logcat | grep -v .*nativeGetEnabledTags.*
Read More

Failed to load libGL.so

I was having this problem while trying to run the Android emulator on my machine. To fix it you can use this command for Fedora:

1
sudo yum install mesa-libGL-devel

or this command for Ubuntu:

1
sudo apt-get install libgl1-mesa-dev
Read More

Publishing an Android app to the play store

There is extensive documentation of what you need to do to release your app in the Android documentation. It is so much documentation that I wanted to gather a more straight forward list of steps.

Create a self-signed key

To generate your key, use this command:

1
keytool -genkey -v -keystore my-release-key.keystore -alias alias_name -keyalg RSA -keysize 2048 -validity 10000

You will be prompted for a password (You might want to use a strong one) for your keystore. Then you will be asked for some information and finally another password for your key (You can use the same as the one you used for the keystore). That will generate a file called my-release-key.keystore. Keep this file in a safe place because you will need it every time you update your app.

Read More

Instant mock testing with PowerMock - Book Review

A few weeks ago someone from Packt publishing contacted me offering me a free copy of Instant mock testing with PowerMock if I accepted to write a review afterwards. Since I’ve been lately suffering with Unit Testing on Android I accepted and here is my review.

The book is really short and focused, which made it a very fast ready. I like this because I don’t really want to spend a lot of time learning how to mock something, it should be really straight forward.

Read More

Big O Notation

Big O notation is a way to represent how well an algorithm scales as the amount of data involved increases. I will go over some examples of the most common orders and try to explain what each one means:

O(1)

This means that the algorithm will perform the same way no matter how long the data set is. This is usually the case for hash tables. Here is an example of an O(1) algorithm:

1
2
3
4
5
var myArray = [];

function addToArray(num) {
  myArray.push(num);
}
Read More

Increase number of files Command-T will search

I was having a problem with Vim’s Command-T plugin, where it didn’t find some files. Searching through the documentation I found the problem was that Command-T will search a maximum of 10,000 files by default. This can be changed by adding this to your .vimrc:

1
2
" Increase the number for files Command-T will search "
let g:CommandTMaxFiles=50000
Read More