Example usage for java.net URL URL

List of usage examples for java.net URL URL

Introduction

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

Prototype

public URL(String spec) throws MalformedURLException 

Source Link

Document

Creates a URL object from the String representation.

Usage

From source file:Main.java

public static Drawable LoadImageFromWebOperations(String url) {
    try {/*  ww w .  j  ava  2s .co m*/
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "src name");
        return d;
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static Bitmap getBitmap(String s) {
    Bitmap bitmap = null;//ww w  .  ja  va 2s  .  c  om
    try {
        URL url = new URL(s);
        bitmap = BitmapFactory.decodeStream(url.openStream());
    } catch (Exception e) {
        // TODO Auto-generated catch block   
        e.printStackTrace();
    }

    return bitmap;
}

From source file:Main.java

public static boolean isUrlValid(final String strUrl) {
    if (null == strUrl) {
        return false;
    }//from   w ww .j  a  v a 2  s  . c  om
    try {
        new URL(strUrl);
        return true;
    } catch (final MalformedURLException e) {
        return false;
    }
}

From source file:Main.java

public static InputStream HandlerData(String url) {
    InputStream inStream = null;/*from  ww w. j  a  v a 2s.co  m*/

    try {
        URL feedUrl = new URL(url);
        URLConnection conn = feedUrl.openConnection();
        conn.setConnectTimeout(10 * 1000);
        inStream = conn.getInputStream();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return inStream;
}

From source file:Main.java

public static Bitmap InternetUrlToBitmap(String url) throws Exception {
    return inputStreamToBitmap(new URL(url).openStream());
}

From source file:Main.java

public static String requestPage(String urlPath) throws Exception {
    URL url = new URL(urlPath);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    int responseCode = connection.getResponseCode();
    if (responseCode != 200) {
        throw new Exception("Error loading page: " + responseCode + connection.getResponseMessage());
    }/*from  w ww.  j  a va2s. co m*/

    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String inputLine;
    String response = "";

    while ((inputLine = in.readLine()) != null) {
        response += inputLine;
    }

    in.close();
    return response;
}

From source file:Main.java

public static byte[] getBytes(String url) {
    try {//from w w  w  .  j  a  va2  s .co  m
        URL u = new URL(url);
        HttpURLConnection connection = (HttpURLConnection) u.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Charset", "UTF-8");
        InputStream is = connection.getInputStream();
        int contentLength = connection.getContentLength();
        byte[] data = new byte[contentLength];
        int alreadyRead = 0;
        int len = 0;
        while (alreadyRead < contentLength) {
            len = is.read(data, alreadyRead, contentLength - alreadyRead);
            alreadyRead += len;
        }
        is.close();
        return data;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

}

From source file:Main.java

private static HttpURLConnection _openPostConnection(String purl) throws IOException {
    URL url = new URL(purl);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoInput(true);/*from  w  w w  .  jav  a  2s  .  co m*/
    connection.setUseCaches(false);
    connection.setDoOutput(true);
    connection.setRequestMethod("GET");
    connection.setRequestProperty("User-Agent", "Android Client Agent");

    return connection;
}

From source file:WebReader.java

static void getData(String address) throws Exception {
    URL page = new URL(address);
    StringBuffer text = new StringBuffer();
    HttpURLConnection conn = (HttpURLConnection) page.openConnection();
    conn.connect();//  w ww . ja  v  a  2 s .c  o  m
    InputStreamReader in = new InputStreamReader((InputStream) conn.getContent());

    BufferedReader buff = new BufferedReader(in);
    String line = buff.readLine();
    while (line != null) {
        text.append(line + "\n");
        line = buff.readLine();
    }
    System.out.println(text.toString());
}

From source file:Main.java

/**
 * Returns new URL object from the given string URL.
 *///from  w w  w .  j a v a2 s. co  m
private static URL createUrl(String stringUrl) {
    URL url = null;
    try {
        url = new URL(stringUrl);
    } catch (MalformedURLException e) {
        //handle exception
    }
    return url;
}