ServiceHandler with Android HttpURLConnection

I’m sharing my gist of ServiceHandler with the new post Android Marshmallow (API 21) HttpURLConnection

https://gist.github.com/mypapit/642de0968a01bf13a936b6f62e874a48

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
public class ServiceHandler {
public final static int GET = 1;
public final static int POST = 2;
public final static int PUT = 3;
public final static int DELETE = 4;
public final static int PATCH = 5;
public static int STATUS_CODE = 0;
Activity mActivity;
StringBuilder sbResponse;
public ServiceHandler() {
}
public String makeServiceCall(String stringUrl, int method, String urlParameters, Activity activity,
boolean isJsonRequest) {
this.mActivity = activity;
HttpURLConnection httpURLConnection = null;
try {
if (method == GET) {
if (urlParameters != null) {
stringUrl = stringUrl + "?" + urlParameters;
}
URL url = new URL(stringUrl);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setUseCaches(false);
httpURLConnection.setAllowUserInteraction(false);
httpURLConnection.setConnectTimeout(0);
httpURLConnection.setReadTimeout(0);
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setDoOutput(false);
httpURLConnection.connect();
} else if (method == POST) {
URL url = new URL(stringUrl);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setUseCaches(false);
httpURLConnection.setAllowUserInteraction(false);
httpURLConnection.setConnectTimeout(0);
httpURLConnection.setReadTimeout(0);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
if (urlParameters != null) {
if (isJsonRequest) {
httpURLConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
} else {
httpURLConnection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
}
httpURLConnection.connect();
OutputStream os = httpURLConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(urlParameters);
writer.flush();
writer.close();
os.close();
}
}
BufferedReader br = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
sbResponse = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sbResponse.append(line + "\n");
}
br.close();
STATUS_CODE = httpURLConnection.getResponseCode();
} catch (Exception ex) {
ex.printStackTrace();
try {
STATUS_CODE = httpURLConnection.getResponseCode();
} catch (IOException e) {
e.printStackTrace();
}
}
httpURLConnection.disconnect();
return sbResponse.toString();
}
}

Thoughts about Android Marshmallow 6.0 Permission model

I hope it is the rumored granular permission mode. But if it resembles anything in this SMH article, then it sounds like JavaME permission model all over again :p

Marshmallow also streamlines the “permissions” model for users to install and upgrade apps.
Users running certain apps will not need to grant any permissions when they install or upgrade, and the applications instead request permissions as it needs them.

Read more: http://www.smh.com.au/digital-life/digital-life-news/googles-android-60-will-be-known-as-marshmallow-20150817-gj1b2y.html

How to set Android *.apk mime-type for Nginx web server

Here’s a simple guide on how to add the correct mime-type for Android APK file for Nginx webserver.

sudo nano /etc/nginx/mime.types

In “mime.types” file, add this line within the “types” block

types {
     ...
     ...
     application/vnd.android.package-archive     apk;
     ...
     ...
}
     

Restart nginx server

sudo service nginx restart

Done!

Checking if website is Mobile-Friendly

Google has announced that they will take Mobile-Friendly site into account when indexing sites. Thus, it is prudent to ensure the particular website that you’re maintaining is mobile friendly.

There are two main tools released by Google for testing if the website is Mobile-Friendly:

  1. Google Mobile Friendly Test
  2. PageSpeed Insights

However, personally I like PageSpeed Insights tool better because it gave more in-depth explanation on how to improve my site.

mobile-friendly-test

Have fun trying out.

How to convert between sqlite2 and sqlite3 database

Here’s a short guide on how to convert between sqlite2 to sqlite3 database file:

sqlite2 /path/to/mysqlite2.db .dump > backupfile
sqlite3 /path/to/mynewsqlite3.db < backupfile

Using the same method, you can convert sqlite3 db to sqlite2 db too!

p/s: Why you need to convert? because embedded device (read: iPhone and Android) only supports sqlite3 database, while PHP 5 by default supports sqlite2 database.

Thus, this method provide a convenient way to convert between the two different version of sqlite db format.

Recommended Reading

My new Android app development machine – running on Ubuntu Precise

I always wanted a smooth development machine which can run multiple virtual machine (VM) at once, which is a must when testing app that is meant to be portable across different platform. So just before Ramadhan (in July), I bought 8GB RAM and 1TB hdd from a local pc shop and proceed to set-up my development machine.

The machine already has 4GB ram so adding the extra RAM bumped it to 12GB. The machine I was running is an older Intel Core i5, which is decent enough to execute all the VM that is necessarily when developing and testing the app.

 

with all those RAM, seems it’ll be quite a while before I use the swap space again.