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:com.wishlist.Utility.java

public static Bitmap getBitmap(String url) {
    Bitmap bm = null;//w  ww .  j  a  va  2  s . c om
    try {

        URL aURL = new URL(url);
        URLConnection conn = aURL.openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        bm = BitmapFactory.decodeStream(new FlushedInputStream(is));
        bis.close();
        is.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (httpclient != null) {
            httpclient.close();
        }
    }
    return bm;
}

From source file:Main.java

public static Bitmap getImageFromURL(URL url) {

    Bitmap bitmap = null;//from w ww .ja v  a 2s .c o m

    HttpURLConnection connection = null;

    try {

        connection = (HttpURLConnection) url.openConnection();

        if (connection.getResponseCode() != 200) {
            return null;
        }
        if (!CONTENT_TYPE_IMAGE.equalsIgnoreCase(connection.getContentType().substring(0, 5))) {
            return null;
        }

        bitmap = BitmapFactory.decodeStream(connection.getInputStream());

    } catch (IOException e) {

        e.printStackTrace();

    }

    if (connection != null) {

        connection.disconnect();

    }

    return bitmap;

}

From source file:eionet.cr.util.FileUtil.java

/**
 * Downloads the contents of the given URL into the given file and closes the file.
 *
 * @param urlString downloadable url/*  ww w .  java2s.c  om*/
 * @param toFile output file location
 * @throws IOException if input of output fails
 */
public static void downloadUrlToFile(final String urlString, final File toFile) throws IOException {

    URLConnection urlConnection = null;
    InputStream inputStream = null;
    try {
        URL url = new URL(urlString == null ? urlString : StringUtils.replace(urlString, " ", "%20"));
        urlConnection = url.openConnection();
        urlConnection.setRequestProperty("Connection", "close");
        inputStream = urlConnection.getInputStream();
        FileUtil.streamToFile(inputStream, toFile);
    } finally {
        IOUtils.closeQuietly(inputStream);
        URLUtil.disconnect(urlConnection);
    }
}

From source file:ilearnrw.utils.ServerHelperClass.java

public static String sendGet(String link) throws Exception {
    String url = baseUrl + link;//from   w  w w. ja  va 2 s .c  o  m
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    // optional default is GET
    con.setRequestMethod("GET");
    con.setRequestProperty("Authorization", userNamePasswordBase64("api", "api"));
    //con.setRequestProperty("User-Agent", USER_AGENT);

    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();
    return response.toString();
}

From source file:org.cytoscape.app.internal.net.server.CyHttpdImplTest.java

private static HttpURLConnection connectToURL(String urlString, String method) throws Exception {
    final URL url = new URL(urlString);
    final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod(method);
    connection.setDoOutput(true);/*  w  w w .jav a 2s  .  c o m*/
    connection.connect();
    return connection;
}

From source file:dataflow.feed.api.Weather.java

/**
 * Method which builds the string of the URL to call
 * @param myURL the URL which must become a callURL
 * @return //from   www.j a va  2 s.c o  m
 */
public static String callURL(String myURL) {
    //System.out.println("Requested URL:" + myURL);
    StringBuilder sb = new StringBuilder();
    URLConnection urlConn = null;
    InputStreamReader in = null;
    try {
        URL url = new URL(myURL);
        urlConn = url.openConnection();
        if (urlConn != null)
            urlConn.setReadTimeout(60 * 1000);
        if (urlConn != null && urlConn.getInputStream() != null) {
            in = new InputStreamReader(urlConn.getInputStream(), Charset.defaultCharset());
            BufferedReader bufferedReader = new BufferedReader(in);
            if (bufferedReader != null) {
                int cp;
                while ((cp = bufferedReader.read()) != -1) {
                    sb.append((char) cp);
                }
                bufferedReader.close();
            }
        }
        in.close();
    } catch (Exception e) {
        throw new RuntimeException("Exception while calling URL:" + myURL, e);
    }

    return sb.toString();
}

From source file:it.isti.cnr.hpc.europeana.hackthon.domain.FreebaseMapper.java

public static Freebase getInstanceFromQuery(String query)
        throws IOException, EntityException, NoResultException {
    KeyGenerator kg = new KeyGenerator();
    Integer key = kg.getKey(query);
    if (cache.containsKey(key))
        return cache.get(key);

    String uri = SuggestionProperties.getInstance().getProperty("freebase.query.api");
    uri += query;/*from   w  w w. j ava 2  s  . com*/

    try {
        uri = URIUtil.encodeQuery(uri);
    } catch (URIException e) {
        logger.error("Error producing the enconded query for {}", uri);
        return null;
    }
    try {
        URL u = new URL(uri);
        URLConnection uc = u.openConnection();

        InputStream is = uc.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        Freebase fb = parseFromJson(br);
        cache.put(key, fb);
        return fb;

    } catch (IOException e) {
        throw new IOException("error retrieving the query from freebase");

    }

}

From source file:gov.nist.healthcare.ttt.webapp.common.controller.GetCCDADocumentsController.java

public static JSONObject getHTML(String urlToRead) throws Exception {
    StringBuilder result = new StringBuilder();
    URL url = new URL(urlToRead);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;//from   ww  w. j ava 2 s  . c  o m
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }
    rd.close();
    return new JSONObject(result.toString());
}

From source file:com.melchor629.musicote.Utils.java

/**
 * HostTest//from  ww  w.  ja  v a2  s . c  om
 * Sirve para comprobar si est encendido el PC
 * Nothing to see here...
 *
 * @param host HOST IP
 * @return int response
 */
public static int HostTest(String host) {
    int response = 0;

    try {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        URL urlhttp = new URL("http://" + host);
        HttpURLConnection http = (HttpURLConnection) urlhttp.openConnection();
        http.setReadTimeout(1000);
        response = http.getResponseCode();
        http.disconnect();
    } catch (Exception e) {
        Log.e("Comprobando", "Excepcin HTTPURL: " + e.toString() + " " + host);
    }

    return response;
}

From source file:com.example.facebook_photo.Utility.java

public static Bitmap getBitmap(String url) {
    Bitmap bm = null;/*from w  w w  . j  a v a 2  s  . c  o m*/
    try {
        URL aURL = new URL(url);
        URLConnection conn = aURL.openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        bm = BitmapFactory.decodeStream(new FlushedInputStream(is));
        bis.close();
        is.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (httpclient != null) {
            httpclient.close();
        }
    }
    return bm;
}