Java – Android: read the HTML of the web page into a string
I'm new to Android development. I'm trying to read the HTML of the web page, store it in the following string ("myhtml") and display it on the application
However, as long as it runs, the application will end I have been searching the Internet for this reason and found some articles saying that due to its "expensive" nature, Internet access cannot be completed in the main UI thread of the application Has anyone ever had a similar problem? I would appreciate any further information on this issue... At the beginner level:)
This is the procedure:
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); }
Of logcat:
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.<init>(httpconnection.java:71) 12-29 14:41:44.441: E/AndroidRuntime(540): at libcore.net.http.httpconnection.<init>(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
Solution
You can use httpclient to request this information It will complete synchronously, but you can also make asynchronous requests
String myUri = "http://www.whatever.com"; HttpClient httpClient = new DefaultHttpClient(); HttpGet get = new HttpGet(myUri); HttpResponse response = httpClient.execute(get); // Build up result String bodyHtml = EntityUtils.toString(response.getEntity());
You also need to add the following to the manifest file of the application
<uses-permission android:name="android.permission.INTERNET" />
A good thread on how to use asynctask to wrap it is: common class for asynctask in Android?