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 Document openUrl(String location) throws Exception {
    URL url = new URL(location);
    //System.out.println(url);

    URLConnection con = url.openConnection();
    InputStream responseStream = con.getInputStream();

    Document result = loadFromStream(responseStream);
    responseStream.close();/*from w  ww.  j  av  a 2 s.  c o  m*/
    return result;
}

From source file:com.ln.methods.Getcalendar.java

public static void Main() {
    //TODO store file locally and check if file changed == redownload
    try {// w w w.j  a va2s .  co m

        //Get source
        // System.setProperty("http.agent", "lnrev2");
        URL calendar = new URL(Calendarurl);
        HttpURLConnection getsource = (HttpURLConnection) calendar.openConnection();
        InputStream in = new BufferedInputStream(getsource.getInputStream());
        //Write source
        Writer sw = new StringWriter();
        char[] b = new char[1024];
        Reader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
        int n;
        while ((n = reader.read(b)) != -1) {
            sw.write(b, 0, n);
        }
        //Source == String && Send source for parsing
        Parser.source = sw.toString();
        Betaparser.source = sw.toString();

        //String lol = sw.toString();
        //lol = StringUtils.substringBetween(lol, "<month year=\"2012\" num=\"2\">", "</month>");
        //Parser.parse();

        Betaparser.parse();

    } catch (MalformedURLException e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(null, e.getStackTrace(), "Error", JOptionPane.WARNING_MESSAGE);
    } catch (IOException e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(null, e.getStackTrace(), "Error", JOptionPane.WARNING_MESSAGE);
    }
}

From source file:Main.java

private static String getStringFromUrl(String url, int connectTimeout, int readTimeout)
        throws MalformedURLException, JSONException, IOException {
    URL urlObject = new URL(url);
    HttpURLConnection urlConn = (HttpURLConnection) urlObject.openConnection();
    String jsonString = "";

    if (connectTimeout != 0) {
        urlConn.setConnectTimeout(connectTimeout);
    }/*from  w  ww  .j  a va2s  . c  o  m*/
    if (readTimeout != 0) {
        urlConn.setReadTimeout(readTimeout);
    }

    try {
        jsonString = getStringFromInputStream(urlConn.getInputStream());
    } finally {
        urlConn.disconnect();
    }
    return jsonString;
}

From source file:Main.java

static public String download_json(String url_addr) {
    StringBuilder result = new StringBuilder();
    try {//  w w  w. j a  va  2 s .  c o  m
        URL url = new URL(url_addr);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        if (conn != null) {
            conn.setConnectTimeout(10000);
            conn.setUseCaches(false);
            if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                for (;;) {
                    String line = br.readLine();
                    if (line == null)
                        break;
                    result.append(line + '\n');
                }
                br.close();
            } else {
                conn.disconnect();
                return null;
            }
            conn.disconnect();
        }
    } catch (Exception e) {
        Log.e(TAG, e.toString());
        return null;
    }
    return result.toString();
}

From source file:Main.java

public static String getJsonCurrencyObject(String myurl) {
    StringBuilder strBuilder = new StringBuilder();

    try {//from  ww w  .  java  2 s. c o  m
        URL url = new URL(myurl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        InputStream inputStream = new BufferedInputStream(connection.getInputStream());

        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String line;
        while ((line = reader.readLine()) != null) {
            strBuilder.append(line);
        }
        inputStream.close();

    } catch (MalformedURLException ex) {
        Log.d("HttpURLConnection", "getJsonCurrencyObject: " + ex.getMessage());
    }

    catch (IOException e) {
        Log.d("readJSONFeed", e.getLocalizedMessage());
        e.printStackTrace();
    } catch (Exception ex) {
        Log.d("GeneralException", ex.getMessage());
    }

    return strBuilder.toString();
}

From source file:Main.java

public static boolean downloadUrlToStream(String urlString, OutputStream outputStream) {
    HttpURLConnection urlConnection = null;
    BufferedOutputStream out = null;
    BufferedInputStream in = null;
    try {/*from   ww  w  .  j  a v  a  2s  . c  o  m*/
        final URL url = new URL(urlString);
        urlConnection = (HttpURLConnection) url.openConnection();
        in = new BufferedInputStream(urlConnection.getInputStream(), 8 * 1024);
        out = new BufferedOutputStream(outputStream, 8 * 1024);
        int b;
        while ((b = in.read()) != -1) {
            out.write(b);
        }
        return true;
    } catch (final IOException e) {
        e.printStackTrace();
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
        try {
            if (out != null) {
                out.close();
            }
            if (in != null) {
                in.close();
            }
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }
    return false;
}

From source file:Main.java

/**
 * download and return bitmap for the given url
 * DO not call this method from main thread
 * @param src//  w  ww.j  a va  2s.  co  m
 * @return
 */
public static Bitmap getBitmapFromURL(String imageUrl) {
    try {
        URL url = new URL(imageUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        if (myBitmap != null) {
            myBitmap = getResizedBitmap(myBitmap, 200, 200);
        }
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

/**
 * Make a network request form ONLINE_LOCATION.
 *///from   w  w  w  .j a v a  2 s.  c  o  m
public static boolean makeNetworkCall() {
    try {
        URL url = new URL(ONLINE_LOCATION);
        HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
        httpsURLConnection.getInputStream();
        Log.d(TAG, "Network call completed.");
        return true;
    } catch (IOException e) {
        Log.e(TAG, "IOException " + e.getMessage());
        return false;
    }
}

From source file:Main.java

static String downloadUrl(String myurl) throws IOException {
    InputStream is = null;//from  w  w  w . ja  va  2  s  .c  om

    try {
        URL url = new URL(myurl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("Authorization", "Basic cm9vdDpvcmllbnRkYg==");
        conn.setReadTimeout(10000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        //start
        conn.connect();
        int response = conn.getResponseCode();
        Log.e("The response is: ", "" + response);
        is = conn.getInputStream();
        //converte inputStream in stringa
        String contentAsString = readIt(is);
        return contentAsString;
    } catch (IOException e) {
        Log.e("HTTP", e.getMessage());
        return null;
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

From source file:Main.java

public static String loadImageFromUrl(Context context, String imageURL, File file) {
    try {//from w  ww.  j  a  v a 2  s.com
        URL url = new URL(imageURL);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setConnectTimeout(5 * 1000);
        con.setDoInput(true);
        con.connect();
        if (con.getResponseCode() == 200) {
            InputStream inputStream = con.getInputStream();

            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024 * 20];
            int length = -1;
            while ((length = inputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, length);
            }
            byteArrayOutputStream.close();
            inputStream.close();

            FileOutputStream outputStream = new FileOutputStream(file);
            outputStream.write(byteArrayOutputStream.toByteArray());
            outputStream.close();
            return file.getPath();
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}