Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Daf XF
Courses Plus Student 233 PointsWhy is my Android code not working?
Hello,
I'm fairly new to Android programming but I can't find out why my code isn't working. I adapted the Java code to work with Android but it still isn't working. The code is working fine in Eclipse but in Android Studio it isn't. Here is my code:
-- Eclipse --
try {
URL TestURL = new URL("http://www.oracle.com");
BufferedReader bufferReader = new BufferedReader(
new InputStreamReader(TestURL.openStream()));
String outputCode;
while ((outputCode = bufferReader.readLine()) != null)
System.out.println(outputCode);
bufferReader.close();
}
catch (Exception e) {
System.out.println("Oops, something went wrong.");
}
-- Android Studio --
BTSend.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// I also tried to use a static URL such as the example above but this doesn't work either.
String ETURL = ETURLInput.getText().toString();
try {
URL TestURL = new URL(ETURL);
BufferedReader bufferReader = new BufferedReader(
new InputStreamReader(TestURL.openStream()));
String outputCode;
while ((outputCode = bufferReader.readLine()) != null)
TVCode.setText(outputCode);
bufferReader.close();
}
catch (Exception e) {
TVCode.setText("Oops, something went wrong.")
}
}
The weird thing is the code works fine in Eclipse but in Android Studio is isn't. Android Studio shows no errors and building the app goes fine but when I run in on my phone it catches a exception. I do have the Internet permission in my manifest and I also have the correct classes imported into Android Studio. Does anyone know why my code isn't working?
1 Answer
Jacob Bergdahl
29,119 PointsIt's very difficult to tell without seeing the error code. Have you tried debugging through the code? There are many things that can go wrong.