Example usage for java.net URL openConnection

List of usage examples for java.net URL openConnection

Introduction

In this page you can find the example usage for java.net URL openConnection.

Prototype

public URLConnection openConnection() throws java.io.IOException 

Source Link

Document

Returns a java.net.URLConnection URLConnection instance that represents a connection to the remote object referred to by the URL .

Usage

From source file:Main.java

public static InputStream getInputStreamFromUrl(String urlStr) throws MalformedURLException, IOException {
    URL url = new URL(urlStr);
    HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
    InputStream inputStream = urlConn.getInputStream();
    return inputStream;
}

From source file:Main.java

public static boolean isExists(String urlString) {
    try {//  w  w  w  .  j  a  v  a  2s .c  om
        URL u = new URL(urlString);
        HttpURLConnection huc = (HttpURLConnection) u.openConnection();
        huc.setRequestMethod("GET");
        huc.connect();
        int rc = huc.getResponseCode();
        return (rc == HttpURLConnection.HTTP_OK);
        // Handle response code here...
    } catch (UnknownHostException uhe) {
        // Handle exceptions as necessary
        Log.w("EPub", uhe.getMessage());
    } catch (FileNotFoundException fnfe) {
        // Handle exceptions as necessary
        Log.w("EPub", fnfe.getMessage());
    } catch (Exception e) {
        // Handle exceptions as necessary
        Log.w("EPub", e.getMessage());
    }
    return false;
}

From source file:Main.java

public static int getRequest(String link) throws IOException {
    URL url = new URL(link);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    System.out.println(connection.getResponseCode());
    if (connection.getResponseCode() == HttpURLConnection.HTTP_OK || connection.getResponseCode() == 302) {
        InputStream is = connection.getInputStream();
        setParsedString(convertInputStreamToString(is));
    }/*from   ww  w  .ja  v a2 s  . co m*/
    return connection.getResponseCode();
}

From source file:Main.java

public static String download(String urlStr) {
    StringBuffer sb = new StringBuffer();
    String line = null;/*from   w w w.  j a  v a  2 s.com*/
    BufferedReader buffer = null;
    try {
        URL url = new URL(urlStr);
        HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
        buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
        while ((line = buffer.readLine()) != null) {
            sb.append(line);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            buffer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

From source file:Main.java

static private BitmapDrawable bitmapDrawableFromURL(Resources resources, String str_url, boolean scale, int w,
        int h) {//from w  w w.  jav  a  2  s .  c  o  m
    if (str_url == null || str_url.length() <= 0)
        return null;

    BitmapDrawable thumb = null;
    try {
        URL url = new URL(str_url);
        URLConnection connection = url.openConnection();
        connection.connect();

        InputStream stream = connection.getInputStream();
        BufferedInputStream data = new BufferedInputStream(stream);

        Bitmap bitmap = BitmapFactory.decodeStream(data);
        if (bitmap != null) {
            if (scale)
                thumb = new BitmapDrawable(resources, Bitmap.createScaledBitmap(bitmap, w, h, true));
            else
                thumb = new BitmapDrawable(resources, bitmap);
        }

    } catch (MalformedURLException e) {
        //e.printStackTrace();
    } catch (IOException e) {
        //e.printStackTrace();
    }

    return thumb;
}

From source file:Main.java

/**
 * Given a string url, connects and returns response code
 *
 * @param urlString       string to fetch
 * @param readTimeOutMs       read time out
 * @param connectionTimeOutMs       connection time out
 * @param urlRedirect       should use urlRedirect
 * @param useCaches       should use cache
 * @return httpResponseCode http response code
 * @throws IOException/*w  w w  . j av a  2s  . c o m*/
 */

public static int checkUrlWithOptions(String urlString, int readTimeOutMs, int connectionTimeOutMs,
        boolean urlRedirect, boolean useCaches) throws IOException {
    URL url = new URL(urlString);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setReadTimeout(readTimeOutMs /* milliseconds */);
    connection.setConnectTimeout(connectionTimeOutMs /* milliseconds */);
    connection.setRequestMethod("GET");
    connection.setInstanceFollowRedirects(urlRedirect);
    connection.setUseCaches(useCaches);
    // Starts the query
    connection.connect();
    int responseCode = connection.getResponseCode();
    connection.disconnect();
    return responseCode;
}

From source file:Main.java

public static byte[] getBytes(String url) {
    try {/* w ww .  j  a v a 2s . c o m*/
        URL u = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) u.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Charset", "UTF-8");
        InputStream is = connection.getInputStream();
        int contentLength = connection.getContentLength();
        byte[] data = new byte[contentLength];
        int alreadyRead = 0;
        int len = 0;
        while (alreadyRead < contentLength) {
            len = is.read(data, alreadyRead, contentLength - alreadyRead);
            alreadyRead += len;
        }
        is.close();
        return data;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

}

From source file:com.mycompany.test.Jaroop.java

/**
 * @throws MalformedURLException/*from w  w  w  .j  a  v  a2  s  . c  om*/
 * @throws ProtocolException
 * @throws IOException
 * This method obtains the HTTP connection required in order to make a GET request.
 */
private static HttpURLConnection getConnection(String url)
        throws MalformedURLException, ProtocolException, IOException {
    URL urlObject = new URL(url);
    HttpURLConnection connection = (HttpURLConnection) urlObject.openConnection();
    connection.setRequestMethod("GET");
    return connection;
}

From source file:Main.java

public static String requestData(String address) throws Exception {
    URL url = new URL(address);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.setConnectTimeout(5000);/*  w  w w  .j  a v a 2 s  .c om*/
    connection.setReadTimeout(5000);
    String data = null;
    InputStream is = null;
    if (connection.getResponseCode() == 200) {
        is = connection.getInputStream();
        data = readFromStream(is);
    }
    if (is != null) {
        is.close();
    }
    return data;
}

From source file:Main.java

public static Bitmap getBitmapAndScale(String url, int requiredSize) {
    Bitmap bm = null;//from w  ww . j a v  a2 s . c  o m
    try {
        URL aURL = new URL(url);
        URLConnection conn = aURL.openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();

        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(is, null, o);
        // The new size we want to scale to

        // Find the correct scale value. It should be the power of 2.
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < requiredSize || height_tmp / 2 < requiredSize)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }
        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        bm = BitmapFactory.decodeStream(is, null, o2);
        return bm;

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        return bm;

    }
}