Tuesday, March 31, 2015

Java: JSON

In this tutorial we will parse Flickr JSON


https://api.flickr.com/services/feeds/photos_public.gne?tags=mountains&tagmode=all&format=json#






Java: read URL to String text

To process JSON feed from Flickr we will have to get text from URL, this can be done with this simple utility



JSON vs. XML

Comparison of JSON and XML syntax (from http://json.org/example)



Friday, March 27, 2015

Android: adb: filtering logcat

Usually I use IDE, but sometimes I have to create a very precise LOGCAT FILTER in terminal window.
Here is how I create a tag:
private static final String TAG = SomeActivity.class.getSimpleName();
 Log.d(TAG, "some description");
You could use getCannonicalName
Here I have following TAG filters:
  • any (*) View - VERBOSE
  • any (*) Activity - VERBOSE
  • any tag starting with Xyz(*) - ERROR
  • System.out - SILENT (since I am using Log in my own code)
Here what I type in terminal:
$  adb logcat *View:V *Activity:V Xyz*:E System.out:S

please +1 on stockoverflow:

http://stackoverflow.com/a/29307682/3566154









It takes time and effort to create tutorials, please support my efforts with a couple-dollar donation, any amount will be greatly appreciated and highly motivating!

Thursday, March 26, 2015

Linux: RPM

Here is couple of functions I use when working with RPM packages:


Show containing "postg"

rpm -qa | grep postg


Show top 20:

rpm -qa | tail -20



Install package:

rpm -ivh somed-1.0.0.armv7l


Uninstall:

rpm -e somed-1.0.0.armv7l





Thursday, March 19, 2015

Java Map interface

In this tutorial we will discuss various implementations of Java Map interface. Java Map is one of the most commonly used datatypes when you want to store items by key.


HashMap


HashMap should be only used when the objects we store as KEYS have implemented the method hashCode in such way that it give unique key for a given object.

 @Override public int hashCode() {


Let's say we have an object USCitizen

name,
socialSecurityNumber


Map myMap = new HashMap();

Tuesday, March 10, 2015

Java: RegEx: WebSpider

In this tutorial we will create a basis for a WebSpider program that fetches all links from the given Web page, checks which have extensions of interest and downloads these files.

The result of the app will be a folder with Google Earth map overlays:


/Users/uki/Desktop/KMZ
├── 1.kmz
├── 10.kmz
├── 11.kmz
├── 12.kmz
├── 13.kmz
├── 14.kmz
├── 15.kmz
├── 16.kmz
├── 17.kmz

├── 18.kmz


We will use basic java networking classes:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

and regular expressions:

ublic class RegExConstants {

   /**    * @param extensions    * @return    *  (                -- start of main grouping    *     [^\s]+        -- must contains one, or many strings (but not white space)    *     (             -- start of extension grouping    *        \.         -- existence of a dot, eg.: .kmz    *        (?i)       -- NOT case sensitive for the next group    *        (kmz|kml)  -- kmz OR kml strings    *     )$            -- should exist on the end    *  )                -- end of main grouping    */   public static String fileExtensionPattern(String[] extensions) {

      StringBuilder sb = new StringBuilder("");

      if (extensions.length > 0) {
         int n = 0;
         for (String extension : extensions) {
            if (n > 0) {
               sb.append("|"); // append OR            }
            sb.append(extension);
         }
      }
      String pattern = "([^\\s]+(\\.(?i)(" + sb.toString() + "))$)";
      System.out.println("fileExtensionPattern: " + pattern);
      return pattern;
   }

   public static final String anchorTagPattern = "<a *href=\"(.+?)</a>";
   public static final String urlPattern = "(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
}

Tuesday, March 3, 2015

Java: running with Maven



Compiling with Maven

note:

  • mvn clean install
  • location of created jar



uki@ WebSocketClientServer $ mvn clean install

[INFO] Scanning for projects...

[INFO]                                                                         

[INFO] ------------------------------------------------------------------------

[INFO] Building WebSocket-GlassFish 1.0-SNAPSHOT

[INFO] ------------------------------------------------------------------------

...

.to /Users/uki/.m2/repository/edu/clcillinois/cit137/WebSocket-GlassFish/1.0-SNAPSHOT/WebSocket-GlassFish-1.0-SNAPSHOT-jar-with-dependencies.jar

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO] ------------------------------------------------------------------------







Using variables to store long values



uki@ ~ $ export WEBSOCKET_JAR=~/.m2/repository/edu/clcillinois/cit137/WebSocket-GlassFish/1.0-SNAPSHOT/WebSocket-GlassFish-1.0-SNAPSHOT-jar-with-dependencies.jaruki@ ~ $ echo $WEBSOCKET_JAR/Users/uki/.m2/repository/edu/clcillinois/cit137/WebSocket-GlassFish/1.0-SNAPSHOT/WebSocket-GlassFish-1.0-SNAPSHOT-jar-with-dependencies.jar




Starting SERVER



$ java -cp $WEBSOCKET_JAR edu.clcillinois.websocket.server.WebSocketServer -h localhost -p 8025




Setting homeName to: localhostStarting the server.Mar 03, 2015 3:56:54 PM org.glassfish.tyrus.server.ServerContainerFactory createINFO: Provider class loaded: org.glassfish.tyrus.container.grizzly.GrizzlyEngineMar 03, 2015 3:56:54 PM org.glassfish.grizzly.http.server.NetworkListener startINFO: Started listener bound to [0.0.0.0:8025]Mar 03, 2015 3:56:54 PM org.glassfish.grizzly.http.server.HttpServer startINFO: [HttpServer] Started.Mar 03, 2015 3:56:54 PM org.glassfish.tyrus.server.Server startINFO: WebSocket Registered apps: URLs all start with ws://localhost:8025Mar 03, 2015 3:56:54 PM org.glassfish.tyrus.server.Server startINFO: WebSocket server started.
Type 'q' and Enter to stop server: 



Starting Client(s)




uki@ CNH_PROD $ java -cp $WEBSOCKET_JAR edu.clcillinois.websocket.client.WebSocketClient -h localhost -p 8025
[DEBUG] 2015-03-03 16:02:44,354 edu.clcillinois.websocket.client.WebSocketClient main - Starting WebSockets clientSetting homeName to: localhost



Monitoring Java processes and Killing them


$ ps | grep java
30227 ttys001    0:00.00 grep java

30203 ttys002    0:01.37 /usr/bin/java -cp ~/.m2/repository/edu/clcillinois/cit137/xyz.jar edu.clcillinois.websocket.server.WebSocketServer -h localhost -p 8025

$ kill -9 30203 uki@ SimpleWebSocketServer $ ps | grep java30230 ttys001    0:00.00 grep java


Maven pom.xml example

note: 
  • group id
  • artifact id
  • version
  • start-class















Maven Dependencies


note:
  • group id
  • artifact id
  • version








Maven build


note: 
  • plugins 
  • Java version 1.6, 1.7 or 1.8
  • TODO change to Java 1.6 and observer what fails
  • jar with dependencies







Friday, February 27, 2015

Android: UI: horizontal divider

In this recipe we will add a thin gray line divider to our UI



       <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_marginTop="6dp"
            android:alpha="0.5"
            android:background="@color/bar_grey_light"/>



Android: xmlns res-auto

You can use "res-auto" URI instead of defining custom packages:

so...

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:my_tag="http://schemas.android.com/apk/res-auto"

instead of...

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:my_tag="http://schemas.android.com/apk/res/com.my_company.xyz"



and then use your tag...

       <com.my_company.xyz.control.MyPanelView
                android:id="@+id/jdhfkjsd"
                style="@style/kjdlksjdhjhk"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                my_tag:header="@string/kjsdlfkdslkfj"