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:Main.java

public static DataInputStream getStream(String filePath) throws Exception {
    URL url1 = new URL(filePath);
    HttpURLConnection conn = (HttpURLConnection) url1.openConnection();
    DataInputStream in = new DataInputStream(conn.getInputStream());
    return in;/*w ww.j  a v  a2 s  .  c  o m*/
}

From source file:Main.java

/**
 * //from   w  w w .  ja va2  s .c om
 * @param imgURL
 * @return
 */
public static BitmapDrawable downloadBitmapDrawable(String imgURL) {
    BitmapDrawable icon = null;
    try {
        URL url = new URL(imgURL);
        HttpURLConnection hc = (HttpURLConnection) url.openConnection();
        icon = new BitmapDrawable(hc.getInputStream());
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return icon;
}

From source file:Main.java

public static String getStringFromConnection(HttpURLConnection connection) throws IOException {
    InputStream inputStream = new BufferedInputStream(connection.getInputStream());
    String jsonString = convertStreamToString(inputStream);
    inputStream.close();//from   www .  jav  a 2s .co m

    return jsonString;
}

From source file:Main.java

public static InputStream getInputStream(HttpURLConnection connection) {
    if (connection != null) {
        try {//w  w w .  ja va 2 s . co  m
            InputStream inputStream = connection.getInputStream();
            return inputStream;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:Main.java

public static InputStream getInputStreamFromUrl(String urlStr) throws MalformedURLException, IOException {
    URL url = new URL(urlStr);
    HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
    InputStream inputStream = urlConn.getInputStream();
    return inputStream;
}

From source file:Main.java

/**
 * Read bytes from an HTTP connection.//from  w  w w  .j a  v a2s. com
 * @param connection the connection to read from.
 * @return an array of the bytes read.
 * @throws Exception if any error occurs.
 */
static byte[] readBytes(HttpURLConnection connection) throws Exception {
    return readBytes(new BufferedInputStream(connection.getInputStream()));
}

From source file:Main.java

/**
 * Extract the body of an HTTP response as a string.
 * @param connection the connection to read from.
 * @return the string extracted from the connection's input stream.
 * @throws Exception if any error occurs.
 *///from ww w .  j  a  v  a 2s . c  o  m
static String readResponseBody(HttpURLConnection connection) throws Exception {
    return readString(new BufferedInputStream(connection.getInputStream()));
}

From source file:Main.java

public static InputStream getIS(String address) {
    InputStream is = null;//from   ww w  . jav  a2 s. c  o m
    try {
        URL url = new URL(address);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true);
        conn.connect();
        is = conn.getInputStream();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return is;
}

From source file:com.prodyna.liferay.devcon.hystrix.demo.service.ChuckNorrisFactsServiceClient.java

/**
 * Call Chuck Norris facts service using simple {@link HttpURLConnection}.
 * /* w w  w.j a va 2 s.  co  m*/
 * @return Fact about Chuck Norris.
 * @throws IOException
 *             is thrown if the service could not be called for some reason.
 */
public static String callChuckNorrisFactsService() throws IOException {
    URL url = new URL(CHUCK_NORRIS_SERVICE_ENDPOINT);

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    InputStream inputStream = connection.getInputStream();

    String response = IOUtils.toString(inputStream);

    JSONObject jsonResponse = new JSONObject(response);
    String chuckNorrisFact = jsonResponse.getString("fact");

    return chuckNorrisFact;
}

From source file:Main.java

public static String download(String urlStr) {
    StringBuffer sb = new StringBuffer();
    String line = null;//from  w  ww .j  av  a  2s . com
    BufferedReader buffer = null;
    try {
        URL url = new URL(urlStr);
        HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
        buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
        while ((line = buffer.readLine()) != null) {
            sb.append(line);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            buffer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}