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();
}
}

Easyrec – Integrate Recommender Engine in your website – open source

Easyrec is an open source recommender engine which can be trained and customized to provide personalized recommendations using REStful Web Service.

easyrec-engine
Easyrec exposes its functionality through REST API which provides several interaction types:

Actions

  1. view
  2. buy
  3. sendaction

Recommedations

  1. other users also viewed
  2. other users also bought
  3. items rated good by other users
  4. recommendations for user
  5. related items
  6. action history for user

Community Rankings

  1. most viewed items
  2. most bought items
  3. most rated items
  4. best rated items
  5. worst rated items

The list of recommendations is returned in XML and JSON notation to be further processed by your web application.

Easyrec API can be accessed from its main website http://easyrec.org/ or could be installed alongside with the web application on your own server. Easyrec require at least Java 1.5 and MySQL server for its functionality.

Download easyrect from: http://easyrec.org/recommendation-engine

Does anyone here still uses Java Applets?

When I was studying in the university (more than 10 years ago), I learn how to build and embed Java Applets on web pages using Java 1.0 (after that Java 1.2), the functionality is quite limited since there’s no default 3D support in Java yet. Compared to Flash (then Flash 3.0 during that time), Java Applet is more suitable for math and scientific application, generating graphs and kaleidoscope base on mathematical formula, and everything.

duke java logo

Heck, Netscape Navigator still rules the day during that time. But what about now? With the technology explosion, does anyone here still use Java Applets?

How to make JavaME .jar files downloadable from Apache Web Server

Mobile application developers may have realize that one of the best (and recommended) way to distribute their JavaME/J2ME application is by hosting it on a website. This makes it easier for potential users to navigate and download the .jad or .jar files from their phone browsers and to execute it directly.

However, some web servers are not configured to handle .jar / .jad file requests, eventually leading to failed install response received by the mobile users.

To remedy this, .jad / .jar files need to be associated with the correct MIME type. In Apache, you can do this by creating ‘.htaccess’ file in your web directory, and inserting these lines :

# JavaME
AddType text/vnd.sun.j2me.app-descriptor .jad
AddType application/java-archive .jar

Afterwards, safe the file. The web server should behave accordingly when requests are made to either of these files. For other web servers, please refer to their respective manual or online-help on how to change document MIME type.