Example usage for java.net HttpURLConnection connect

List of usage examples for java.net HttpURLConnection connect

Introduction

In this page you can find the example usage for java.net HttpURLConnection connect.

Prototype

public abstract void connect() throws IOException;

Source Link

Document

Opens a communications link to the resource referenced by this URL, if such a connection has not already been established.

Usage

From source file:Main.java

public static Bitmap fetchImage(String fileUrl) {
    try {//from w  w  w  .  j  ava2 s .c o m
        URL myFileUrl = new URL(fileUrl);
        HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
        Bitmap image = BitmapFactory.decodeStream(is);
        is.close();
        return image;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Bitmap getBitmapFromURL(String strURL) {
    try {//from  w  w  w . j a va 2s.c o  m
        URL url = new URL(strURL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        return BitmapFactory.decodeStream(input);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Bitmap getBitmapFromUrl(String urlSource) {
    try {// w w w .j  av a 2s  . co  m
        URL url = new URL(urlSource);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static boolean isInternetWorking() {
    boolean success = false;
    try {/*from www  .ja  v  a 2 s  .  co  m*/
        URL url = new URL("https://google.com");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setConnectTimeout(8000);
        connection.connect();
        success = connection.getResponseCode() == 200;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return success;
}

From source file:Main.java

public static boolean isInternetWorking() {
    boolean success = false;
    try {//  w w w. j  a  va 2  s  .  co  m
        URL url = new URL("https://google.com");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setConnectTimeout(10000);
        connection.connect();
        success = connection.getResponseCode() == 200;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return success;
}

From source file:Main.java

public static Bitmap getBitMap(String url) {
    URL myFileUrl = null;//from w  w  w . j  a va2 s.  c o  m
    Bitmap bitmap = null;
    try {
        myFileUrl = new URL(url);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    try {
        HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
        bitmap = BitmapFactory.decodeStream(is);
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bitmap;
}

From source file:Main.java

public static boolean isExists(String urlString) {
    try {/*from w  ww.j a  va2s  .co m*/
        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 Bitmap returnBitMap(String url) {
    URL myFileUrl = null;//from ww w . j a  v  a 2s .  co m
    Bitmap bitmap = null;
    try {
        myFileUrl = new URL(url);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    try {
        HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
        bitmap = BitmapFactory.decodeStream(is);
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // Log.v(tag, bitmap.toString());

    return bitmap;
}

From source file:Main.java

public static Bitmap returnBitmap(String url) {
    URL fileUrl = null;//from  w  w w .  jav  a 2  s . c  o m
    Bitmap bitmap = null;

    try {
        fileUrl = new URL(url);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    try {
        HttpURLConnection conn = (HttpURLConnection) fileUrl.openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
        bitmap = BitmapFactory.decodeStream(is);
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bitmap;

}

From source file:Main.java

private static InputStream getHttpConnection(String urlString) throws IOException {
    InputStream stream = null;/*from  ww w.ja v a2 s . co  m*/
    URL url = new URL(urlString);
    URLConnection connection = url.openConnection();

    try {
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
        httpConnection.setRequestMethod("GET");
        httpConnection.connect();

        if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            stream = httpConnection.getInputStream();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return stream;
}