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#








As an Amazon Associate I earn from qualifying purchases.

Java: read URL to String text

The blog post describes a method for reading data from a URL in Java.
It explains how to establish a stream connection, read data into a buffer, and convert that data into a String format.

The method is particularly useful for developers needing to fetch web data in Java applications.

I was using it here to retrieve a JSON feed from Flickr.





As an Amazon Associate I earn from qualifying purchases.

JSON vs. XML

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





As an Amazon Associate I earn from qualifying purchases.

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!



As an Amazon Associate I earn from qualifying purchases.

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







As an Amazon Associate I earn from qualifying purchases.

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


As an Amazon Associate I earn from qualifying purchases.

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+&@#/%=~_|]";
}


As an Amazon Associate I earn from qualifying purchases.

Project Planning

"Planning is a good preparation for an inevitable change of direction that throws out any plans made." ~Uki


As an Amazon Associate I earn from qualifying purchases.

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









As an Amazon Associate I earn from qualifying purchases.

My favorite quotations..


“A man should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly. Specialization is for insects.”  by Robert A. Heinlein

"We are but habits and memories we chose to carry along." ~ Uki D. Lucas


Popular Recent Articles