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:hackathon.openrice.CardsActivity.java

public static Bitmap getBitmapFromURL(String src) {
    try {//from  w ww.  j  a v a 2  s.  c om
        URL url = new URL(src);
        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:com.samsung.msf.youtubeplayer.client.util.FeedParser.java

/**
 * Given a string representation of a URL, sets up a connection and gets an input stream.
 *//*from  www.java 2 s . c  o m*/
public static InputStream downloadUrl(final URL url) throws IOException {
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setReadTimeout(NET_READ_TIMEOUT_MILLIS /* milliseconds */);
    conn.setConnectTimeout(NET_CONNECT_TIMEOUT_MILLIS /* milliseconds */);
    conn.setRequestMethod("GET");
    conn.setDoInput(true);
    // Starts the query
    conn.connect();
    return conn.getInputStream();
}

From source file:Main.java

public static void downloadImage(String imageUrl) {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        Log.d("TAG", "monted sdcard");
    } else {//from ww  w .j a va2  s . co m
        Log.d("TAG", "has no sdcard");
    }
    HttpURLConnection con = null;
    FileOutputStream fos = null;
    BufferedOutputStream bos = null;
    BufferedInputStream bis = null;
    File imageFile = null;
    try {
        URL url = new URL(imageUrl);
        con = (HttpURLConnection) url.openConnection();
        con.setConnectTimeout(5 * 1000);
        con.setReadTimeout(15 * 1000);
        con.setDoInput(true);
        con.setDoOutput(true);
        bis = new BufferedInputStream(con.getInputStream());
        imageFile = new File(getImagePath(imageUrl));
        fos = new FileOutputStream(imageFile);
        bos = new BufferedOutputStream(fos);
        byte[] b = new byte[1024];
        int length;
        while ((length = bis.read(b)) != -1) {
            bos.write(b, 0, length);
            bos.flush();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (bis != null) {
                bis.close();
            }
            if (bos != null) {
                bos.close();
            }
            if (con != null) {
                con.disconnect();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    if (imageFile != null) {
    }
}

From source file:com.brobwind.brodm.NetworkUtils.java

public static void deviceInfo(String path, OnMessage callback) {
    try {//  ww  w . ja v a2s  . c om
        URL url = new URL(path + "/privet/info");
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestProperty("Authorization", "Basic anonymous");
        urlConnection.setRequestProperty("Content-Type", "application/json");
        urlConnection.connect();
        try {
            InputStream in = new BufferedInputStream(urlConnection.getInputStream());
            BufferedReader bufReader = new BufferedReader(new InputStreamReader(in));
            StringBuilder sb = new StringBuilder();
            for (String line = bufReader.readLine(); line != null;) {
                sb.append(line).append("\n");
                line = bufReader.readLine();
            }

            callback.onMessage(sb.toString(), null);
            return;
        } finally {
            urlConnection.disconnect();
        }
    } catch (MalformedURLException muex) {
        Log.e(TAG, "Malformed url: " + path);
        callback.onMessage(null, muex.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
        callback.onMessage(null, e.getMessage());
    }
}

From source file:fr.zcraft.zlib.tools.mojang.UUIDFetcher.java

/**
 * Reads the response as a JSON object.//ww  w  . j  a  v a 2s.c  o m
 *
 * @param connection The connection.
 *
 * @return An object returned by the GSON's {@link JSONParser#parse(Reader)}.
 * @throws IOException    If an exception occurred while contacting the server.
 * @throws ParseException If the response cannot be parsed as a JSON object.
 */
private static Object readResponse(HttpURLConnection connection) throws IOException, ParseException {
    if (connection.getResponseCode() == 204)
        return null;

    return new JSONParser().parse(new InputStreamReader(connection.getInputStream()));
}

From source file:Main.java

private static InputStream getInputStreamFromUrl_V9(Context paramContext, String paramString) {
    if (confirmDownload(paramContext, paramString)) {
        try {// w w  w  .j  a  va 2 s . c om

            URL localURL = new URL(paramString);
            HttpURLConnection localHttpURLConnection2 = (HttpURLConnection) localURL.openConnection();
            localHttpURLConnection2.setRequestProperty("Accept-Charset", "UTF-8");
            localHttpURLConnection2.setReadTimeout(30000);
            localHttpURLConnection2.setConnectTimeout(30000);
            localHttpURLConnection2.setRequestMethod("GET");
            localHttpURLConnection2.setDoInput(true);
            localHttpURLConnection2.connect();
            return localHttpURLConnection2.getInputStream();

        } catch (Throwable localThrowable) {

            localThrowable.printStackTrace();
            return null;

        }
    } else {
        return null;
    }
}

From source file:com.gson.util.HttpKit.java

/**
 * ?/* www .  j a  va  2s  . c o  m*/
 * @param url
 * @return
 * @throws IOException
 */
public static Attachment download(String url) throws IOException {
    Attachment att = new Attachment();
    HttpURLConnection conn = initHttp(url, _GET, null);
    if (conn.getContentType().equalsIgnoreCase("text/plain")) {
        // BufferedReader???URL?  
        InputStream in = conn.getInputStream();
        BufferedReader read = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET));
        String valueString = null;
        StringBuffer bufferRes = new StringBuffer();
        while ((valueString = read.readLine()) != null) {
            bufferRes.append(valueString);
        }
        in.close();
        att.setError(bufferRes.toString());
    } else {
        BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
        String ds = conn.getHeaderField("Content-disposition");
        String fullName = ds.substring(ds.indexOf("filename=\"") + 10, ds.length() - 1);
        String relName = fullName.substring(0, fullName.lastIndexOf("."));
        String suffix = fullName.substring(relName.length() + 1);

        att.setFullName(fullName);
        att.setFileName(relName);
        att.setSuffix(suffix);
        att.setContentLength(conn.getHeaderField("Content-Length"));
        att.setContentType(conn.getHeaderField("Content-Type"));

        att.setFileStream(bis);
    }
    return att;
}

From source file:com.tohours.imo.util.TohoursUtils.java

/**
 * http/*  ww w  .j ava 2 s  .co  m*/
 * @param path
 * @return
 * @throws IOException
 */
public static String httpGet(String path, String charsetName) throws IOException {
    String rv = null;
    URL url = null;
    HttpURLConnection httpConnection = null;
    InputStream input = null;
    try {
        url = new URL(path);
        httpConnection = (HttpURLConnection) url.openConnection();
        input = httpConnection.getInputStream();
        rv = TohoursUtils.inputStream2String(input, charsetName);
    } finally {
        if (input != null) {
            input.close();
        }
    }
    return rv;
}

From source file:Main.java

private static String httpPost(String address, String params) {
    InputStream inputStream = null;
    HttpURLConnection urlConnection = null;
    try {/*  w  w w . j  a v  a  2 s .  c  om*/
        URL url = new URL(address);
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setDoOutput(true);
        urlConnection.setRequestMethod("POST");
        urlConnection.getOutputStream().write(params.getBytes());
        urlConnection.getOutputStream().flush();

        urlConnection.connect();
        if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            inputStream = urlConnection.getInputStream();
            int len = 0;
            byte[] buffer = new byte[1024];
            StringBuffer stringBuffer = new StringBuffer();
            while ((len = inputStream.read(buffer)) != -1) {
                stringBuffer.append(new String(buffer, 0, len));
            }
            return stringBuffer.toString();
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        close(inputStream);
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
    return null;

}

From source file:info.dc585.hpt.NetworkUtilities.java

/**
 * Download the avatar image from the server.
 *
 * @param avatarUrl the URL pointing to the avatar image
 * @return a byte array with the raw JPEG avatar image
 *//*from   w w  w.j  a va 2  s .c om*/
public static Bitmap downloadBitmap(final String avatarUrl) {

    // If there is no avatar, we're done
    if (TextUtils.isEmpty(avatarUrl)) {
        return null;
    }

    try {
        //Log.v(TAG, "Downloading avatar: " + avatarUrl);
        // Request the avatar image from the server, and create a bitmap
        // object from the stream we get back.
        URL url = new URL(avatarUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.connect();
        try {
            final BitmapFactory.Options options = new BitmapFactory.Options();
            final Bitmap avatar = BitmapFactory.decodeStream(connection.getInputStream(), null, options);

            return avatar;

        } finally {
            connection.disconnect();
        }
    } catch (MalformedURLException muex) {
        // A bad URL - nothing we can really do about it here...
        Log.e(TAG, "Malformed avatar URL: " + avatarUrl, muex);
    } catch (IOException ioex) {
        // If we're unable to download the avatar, it's a bummer but not the
        // end of the world. We'll try to get it next time we sync.
        Log.e(TAG, "Failed to download user avatar: " + avatarUrl, ioex);
    }
    return null;
}