How to read from the Assets folder in an Eclipse Android application

Seeing errors like these?


java.io.FileNotFoundException
at android.content.res.AssetManager.openAsset(Native Method)
at android.content.res.AssetManager.open(AssetManager.java:315)
at android.content.res.AssetManager.open(AssetManager.java:289)

The problem is that Eclipse isn’t finding your file, because you may not be using AssetManager to retrieve plain-text files from the assets folder. Here’s how to fix this:

  1. Note that there is no file extension on text. When I pasted ‘text.txt’ into the Assets folder to begin with, it had no extension. I tried and tried to get the app to recognize that Assets had a text.txt file in there, and finally tried it with no file extension as it appeared in the folder view.
  2. The try/catch AND the throws declaration are necessary to handle Eclipse’s fiddly compiler.
  3. Don’t cry. If you still can’t figure out how to get Eclipse
    and your app to read the file, clean the project (Project –> Clean), make sure that the Assets folder is NOT on the build path, restart Eclipse, ensure you have no other errors (ADB, DDMS, anything), and try again. If it still doesn’t work, comment here and I’ll both try to help and make updates to the code.


public void openFile () throws IOException {
System.out.println("Starting openFile now");
AssetManager am = context.getAssets();
try {
InputStream is = am.open("text");
BufferedReader in = new BufferedReader(new InputStreamReader(is));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}

Useful links (none of which contain the entire solution, but all of which had some piece):

http://stackoverflow.com/questions/6039862/location-of-apk-file
http://stackoverflow.com/questions/5086539/reading-file-from-assets-directory-throws-filenotfoundexception
http://developer.android.com/reference/android/content/res/AssetManager.html
http://developer.android.com/reference/java/io/InputStream.html
http://stackoverflow.com/questions/5771366/reading-a-simple-text-file
http://stackoverflow.com/questions/10267594/how-to-access-
assets-folder-in-my-android-app

http://stackoverflow.com/questions/9674815/trouble-with-reading-file-from-assets-folder-in-android

Which JSON library should I use in my Android and Java projects?

JSON.org is where you start; this is the prepackaged JSON found in Oracle and Open Java Development Kits. The home page is a useful resource to see all the different JSON libraries out there for all different languages.

GSON is most useful for converting Java objects into their JSON representation. The Google libs can also convert a JSON representation into a Java object; this can be a game-changer for dealing with inheritance.

FlexJSON has a serious strength in web development. Because you can specify deep or shallow copies of objects, you can speed up transmission of information from backend Java code to your front end and client-facing architecture.

Jackson is almost certainly
the fastest JSON parsing library out there; it’s an active project, and has speed and flexibility to recommend it. I have started using it by default. Most useful is the fact that you can switch between a tree model and object mapping at will. It has a JSONFactory method that is extremely useful when parsing and manipulating JSON representations that need to be read, processed, and rewritten to provide data to an Android app.

JSON-lib is mostly focused on translation. Need to translate objects and data back and forth between Java objects, beans, servlets, and DynaBeans? This is the lib to use. Notably, it’s a bit heavy, as you might expect from a translation-centric library, so it’s probably best used when you’re managing legacy code or are a one-woman-shop.

Apache Virtual Hosts with SNI and SSL on Ubuntu 12.04 in Rackspace

Here’s a little howto: I was having the devil of a time earlier today configuring an SSL cert for a site Lorraine and I are working on right now. My problem is that I’ve never configured an SSL cert before, and proceeded to jump right on in with a whole lot of enthusiasm and zero knowhow.

It turns out that because the site we’re working on is on a Rackspace cloud server, and we’re hosting several sites on the same server using Apache virtual hosts to configure http requests via hostname as opposed to IP address, there is some extra configuration to be done. Add to that the fact that we’re serving secure and insecure content at the same hostname, and you have a recipe for a headache. So, here’s the way I did it.

First, let me list the useful tutorials and links, and then I’ll post the config files and examples that worked.

The most useful: http://www.tc.umn.edu/~brams006/selfsign_ubuntu.html

http://en.wikipedia.org/wiki/Server_Name_Indication

http://en.gentoo-wiki.com/wiki/Apache2/SSL_and_Name_Based_Virtual_Hosts

http://httpd.apache.org/docs/2.2/mod/mod_ssl.html

http://www.sslshopper.com/article-most-common-openssl-commands.html

Read this to understand why you’re doing this and what a certificate is: http://httpd.apache.org/docs/2.2/ssl/ssl_intro.html

Pro tips:

  1. You don’t need to turn on the ”
    Listen 443″ switch in apache2.conf; that’s already on and enabled with mod_ssl, the Apache module that handles SSL.
  2. If you keep seeing people telling you to edit your ssl.conf, what they mean in Ubuntu is /etc/apache2/sites-available/default-ssl. There’s already the stub there.
  3. Generate a CSR easily and simply by pasting this into a bash shell: openssl req -out CSR.csr -new -newkey rsa:2048 -nodes -keyout privateKey.key
  4. You have to create a symbolic link between the default-ssl in sites-available and sites-enabled like this:
  5. sudo ln -s /etc/apache2/sites-available/default-ssl /etc/apache2/sites-enabled/000-default-ssl

Ok, now here’s the example default-ssl file:




ServerAdmin webmaster@localhost
DocumentRoot /path/to/your/website/root/
r

Options FollowSymLinks
AllowOverride None


Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all

ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn

CustomLog ${APACHE_LOG_DIR}/ssl_access.log combined

Alias /doc/ "/usr/share/doc/"

Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128

SSLEngine on
SSLCertificateFile /path/to/cert
SSLCertificateKeyFile /path/to/key/generated/from/CSR
SSLCertificateChainFile /path/to/bundled/certs/
from/your/issuer


SSLOptions +StdEnvVars


SSLOptions +StdEnvVars

BrowserMatch "MSIE [2-6]" \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0
BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown



And here’s the trippy part: you actually don’t need to edit your current virtual host file (presuming you have it correctly configured to serve nonsecure content via port 80) which should be living in sites-available. I have the feeling that if I was trying to serve more than one secure site on this server that I’d need to configure a NameVirtualHost, but since I don’t, all requests on port 443 can just get shoved to the document root of the secured site. I’ll explore that later, I suppose.

How To Build A Ruby Server With RVM On Kubuntu 12.10

So I’m building a Ruby server right now for the first time. I’m going to list out here the resources I used as a skilled web dev to get this up and running right away. These links do assume that you know what you’re doing in the cloud, but if so, they’re the best way to get running fast.

http://guides.rubyonrails.org/getting_started.html
https://www.digitalocean.com/community/articles/how-to-install-ruby-on-rails-on-ubuntu-12-04-lts-precise-pangolin-with-rvm
This is the best link: http://www.andrehonsberg.com/article/install-rvm-ubuntu-1204-linux-for-ruby-193

And here are the steps:
n
Update, upgrade, dist-upgrade
ufw default deny and add 80 and ssh port
apt-get install build-essential git-core curl
bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer) echo '[[ -s "/home/andre/.rvm/scripts/rvm" ]] && source "/home/andre/.rvm/scripts/rvm"' >> ~/.bashrc
source ~/.bashrc
sudo apt-get install libsqlite3-dev
rvm pkg install zlib
rvm reinstall $(rvm list strings | tr "\n" ',')
rvm reinstall 1.9.3 --with-openssl-dir=/usr/local
rvm all-gemsets do rvm gemset pristine
gem install sqlite3-ruby -- --with-sqlite3-dir=/usr/local/lib
rvm gemset create rails31
rvm gemset use rails31
rvm current
gem list
echo "gem: --no-rdoc --no-ri" > ~/.gemrc
rvm pkg install openssl
sudo apt-get install -y libssl-dev
rvm reinstall $(rvm list strings | tr "\n" ',')
rvm all-gemsets do rvm gemset pristine
gem install rails
bundle install
rails new NewProject -d sqlite3
echo "gem '
therubyracer'" >> Gemfile