I’m having a bit of trouble with my Android app.
The application seems to force close whenever I put in:
String fileContents = getFileContents("http://www.youtube.com/watch?v="+code);
fileContents = fileContents.substring(fileContents.indexOf("\'VIDEO_ID\': "+"\""+code+"\""),fileContents.indexOf("yt.setMsg({"));
String[] settings = fileContents.split("fmt_url_map");
settings = settings[settings.length-1].split("\"");
String map = settings[2];
map = map.replace("\\/", "/").replace("\\u0026", "&");
fmtMap = map.split("[,|]+");
hash = new HashMap<Integer, String>();
for(int i = 0; i< fmtMap.length/2; i ++) {
hash.put(new Integer(Integer.parseInt(fmtMap[2*i])),fmtMap[2*i+1]);
}
My getFileContents() function is:
public static String getFileContents(String path) {
InputStreamReader instream = null;
URL url = null;
URLConnection urlConn = null;
try {
url = new URL(path);
try {
urlConn = url.openConnection();
instream = new InputStreamReader(urlConn.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String separator = System.getProperty("line.separator");
StringBuffer contents = new StringBuffer();
try {
BufferedReader f = new BufferedReader(instream);
try {
String line = null;
while((line = f.readLine()) != null) {
contents.append(line);
contents.append(separator);
}
}
finally {
f.close();
}
}
catch (IOException ex) {
System.out.println(ex.toString());
}
String text = contents.toString();
return text;
}
I suspect it’s not working because I copied this directly from one of my Java projects, but I don’t understand why.
If anybody can help, that would be great!
😀
I first needed to add the following line in my AndroidManifest.xml:
After adding this line, the
getFileContents(String path)method returned an HTML string.The
fileContents.substringcall throws aStringIndexOutOfBoundsExceptionon this line:The expression
fileContents.indexOf("yt.setMsg({")returns -1, and it’s likely the otherindexOfcall is also returning -1.To diagnose these problems Logcat is your best friend!