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

/**
 * Makes a GET call to the server./*from w  w w . ja va2s  .c  o m*/
 * @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:com.uksf.mf.core.utility.ClassNames.java

/**
 * Checks if connection to URL can be established
 * @param url url to check//from  www .j a v  a2 s.  com
 * @return connection state
 * @throws IOException error
 */
private static boolean checkConnection(URL url) throws IOException {
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.setConnectTimeout(1500);
    connection.setReadTimeout(1500);
    connection.setRequestProperty("User-Agent",
            "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)");
    connection.connect();
    return connection.getResponseCode() == 404;
}

From source file:Main.java

private static String httpGet(String address) {
    InputStream inputStream = null;
    HttpURLConnection httpURLConnection = null;
    try {/*from www. ja  v  a2  s . com*/
        URL url = new URL(address);
        httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.connect();
        if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            inputStream = httpURLConnection.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 (httpURLConnection != null) {
            httpURLConnection.disconnect();
        }

    }

    return null;

}

From source file:Main.java

public static byte[] retrieveImageData_fromUrl(String imageUrl) throws IOException {
    URL url = new URL(imageUrl);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestProperty("User-Agent",
            System.getProperties().getProperty("http.agent") + " FacebookAndroidSDK");

    // determine the image size and allocate a buffer
    int fileSize = connection.getContentLength();
    byte[] imageData = new byte[fileSize];

    // download the file
    Log.d(TAG, "fetching image " + imageUrl + " (" + fileSize + ")");

    if (fileSize > 0) {

        BufferedInputStream istream = new BufferedInputStream(connection.getInputStream());
        int bytesRead = 0;
        int offset = 0;
        while (bytesRead != -1 && offset < fileSize) {
            bytesRead = istream.read(imageData, offset, fileSize - offset);
            offset += bytesRead;// w w w  .java 2  s. c om
        }

        istream.close();
    } else
        Log.d(TAG, "fileSize is 0! skipping");

    // clean up
    connection.disconnect();

    return imageData;
}

From source file:Main.java

private static String getData(String target, String method, String token) {
    try {/* w ww. j a  v  a  2s.  com*/
        URL url = new URL(target);
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

        if (!TextUtils.isEmpty(token))
            conn.setRequestProperty("Authorization", token);

        conn.setRequestMethod(method);
        conn.connect();

        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line, data = "";
        while ((line = in.readLine()) != null)
            data += line;
        in.close();

        return data;
    } catch (IOException ignored) {
        ignored.printStackTrace();
    }

    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);//from   ww w.  ja v a 2 s.  com
    httpConnection.connect();
    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

private static String httpPost(String address, String params) {
    InputStream inputStream = null;
    HttpURLConnection urlConnection = null;
    try {/*from  w  w  w .j  a  v  a  2s. 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:Main.java

public static Bitmap downLoadBitmap(String httpUrl) {
    InputStream inputStream = null;
    try {/*from  w  w w .ja v  a  2  s  . c o m*/
        URL url = new URL(httpUrl);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        conn.setReadTimeout(5000);
        conn.setConnectTimeout(5000);
        conn.connect();
        int responseCode = conn.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            inputStream = conn.getInputStream();

            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

            return bitmap;
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return null;
}

From source file:editor.util.URLUtil.java

public static String sendPost(String url, String data) throws Exception {

    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    //add reuqest header
    con.setRequestMethod("POST");

    // Send post request
    con.setDoOutput(true);//ww w . j  a  v a 2  s .c om
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(data);
    wr.flush();
    wr.close();

    int responseCode = con.getResponseCode();

    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();
    return response.toString();
}

From source file:com.peer2gear.nutch.xquery.XQueryParseFilter.java

private static Content createContent(Configuration conf, String urlStr)
        throws FileNotFoundException, IOException {
    String contentType = "text/html";
    URL url = new URL(urlStr);
    URLConnection connection = url.openConnection();
    InputStream is = connection.getInputStream();
    byte bytes[] = IOUtils.toByteArray(is);
    Content content = new Content(urlStr, urlStr, bytes, contentType, new Metadata(), conf);
    return content;
}