Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8119493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:49:01+00:00 2026-06-06T04:49:01+00:00

I was trying to read the HTML contents from a url. I tried with

  • 0

I was trying to read the HTML contents from a url. I tried with many samples and code examples from many sites, but it didn’t work for me. When I run the code it leaves the default text in textview. I even tried with edittext as well. I have even added permission in the manifest file.

One of the codes I used is here: I have added the complete code here. I tried with all the code below with no success.

 package com.adn;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;

public class REEActivity extends Activity {
    private static BufferedReader reader = null;

    /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    EditText ed = (EditText) findViewById(R.id.editText1);

    try {
        ed.append(getStringFromUrl("http://www.google.com"));
        //getInputStreamFromUrl("").close();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public static InputStream getInputStreamFromUrl(String url){
       InputStream contentStream = null;

       try{
         HttpClient httpclient = new DefaultHttpClient();
         HttpResponse response = httpclient.execute(new HttpGet(url));
         contentStream = response.getEntity().getContent();
       } catch(Exception e){
          e.printStackTrace();
       }
       System.out.println("Content stream is " + contentStream);
       return contentStream;
    }


public static String getStringFromUrl(String url) throws IOException{
    reader = new BufferedReader(new InputStreamReader(getInputStreamFromUrl(url)));

    StringBuilder sb = new StringBuilder();
    try{
    String line = null;
    while((line = reader.readLine()) != null)
    {
        sb.append(line);
    }    
    }catch (IOException e){
     e.printStackTrace();
    }
   getInputStreamFromUrl(url).close();
    return sb.toString();
}
}

Here is the logcat:

    06-20 21:25:18.022: E/AndroidRuntime(14095): FATAL EXCEPTION: main
06-20 21:25:18.022: E/AndroidRuntime(14095): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.adn/com.adn.REEActivity}: java.lang.NullPointerException
06-20 21:25:18.022: E/AndroidRuntime(14095):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at android.os.Looper.loop(Looper.java:137)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at android.app.ActivityThread.main(ActivityThread.java:4424)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at java.lang.reflect.Method.invokeNative(Native Method)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at java.lang.reflect.Method.invoke(Method.java:511)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at dalvik.system.NativeStart.main(Native Method)
06-20 21:25:18.022: E/AndroidRuntime(14095): Caused by: java.lang.NullPointerException
06-20 21:25:18.022: E/AndroidRuntime(14095):    at java.io.Reader.<init>(Reader.java:64)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at java.io.InputStreamReader.<init>(InputStreamReader.java:79)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at com.adn.REEActivity.getStringFromUrl(REEActivity.java:49)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at com.adn.REEActivity.onCreate(REEActivity.java:28)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at android.app.Activity.performCreate(Activity.java:4465)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
06-20 21:25:18.022: E/AndroidRuntime(14095):    ... 11 more

Here is the Manifest file

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.adn"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".REEActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Can someone please help me with this. I am new to Android as well.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T04:49:03+00:00Added an answer on June 6, 2026 at 4:49 am

    replace

    StringBuffer sb = new StringBuffer();
    

    with

    StringBuffer sb = new StringBuilder();
    

    and also close your InputStrem using close() method

    try to use this code in your second method, hope this will solve the problem

    public static String getStringFromUrl(String url) throws UnsupportedEncodingException{
        BufferedReader br = new BufferedReader(new InputStreamReader(getInputStreamFromUrl(url)));
    
        StringBuilder sb = new StringBuilder();
        try{
        String line = null;
        while((line = reader.readLine()) != null)
        {
            sb.append(line);
        }    
        }catch (IOException e){
         e.printStackTrace();
        }
        getInputStreamFromUrl(url).close();
        return sb.toString();
    }
    

    I tried your updated code and it works now, and this is what I get:

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to read the post request parameters from my HTML. I can
im trying to read the contents of myDiv which resides in a separate html
Using httplib2 and urllib2, I'm trying to fetch pages from this url, but all
I'm trying to read the html from this page to later parse it. The
I'm trying to parse HTML on an external page and read its contents (eg.
I'm trying to read some text from an html file, modify it in a
I am trying to read a html file which is a plain page with
I just read about HTML 5's WebSocket interface. How can I start trying this?
Trying to read an RSS and select information using Linq but can't seem to
Im trying to read a column of String values from my DB. Im trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.