Read contents of a URL in Android

I'm new to android and I'm trying to figure out how to get the contents of a URL as a String. For example if my URL is http://www.google.com/ I want to get the HTML for the page as a String. Could anyone help me with this?


Asked by: Patrick135 | Posted: 20-01-2022






Answer 1

From the Java Docs : readingURL

URL yahoo = new URL("http://www.yahoo.com/");
BufferedReader in = new BufferedReader(
            new InputStreamReader(
            yahoo.openStream()));

String inputLine;

while ((inputLine = in.readLine()) != null)
    System.out.println(inputLine);

in.close();

Instead of writing each line to System.out just append it to a string.

Answered by: Aida558 | Posted: 21-02-2022



Answer 2

You can open a stream and read and append each line to a string - remember to wrap everything with a try-catch block - hope it helps!

String fullString = "";
URL url = new URL("http://example.com");
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = reader.readLine()) != null) {
    fullString += line;
}
reader.close();

Answered by: William337 | Posted: 21-02-2022



Answer 3

You can put it in an AsyncTask like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    try {
        new Main2Activity.MyTask().execute(this);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

private static class MyTask extends AsyncTask<Object, Void, String> {

    Main2Activity activity;

    @Override
    protected String doInBackground(Object... params) {
        activity = (Main2Activity)params[0];
        try {
            StringBuilder sb = new StringBuilder();
            URL url = new URL("http://www.google.com/");

            BufferedReader in;
            in = new BufferedReader(
                    new InputStreamReader(
                            url.openStream()));

            String inputLine;
            while ((inputLine = in.readLine()) != null)
                sb.append(inputLine);

            in.close();

            return sb.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(String str) {
        //Do something with result string
        WebView webView = activity.findViewById(R.id.web_view);
        webView.loadData(str, "text/html; charset=UTF-8", null);
    }

}

Answered by: Sophia472 | Posted: 21-02-2022



Similar questions

android - Write contents of custom View to large Image file on SD card

I have a class that extends View. I override the onDraw method and allow the user to draw on the screen. I am at the point where I want to save this view as an image. I Can use buildDrawingCache and getDrawingCache to create a bitmap that I can write to the SD card. However, the image is not good quality at a large size, it has jagged edges. Since I have a View and I use Paths I can transform all by drawing to a bigge...


Change the contents of an Android dialog box after creation

Is there a simple way to change the contents of a dialog box in Android without having to re-create the dialog box? I know that Activity.onCreateDialog() is only called once when the dialog first needs to be created, and this is where you initially set the dialog's contents. I need to change the dialog's contents later, so I'm wondering what is the proper way to do this.


android - Write contents of custom View to large Image file on SD card

I have a class that extends View. I override the onDraw method and allow the user to draw on the screen. I am at the point where I want to save this view as an image. I Can use buildDrawingCache and getDrawingCache to create a bitmap that I can write to the SD card. However, the image is not good quality at a large size, it has jagged edges. Since I have a View and I use Paths I can transform all by drawing to a bigge...


is there a default way to make the first letter of the contents appear (eg as a hover) android

I have a listview in alphabetic order and as the user scrolls i want a way to see the first letter.Like a phone catalog.


How to view the contents of an Android APK file?

Is there a way to extract and view the content of an .apk file?


how to show the html contents to the webview using android

Following is my html content which i want to show in the webview using android sdk. It will displays only //Please But when I put this HTML content into the browser then it shows differently. &lt;br /&gt;&lt;br /&gt;Read the handouts please for tomorrow.&lt;br /&gt;&lt;br /&gt;&lt;!--homework help homework help help with homework homework assignments elementary...


android - Add the contents of one Cursor to another Cursor

I want to join two cursors so that the contents of the second Cursor shall also appear in first Cursor after joining. Precisely here is my code, public final Uri AllImage_URI_Int = MediaStore.Images.Media.INTERNAL_CONTENT_URI; public final Uri AllAudio_URI = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; cContentList = managedQuery(AllImage_URI_Int, null, null, null, MediaStore.Images.ImageColumns.TITLE...


java - How to parse a url and convert the url contents into string for Android?

How to parse and convert the contents of url into string? whether this can be done for Android programming?


android array contents twice the size when written to a file?

i've an app that records audio samples. once a recording has been made it is stored on sdcard under reversme.pcm. i then can enter a filename and the app creates a file under that name and copies the contents of reveseme.pcm to the new file under the new filename. the problem i'm having is the new file is twice the size of the original, and when i try to play it there is no sound. i've run it through audacity and there is ...


android - Row color based on contents in the ListView of my RSS reader

I am very much an Android newbie and I have built a simple RSS reader application based around the free IBM android RSS tutorial. I would like to change the background color of each row if the category of that row is equal to a particular String. I wrote the following "for loop" which discovers the category of each item and runs an if statement should that category be equal to "News". At the moment the background c...


android - Cursory requery doesn't refresh contents

I have implemented a ContentProvider for my app. I call startManagingCursor for my Activity. The View doesn't update. So I go into debug mode and call cursor.requery manually only to see that the contents of the cursor doesn't refresh, and the original values are still there. From the ContentProvider I call notifyChange(contentUri). the contentUri is the same as was used in cursor.query originally. Am I doi...






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top