Example usage for java.net URLConnection getInputStream

List of usage examples for java.net URLConnection getInputStream

Introduction

In this page you can find the example usage for java.net URLConnection getInputStream.

Prototype

public InputStream getInputStream() throws IOException 

Source Link

Document

Returns an input stream that reads from this open connection.

Usage

From source file:com.shahnami.fetch.Controller.JsonReader.java

public static JSONArray readJsonArrayFromUrl(String url) throws IOException, JSONException {
    URLConnection urlConnection = new URL(url).openConnection();
    urlConnection.addRequestProperty("User-Agent",
            "Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.7.2) Gecko/20040803");
    try (InputStream is = urlConnection.getInputStream()) {
        BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
        String jsonText = readAll(rd);
        JSONArray json = new JSONArray(jsonText);
        return json;
    }//from www  .  j  av  a 2  s . c o m
}

From source file:com.shahnami.fetch.Controller.JsonReader.java

public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
    URLConnection urlConnection = new URL(url).openConnection();
    urlConnection.addRequestProperty("User-Agent",
            "Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.7.2) Gecko/20040803");
    try (InputStream is = urlConnection.getInputStream()) {
        BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
        String jsonText = readAll(rd);
        JSONObject json = new JSONObject(jsonText);
        return json;
    }//from   w ww. j av a  2  s. c om
}

From source file:net.foxgenesis.helper.SiteReader.java

private static BufferedReader getStream(URL url) throws IOException {
    URLConnection connection = url.openConnection();
    connection.setRequestProperty("User-Agent", "X-ServerStats");
    connection.setReadTimeout(5000);//w w w  .j  a v a  2  s.  c  om
    return new BufferedReader(new InputStreamReader(connection.getInputStream()));
}

From source file:emily.util.YTUtil.java

/**
 * @param videocode youtubecode/* w  ww .java 2  s .c om*/
 * @return whats in the <title> tag on a youtube page
 */
public static String getTitleFromPage(String videocode) {
    String ret = "";
    try {
        URL loginurl = new URL("https://www.youtube.com/watch?v=" + videocode);
        URLConnection yc = loginurl.openConnection();
        yc.setConnectTimeout(10 * 1000);
        BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
        StringBuilder input = new StringBuilder();
        String inputLine;
        while ((inputLine = in.readLine()) != null)
            input.append(inputLine);
        in.close();
        int start = input.indexOf("<title>");
        int end = input.indexOf("</title>");
        ret = input.substring(start + 7, end - 10);
    } catch (Exception e) {
        System.out.println(e);
        e.printStackTrace();
    }
    return StringEscapeUtils.unescapeHtml4(ret);
}

From source file:jp.seikeidenron.androidtv.common.Utils.java

public static JSONObject parseUrl(String urlString) {
    Log.v(TAG, "Parse URL: " + urlString);
    BufferedReader reader = null;

    try {//from   w  w  w .j  av a2 s  .c o  m
        java.net.URL url = new java.net.URL(urlString);
        URLConnection urlConnection = url.openConnection();
        reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
        //urlConnection.getInputStream(), "iso-8859-1"));
        return convertBufferedReaderToJSONObject(reader);
    } catch (Exception e) {
        Log.w(TAG, "Failed to parse the json for media list", e);
        return null;
    } finally {
        if (null != reader) {
            try {
                reader.close();
            } catch (IOException e) {
                Log.w(TAG, "JSON feed closed", e);
            }
        }
    }
}

From source file:com.googlecode.gmaps4jsf.services.ReverseGeocoderServiceImpl.java

private static String readURL(URL url) throws Exception {
    URLConnection connection = url.openConnection();
    StringBuilder builder = new StringBuilder();
    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String line;//from  w ww. j  a  va  2s .c om

    while ((line = reader.readLine()) != null) {
        builder.append(line);
    }

    return builder.toString();
}

From source file:com.asual.lesscss.ResourceUtils.java

public static byte[] readBinaryUrl(URL source) throws IOException {
    byte[] result;
    try {//from   www.java  2 s  .c  o  m
        URLConnection urlc = source.openConnection();
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        InputStream input = urlc.getInputStream();
        try {
            byte[] buffer = new byte[1024];
            int bytesRead = -1;
            while ((bytesRead = input.read(buffer)) != -1) {
                byteStream.write(buffer, 0, bytesRead);
            }
            result = byteStream.toByteArray();
        } finally {
            byteStream.close();
            input.close();
        }
    } catch (IOException e) {
        logger.error("Can't read '" + source.getFile() + "'.");
        throw e;
    }
    return result;
}

From source file:Main.java

public static InputStream getInputStream(Context context, Uri uri) throws IOException {
    InputStream inputStream;// w w  w.jav a  2  s .  co m
    if (ContentResolver.SCHEME_ANDROID_RESOURCE.equals(uri.getScheme())
            || ContentResolver.SCHEME_ANDROID_RESOURCE.equals(uri.getScheme())
            || ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
        inputStream = context.getContentResolver().openInputStream(uri);
    } else {
        URLConnection urlConnection = new URL(uri.toString()).openConnection();
        urlConnection.setConnectTimeout(URLCONNECTION_CONNECTION_TIMEOUT_MS);
        urlConnection.setReadTimeout(URLCONNECTION_READ_TIMEOUT_MS);
        inputStream = urlConnection.getInputStream();
    }
    return new BufferedInputStream(inputStream);
}

From source file:Main.java

public static void DownloadFromUrl(String thisUrl, String path, String filename) {
    Log.d("DownloadFromUrl", "url: " + thisUrl);
    Log.d("DownloadFromUrl", "path: " + path);
    Log.d("DownloadFromUrl", "filename: " + filename);
    try {/* w w  w.  j ava2 s  .  com*/
        URL url = new URL(thisUrl);
        new File(path).mkdirs();
        File file = new File(path, filename);

        /* Open a connection to that URL. */
        URLConnection ucon = url.openConnection();

        Log.d("DownloadFromUrl", "about to write file");
        /*
         * Define InputStreams to read from the URLConnection.
         */
        InputStream is = ucon.getInputStream();
        try {
            OutputStream os = new FileOutputStream(file);
            try {
                byte[] buffer = new byte[4096];
                for (int n; (n = is.read(buffer)) != -1;)
                    os.write(buffer, 0, n);
            } finally {
                os.close();
            }
        } finally {
            is.close();
        }
        Log.d("DownloadFromUrl", "finished");

    } catch (IOException e) {
        Log.d("DownloadFromUrl", "Error: " + e);
    }
}

From source file:com.orthancserver.OrthancConnection.java

private static InputStream OpenUrl(String urlString, String authentication) throws IOException {
    try {//from w  w w.  j  a  v  a 2s . c o m
        URL url = new URL(urlString);
        // http://blogs.deepal.org/2008/01/sending-basic-authentication-using-url.html
        URLConnection uc = url.openConnection();
        uc.setRequestProperty("Authorization", "Basic " + authentication);
        return uc.getInputStream();
    } catch (ConnectException e) {
        throw new IOException();
    }
}