1

I have problems with getting the html source code with an url. I am trying to develop an application which takes in an url and upon the button submit by the user, it takes in the url and make calls to the url provided to retrieve the html source code.

I have looked up on various methods of doing it, but have always been presented with errors using the codes provided.

Listed below is one of the codes which I have used but it doesn't seem to work for me, it will throw a Error: null in the exception when I use logcat to debug.

please pardon me if the question sounds simple, I'm new to programming. thanks in advance.

String htmlCode = "";

    try {
    URL url = new URL("http://www.google.com/");
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

    String inputLine;

    while ((inputLine = in.readLine()) != null)
        htmlCode += inputLine;

    in.close();
    } catch (Exception e) {
        Log.d(LOG_TAG, "Error: " + e.getMessage());
    }
Ariel Magbanua
  • 3,083
  • 7
  • 37
  • 48
simplified.
  • 11
  • 1
  • 2
  • 1
    Use the version of `Log.d()` that takes the `Exception` as the third parameter and post the full stack trace. – CommonsWare Jun 14 '11 at 22:36
  • I would honestly advise to check the following: 1- Check your Manifest to make sure you added permission to Internet Access. 2- Try e.toString() instead of e.getMessage(). – daniel_c05 Jan 04 '13 at 06:45

5 Answers5

0

That code seems to be functional and works for me. Can you post the exception/error you are getting when you run the code?

Adrian Rodriguez
  • 3,232
  • 2
  • 24
  • 34
  • Thanks for your replies. Hope this will answer citizen conn's query as well. I have added a Log.d(LOG_TAG, "html: " + inputLine); in the while loop to check the output. But it didn't seem to have any response, but instead. the Log.d(LOG_TAG, "Error: " + e.getMessage()); was ran. But it returns Error: null. – simplified. Jun 14 '11 at 20:21
0

At a glance, the code looks fine. Make sure you add the Internet permission in your AndroidManifest.xml.

<uses-permission android:name="android.permission.INTERNET" />
kabuko
  • 36,028
  • 10
  • 80
  • 93
0

I just ran this code and its working fine. The error must be coming from somewhere else?

citizen conn
  • 15,300
  • 3
  • 58
  • 80
0

Are you running this in the emulator? The emulator has DNS problems sometimes. It's possible the emulator can't find www.google.com, see this post:

Upgraded to SDK 2.3 - now no emulators have connectivity

Community
  • 1
  • 1
William Seemann
  • 3,440
  • 10
  • 44
  • 78
  • thanks for your reply. i am running this on an android tablet running android 3.01. is there any other possible problems that may occur? – simplified. Jun 14 '11 at 20:30
  • Change: Log.d(LOG_TAG, "Error: " + e.getMessage()); To: e.printStackTrace(); Run your code and post the output of the stack trace from the logcat window. This stack trace should explain what is causing the issue. – William Seemann Jun 14 '11 at 20:49
  • thanks again. but sorry to ask, i am new to this eclipse ide too, how do i get the output window to show? i can't really find it. – simplified. Jun 14 '11 at 20:56
  • Run adb logcat from a command line window in the following folder: /platform-tools/ – William Seemann Jun 14 '11 at 21:00
  • thanks. i have gotten this results. W/System.err(14647): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) W/System.err(14647): at dalvik.system.NativeStart.main(Native Method) – simplified. Jun 14 '11 at 21:06
  • @william seemann - that was the returns from the printstacktrace, but i'm not too sure what they are. – simplified. Jun 14 '11 at 21:16
  • Is there more info in the log? Stack traces usually point back to the line that caused the problem and the corresponding exception. – William Seemann Jun 14 '11 at 21:16
  • @william seeman - sorry about that, i think i missed out part of the log. W/System.err(14858): android.os.NetworkOnMainThreadException W/System.err(14858): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077) there are still chunks of it below. but are the first two. i can't paste everything here. – simplified. Jun 14 '11 at 21:24
  • I think I found the issue, is the code you posted above running in a thread? Do you see this in the log: "NetworkOnMainThreadException". If the code above isn't running in a thread Android 3.0 will throw an exception. – William Seemann Jun 14 '11 at 21:27
  • @simplified, the link you posted above is what I'm referring to. Can you mark this question as answered if this answer solved your issue, thanks. – William Seemann Jun 16 '11 at 00:15
0

a background process or use AsyncTask to perform your network transaction on a background thread will be required.

http://developer.android.com/resources/articles/painless-threading.html

simplified.
  • 1
  • 1
  • 1
  • @william seemann is that the above solution that i have posted the issue you are talking about? i wasn't able to post comments on the previous answer. – simplified. Jun 14 '11 at 21:42