Example usage for java.net HttpURLConnection setDoInput

List of usage examples for java.net HttpURLConnection setDoInput

Introduction

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

Prototype

public void setDoInput(boolean doinput) 

Source Link

Document

Sets the value of the doInput field for this URLConnection to the specified value.

Usage

From source file:Main.java

public static Bitmap returnBitMap(String url) {
    URL myFileUrl = null;/*from   ww w  .  ja  va2s .  c  o  m*/
    Bitmap bitmap = null;
    try {
        myFileUrl = new URL(url);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    try {
        HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
        bitmap = BitmapFactory.decodeStream(is);
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // Log.v(tag, bitmap.toString());

    return bitmap;
}

From source file:Main.java

public static Bitmap returnBitmap(String url) {
    URL fileUrl = null;/*w  w w . java2s  .c o  m*/
    Bitmap bitmap = null;

    try {
        fileUrl = new URL(url);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    try {
        HttpURLConnection conn = (HttpURLConnection) fileUrl.openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
        bitmap = BitmapFactory.decodeStream(is);
        is.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bitmap;

}

From source file:Main.java

public static Bitmap getBitmapFromURL(String urlStr) {
    Bitmap bitmap = null;/*from w  w  w .ja  va2s.  c o m*/
    try {
        URL url = new URL(urlStr);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();

        InputStream input = connection.getInputStream();
        bitmap = BitmapFactory.decodeStream(input);
    } catch (IOException e) {
        Log.e(LOG_TAG, "cannot get the image from link :" + urlStr);
    }
    return bitmap;
}

From source file:Main.java

/**
 * @param url//from  w  ww.  j a va 2 s. c  o m
 * @return
 */
public static Bitmap getBitMapFromURL(URL url) {
    Bitmap mBitmap = null;

    try {
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true);
        conn.connect();
        conn.getContentLength();
        InputStream is = conn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        mBitmap = BitmapFactory.decodeStream(bis);
        bis.close();
        is.close();
    } catch (Exception e) {
        Log.e("Exception in MapScrollActivity.getBitmapFromURL", "" + e.getMessage());
        e.printStackTrace();
    }
    return mBitmap;
}

From source file:Main.java

public static byte[] downloadImage(final URL url) {
    InputStream in = null;//from  www .  j  a  va 2 s  . c o m
    int responseCode = -1;

    try {
        final HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setDoInput(true);
        con.connect();
        responseCode = con.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            in = con.getInputStream();
            return getByteFromInputStream(in);
        }
    } catch (final IOException e) {
        Log.e(LOG_TAG, "Failed to download image from: " + url.toString(), e);
    }
    return null;
}

From source file:Main.java

public static void setUpConnection(HttpURLConnection connection, String method) {
    try {//from   ww  w  . j  a  va  2  s  .c om
        connection.setRequestMethod(method);
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/json");
    } catch (ProtocolException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

/**
 * download and return bitmap for the given url
 * DO not call this method from main thread
 * @param src//from w w  w .j  av a 2  s . c o  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

@Nullable
private static InputStream getStreamFromUri(Uri selectedImageURI, Context theContext) throws IOException {
    switch (selectedImageURI.getScheme()) {
    case "content":
        return theContext.getContentResolver().openInputStream(selectedImageURI);

    case "file":
        return new FileInputStream(new File(URI.create(selectedImageURI.toString())));

    case "http":
        // Fall through
    case "https":
        final URL url = new URL(selectedImageURI.toString());
        final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();/* w w  w.j a v  a2s . com*/
        return connection.getInputStream();

    default:
        Log.w(TAG, "getStreamFromUri(): unsupported Uri scheme: " + selectedImageURI.getScheme());
        return null;
    }
}

From source file:Main.java

public static final Bitmap fetchAndRescaleBitmap(String uri, int width, int height) throws IOException {
    URL url = new URL(uri);
    HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
    httpConnection.setDoInput(true);
    httpConnection.connect();//ww  w  . j  av a2s. c  om
    InputStream inputStream = httpConnection.getInputStream();
    int scaleFactor = findScaleFactor(width, height, inputStream);

    httpConnection = (HttpURLConnection) url.openConnection();
    httpConnection.setDoInput(true);
    httpConnection.connect();
    inputStream = httpConnection.getInputStream();
    Bitmap bitmap = scaleBitmap(scaleFactor, inputStream);
    return bitmap;
}

From source file:Main.java

public static void post(String actionUrl, String file) {
    try {/*  w w w. jav a2 s . co m*/
        URL url = new URL(actionUrl);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setDoInput(true);
        con.setDoOutput(true);
        con.setUseCaches(false);
        con.setRequestMethod("POST");
        con.setRequestProperty("Charset", "UTF-8");
        con.setRequestProperty("Content-Type", "multipart/form-data;boundary=*****");
        DataOutputStream ds = new DataOutputStream(con.getOutputStream());
        FileInputStream fStream = new FileInputStream(file);
        int bufferSize = 1024; // 1MB
        byte[] buffer = new byte[bufferSize];
        int bufferLength = 0;
        int length;
        while ((length = fStream.read(buffer)) != -1) {
            bufferLength = bufferLength + 1;
            ds.write(buffer, 0, length);
        }
        fStream.close();
        ds.flush();
        InputStream is = con.getInputStream();
        int ch;
        StringBuilder b = new StringBuilder();
        while ((ch = is.read()) != -1) {
            b.append((char) ch);
        }
        new String(b.toString().getBytes("ISO-8859-1"), "utf-8");
        ds.close();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}