Example usage for java.net HttpURLConnection getInputStream

List of usage examples for java.net HttpURLConnection getInputStream

Introduction

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

Prototype

public InputStream getInputStream() throws IOException 

Source Link

Document

Returns an input stream that reads from this open connection.

Usage

From source file:editor.util.URLUtil.java

public static String readText(String uri) {
    String res = "";
    try {/*from w  w  w . ja  v a2  s .co m*/
        java.net.URL url = new java.net.URL(uri);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        InputStream in = connection.getInputStream();
        byte buff[] = new byte[200];
        int ch;
        while ((ch = in.read(buff)) != -1)
            res += new String(buff, 0, ch);
        in.close();

        return res;
    } catch (Exception e) {
        return e.getLocalizedMessage();
    }
}

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  v  a2s . 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

/**
 * Makes a GET call to the server./* w w w. j  a  v a2 s  .com*/
 * @return The serve response (expected is JSON)
 */
public static String httpGet(String urlStr) {
    try {
        URL url = new URL(urlStr);

        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        String r = readStream(in);
        urlConnection.disconnect();
        return r;
    } catch (MalformedURLException e) {
        Log.v("MyLog", "GET: Bad URL");
        e.printStackTrace();
        return "GET: Bad URL";
    } catch (IOException e) {
        Log.v("MyLog", "GET: Bad Con");
        e.printStackTrace();
        return "GET: Bad Con [" + urlStr + "]";
    }
}

From source file:Main.java

public static Bitmap getBitmapFromRemote(String address) {
    Bitmap bitmap = null;//w  ww.  j  av  a2s  . c o  m
    try {
        URL url = new URL(address);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
        bitmap = BitmapFactory.decodeStream(is);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bitmap;
}

From source file:Main.java

public static void downloadResource(final String fileUrl) {
    new Thread(new Runnable() {

        @Override//from w w w.  ja v a2 s .c  o m
        public void run() {
            inStream = null;
            isOver = false;
            try {
                URL url = new URL(fileUrl);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setConnectTimeout(5000);
                inStream = conn.getInputStream();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            isOver = true;
        }
    }).start();
}

From source file:Main.java

public static Bitmap fetchImage(String fileUrl) {
    try {//  w ww . j  a va  2s.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 boolean download(String uri, String filePath) {
    try {/*from  w  ww  .ja  v  a 2  s.  com*/
        URL url = new URL(uri);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("User-Agent", AGENT);
        readStreamToFile(conn.getInputStream(), filePath);
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:Main.java

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

    try {//from w  w w. j a  va  2  s  . co 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 String execUrl(String uri) {
    String res = "";
    try {/*from   w w  w . j  a  v a 2s .  co m*/
        URL url = new URL(uri);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("User-Agent", AGENT);

        res = readStream(conn.getInputStream());
    } catch (Exception e) {
        e.printStackTrace();
        res = "";
    }
    return res;
}

From source file:Main.java

@SuppressWarnings("SameParameterValue")
public static Bitmap fetchAndRescaleBitmap(String uri, int width, int height) throws IOException {
    URL url = new URL(uri);
    BufferedInputStream is = null;
    try {/*from ww w.  j a va 2 s . c o  m*/
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        is = new BufferedInputStream(urlConnection.getInputStream());
        is.mark(MAX_READ_LIMIT_PER_IMG);
        int scaleFactor = findScaleFactor(width, height, is);
        is.reset();
        return scaleBitmap(scaleFactor, is);
    } finally {
        if (is != null) {
            is.close();
        }
    }
}