I am total noob at Android dev. I am still learning, and I am on very first step of my app dev.
I have this code working and it runs fine or regular Java, now I am trying to implement to Android OS.
In my code where it says TEST.openStream() I get Unhandled exception type IOException error.
package com.zv.android;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
public class ZipActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
URL TEST = new URL("http://www.google.com/");
BufferedReader in = new BufferedReader(new InputStreamReader(TEST.openStream()));
String inputLine;
int line=0;
while ((inputLine = in.readLine()) != null){
line++;
//System.out.println(line + "\t" + inputLine);
}
// in.close();
} catch(MalformedURLException e) {
//Do something with the exception.
}
}
}
Error message is simple: you need to catch the
IOException, this becauseURL.openStream()is declared asso by accepting the contract of the method you also accept the fact that you must handle this exception, this is how it works in Java. This is a checked exceptions, then it must be caught because this kind of exceptions represent situations that may arise and that your code must handle.
To catch it just add another case in your
trystatement:Just as a final note: you don’t need to catch it when you call the
openStream()method, you could state that the method that callsopenStream()will forward the exception to the caller but in the end of the call chain you will have to catch it in any case.