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

public static String jsonGetRequest(String urlQueryString) {
    String json = null;/*from   w w  w  .j  ava 2 s. c om*/
    try {
        URL url = new URL(urlQueryString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setInstanceFollowRedirects(false);
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("charset", "utf-8");
        connection.connect();
        InputStream inStream = connection.getInputStream();
        json = streamToString(inStream); // input stream to string
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return json;
}

From source file:Main.java

public static Drawable GetUrlDrawable(String url) {
    try {/*from w  w w  .j a va  2  s.  c o  m*/
        URL aryURI = new URL(url);
        URLConnection conn = aryURI.openConnection();
        InputStream is = conn.getInputStream();
        Bitmap bmp = BitmapFactory.decodeStream(is);
        return new BitmapDrawable(bmp);
    } catch (Exception e) {
        Log.e("ERROR", "urlImage2Drawable failed with image url at " + url, e);
        return null;
    }
}

From source file:Main.java

public static String getJsonString(String urlString) throws Exception {
    InputStream is = null;//from w  w  w  . j av  a  2  s  .c o  m
    Reader reader = null;
    StringBuilder str = new StringBuilder();
    URL url = new URL(urlString);
    URLConnection URLConn = url.openConnection();
    URLConn.setRequestProperty("User-agent", "IE/6.0");
    is = URLConn.getInputStream();
    reader = new InputStreamReader(is, "UTF-8");
    char[] buffer = new char[1];
    while (reader.read(buffer) != -1) {
        str.append(new String(buffer));
    }
    return str.toString();
}

From source file:Main.java

protected static InputStream getInputStream(String path) throws IOException {
    URL url = new URL(path);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    return connection.getInputStream();
}

From source file:Main.java

private static InputStream OpenHttpConnection(String strURL, Boolean cache) throws IOException {
    InputStream inputStream = null;

    try {/*from   ww w . j  av a2 s .  com*/
        URL url = new URL(strURL);
        URLConnection conn = url.openConnection();
        conn.setUseCaches(cache);
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setRequestMethod("GET");
        httpConn.connect();

        if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK)
            inputStream = httpConn.getInputStream();

    } catch (Exception ex) {
    }
    return inputStream;
}

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 w  w .j  av a 2 s. com*/
}

From source file:Main.java

public static boolean delete(String murl) throws Exception {
    URL url = new URL(murl);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("DELETE");
    conn.setConnectTimeout(5000);/* w  w w  . ja va 2 s. co m*/
    if (conn.getResponseCode() == 204) {
        return true;
    }
    return false;
}

From source file:Main.java

public static Drawable getDrawable(String urlpath) {
    Drawable d = null;//  w w  w. j  a  va2 s  .  c o  m
    try {
        URL url = new URL(urlpath);
        URLConnection conn = url.openConnection();
        conn.connect();
        InputStream in;
        in = conn.getInputStream();
        d = Drawable.createFromStream(in, "background.jpg");
        // TODO Auto-generated catch block
    } catch (IOException e) {
        e.printStackTrace();
    }
    return d;
}

From source file:Main.java

private static HttpURLConnection getHttpURLConnection(String strURL) throws IOException {
    URL url = new URL(strURL);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setConnectTimeout(10000);//w  w w .j  a va2s.co  m
    conn.setReadTimeout(15000);
    return conn;
}

From source file:Main.java

public static Bitmap getBitmap(String biturl) {

    Bitmap bitmap = null;//from   ww w  . j a  va 2s  .co  m

    try {

        URL url = new URL(biturl);
        URLConnection conn = url.openConnection();
        InputStream in = conn.getInputStream();
        bitmap = BitmapFactory.decodeStream(new BufferedInputStream(in));

    } catch (Exception e) {

        e.printStackTrace();
    }
    return bitmap;

}