Java URL Read getText(String url)

Here you can find the source of getText(String url)

Description

Returns the response from the given URL as an array of lines.

License

Open Source License

Parameter

Parameter Description
url The url to connect to.

Exception

Parameter Description
Exception This exception is thrown when there was a problem connecting to the URL

Return

A response string from the server.

Declaration

public static String[] getText(String url) throws Exception 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

public class Main {
    /**//from www. j  av a2 s  .  c o m
     * Returns the response from the given URL as an array of lines.
     * @param url
     *           The url to connect to.
     * @return
     *        A response string from the server.
     * @throws Exception
     *                  This exception is thrown when there was a problem connecting to the URL
     */
    public static String[] getText(String url) throws Exception {
        URL website = new URL(url);
        URLConnection connection = website.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

        ArrayList<String> lines = new ArrayList<String>();
        String inputLine;

        while ((inputLine = in.readLine()) != null)
            lines.add(inputLine);

        in.close();

        return lines.toArray(new String[lines.size()]);
    }
}

Related

  1. getStreamByConnection(final HttpURLConnection con)
  2. getStringFromConnection(HttpURLConnection connection)
  3. getStringFromURL(String URLString)
  4. getText(HttpURLConnection conn)
  5. getText(String url)
  6. getText(URL url)
  7. getTextFromURL(final String url)
  8. getTextFromURL(URL url)
  9. getTextFromURL(URL url)