URL and Link repository

Tuesday, February 20, 2018

Android: HttpURLConnection java.io.FileNotFoundException

This is a nasty little error that I was seeing in the Log Cat while debugging my Android app.
I was using
//letterStrings is an ArrayList() populated with 'aaa','bbb','ccc','ddd'
JSONArray jsonArray = new JSONArray(letterStrings);
//jsonArray looks like this: ['aaa','bbb','ccc','ddd'];
sNotes = 'These are my notes';
URL url = null;
HttpURLConnection urlConnection = null;
String link = "http://somewebsite.com?&userID=3&jsonArray="+jsonArray+"¬es="+sNotes;
// The above looks like this when resolved: "http://somewebsite.com?&userID=3&jsonArray=['aaa','bbb','ccc','ddd']¬es=These are my notes";
url = new URL(link);
urlConnection = (HttpURLConnection) url.openConnection();
Funny thing is that the resolved string link runs just fine from the browser address bar.
From within Android using HttpURLConnection, the link results in a FileNotFoundException.
Why? You ask.
Because I must make sure that the values for jsonArray and sNotes are properly formatted for the URLconnection.
Therefore, I must first do the following before I put together the string link.
String urlNotes = URLEncoder.encode(snotes, "UTF-8");
String urlJson = URLEncoder.encode(jsonArray.toString(), "UTF-8");
Now the String link = "http://somewebsite.com?&userID=3&jsonArray="+urlJson+"¬es="+urlNotes;

Android Overlay a First Run Tutorial screen over the Main Activity

Here is a great link on how to create the overlay. Christian Peters