Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: Reading the HTML of a webpage into a String
    primarykey
    data
    text
    <p>I am new to Android Development and I am trying to read the HTML of a webpage and store it in a String ("myHTML") below, and display it on the app.</p> <p>However the application ends whenever it is run. I have been trawling the internet for the reason for this and have come across some articles saying that internet access cannot be done in the main UI thread of the app due to its "expensive" nature. Has anyone come across a similar problem before? I would be grateful for any further information on this problem... at a beginners level :)</p> <p>Here is the program:</p> <pre><code>import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.widget.TextView; import java.util.ArrayList; import java.util.regex.*; import java.net.*; import java.io.*; /* * Gets A webpage's HTML and saves to a string */ public String WebPageToHTML(String Webpage) throws IOException{ URL x = new URL(Webpage); BufferedReader in = new BufferedReader( new InputStreamReader( x.openStream())); String y = ""; String inputLine; while ((inputLine = in.readLine()) != null) y = y.concat(inputLine); in.close(); return y; } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView tv = new TextView(this); String FirstAddress = "http://www.google.com"; String myHTML = ""; try { myHTML = WebPageToHTML(FirstAddress); } catch (IOException e) { e.printStackTrace(); } tv.setText(myHTML); setContentView(tv); } </code></pre> <p>LOGCAT:</p> <pre><code>12-29 14:41:44.441: E/AndroidRuntime(540): java.lang.RuntimeException: Unable to start activity ComponentInfo{my.first.app/my.first.app.WhatHaveIMissedActivity}: android.os.NetworkOnMainThreadException 12-29 14:41:44.441: E/AndroidRuntime(540): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 12-29 14:41:44.441: E/AndroidRuntime(540): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 12-29 14:41:44.441: E/AndroidRuntime(540): at android.app.ActivityThread.access$600(ActivityThread.java:123) 12-29 14:41:44.441: E/AndroidRuntime(540): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 12-29 14:41:44.441: E/AndroidRuntime(540): at android.os.Handler.dispatchMessage(Handler.java:99) 12-29 14:41:44.441: E/AndroidRuntime(540): at android.os.Looper.loop(Looper.java:137) 12-29 14:41:44.441: E/AndroidRuntime(540): at android.app.ActivityThread.main(ActivityThread.java:4424) 12-29 14:41:44.441: E/AndroidRuntime(540): at java.lang.reflect.Method.invokeNative(Native Method) 12-29 14:41:44.441: E/AndroidRuntime(540): at java.lang.reflect.Method.invoke(Method.java:511) 12-29 14:41:44.441: E/AndroidRuntime(540): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 12-29 14:41:44.441: E/AndroidRuntime(540): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 12-29 14:41:44.441: E/AndroidRuntime(540): at dalvik.system.NativeStart.main(Native Method) 12-29 14:41:44.441: E/AndroidRuntime(540): Caused by: android.os.NetworkOnMainThreadException 12-29 14:41:44.441: E/AndroidRuntime(540): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099) 12-29 14:41:44.441: E/AndroidRuntime(540): at java.net.InetAddress.lookupHostByName(InetAddress.java:391) 12-29 14:41:44.441: E/AndroidRuntime(540): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242) 12-29 14:41:44.441: E/AndroidRuntime(540): at java.net.InetAddress.getAllByName(InetAddress.java:220) 12-29 14:41:44.441: E/AndroidRuntime(540): at libcore.net.http.HttpConnection.&lt;init&gt;(HttpConnection.java:71) 12-29 14:41:44.441: E/AndroidRuntime(540): at libcore.net.http.HttpConnection.&lt;init&gt;(HttpConnection.java:50) 12-29 14:41:44.441: E/AndroidRuntime(540): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351) 12-29 14:41:44.441: E/AndroidRuntime(540): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86) 12-29 14:41:44.441: E/AndroidRuntime(540): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 12-29 14:41:44.441: E/AndroidRuntime(540): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308) 12-29 14:41:44.441: E/AndroidRuntime(540): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303) 12-29 14:41:44.441: E/AndroidRuntime(540): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282) 12-29 14:41:44.441: E/AndroidRuntime(540): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232) 12-29 14:41:44.441: E/AndroidRuntime(540): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273) 12-29 14:41:44.441: E/AndroidRuntime(540): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168) 12-29 14:41:44.441: E/AndroidRuntime(540): at java.net.URL.openStream(URL.java:462) 12-29 14:41:44.441: E/AndroidRuntime(540): at my.first.app.WhatHaveIMissedActivity.WebPageToHTML(WhatHaveIMissedActivity.java:71) 12-29 14:41:44.441: E/AndroidRuntime(540): at my.first.app.WhatHaveIMissedActivity.onCreate(WhatHaveIMissedActivity.java:99) 12-29 14:41:44.441: E/AndroidRuntime(540): at android.app.Activity.performCreate(Activity.java:4465) 12-29 14:41:44.441: E/AndroidRuntime(540): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 12-29 14:41:44.441: E/AndroidRuntime(540): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 12-29 14:41:44.441: E/AndroidRuntime(540): ... 11 more </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload