Java – Android: Read the HTML of a web page into a string

Android: Read the HTML of a web page into a string… here is a solution to the problem.

Android: Read the HTML of a web page into a string

I’m new to Android development and I’m trying to read the HTML of a web page and store it in the string below (“myHTML”) and then display it on the app.

However, the application ends at runtime. For this reason, I’ve been searching the internet and have seen articles saying that internet access can’t be done in the main UI thread of an application due to its “expensive” nature. Has anyone ever encountered a similar issue? I would appreciate any further information on this issue… At the beginner level 🙂

The procedure is as follows:

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);
}

Lokat:

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 request this information using HttpClient. 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’ll also need to add the following to your app’s list file.

<uses-permission android:name="android.permission.INTERNET" />

A good thread on how to wrap it in AsyncTask is: Common class for AsyncTask in Android?

Related Problems and Solutions