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:test.LocationCrawler.java

private static String ProcessLocationRequest(String QueryItem) {
    try {//from www .jav  a2  s .c o  m
        QueryItem = URLEncoder.encode(QueryItem, "ISO-8859-1");
        String URLStr = String.format(
                "https://maps.googleapis.com/maps/api/geocode/json?" + "address=%s&sensor=false&key=%s",
                QueryItem,
                //"AIzaSyAOkEu7MBxUj4t2pBq1GZ-0Td7cf7bOKTg");
                //"AIzaSyCvzTx408371P7CtoXN8BejAAmBa0NUZbc");
                "AIzaSyAIyQ3XaHrC9TQVMIs65IrwzREtnzqZRKw");
        URL url = new URL(URLStr);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");

        if (connection.getResponseCode() == 200) {
            InputStream is = connection.getInputStream();
            InputStreamReader isr = new InputStreamReader(is, "UTF-8");
            BufferedReader br = new BufferedReader(isr);
            String sb = "";
            String str;

            while ((str = br.readLine()) != null) {
                sb += (str);
            }

            br.close();

            return sb;
        } else {
            return "";
        }
    } catch (Exception e) {
        return "";
    }
}

From source file:net.idlesoft.android.apps.github.utils.GravatarCache.java

private static Bitmap downloadGravatar(final String id) throws IOException {
    final URL aURL = new URL("http://www.gravatar.com/avatar/" + URLEncoder.encode(id) + "?size=100&d=mm");
    final HttpURLConnection conn = (HttpURLConnection) aURL.openConnection();
    conn.setDoInput(true);// w  ww.  ja  va2 s.c o  m
    conn.connect();
    final InputStream is = conn.getInputStream();
    final Bitmap bm = BitmapFactory.decodeStream(is);
    is.close();
    return bm;
}

From source file:Main.java

public static String request(String httpUrl, String httpArg) {
    BufferedReader reader = null;
    String result = null;/*from   w w  w .jav  a 2  s  .co  m*/
    StringBuffer sbf = new StringBuffer();
    httpUrl = httpUrl + "?" + httpArg;

    try {
        URL url = new URL(httpUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("apikey", "2ffbcae4d025b6c109af30d7de2d7c09");
        connection.connect();
        InputStream is = connection.getInputStream();
        reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        String strRead = null;
        while ((strRead = reader.readLine()) != null) {
            sbf.append(strRead);
            sbf.append("\r\n");
        }
        reader.close();
        result = sbf.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:com.leosys.core.utils.HttpServiceImpl.java

public static String getMyitem(String letter, Integer page) {
    Properties p = new Properties();
    String urls = getXmlPath();//  w w  w . jav a2  s.c  o  m
    try {
        p.load(new FileInputStream(urls));
    } catch (Exception e) {
        e.printStackTrace();
    }
    String subUrl = p.getProperty("path");
    String restUrl = subUrl + "model=myservice&action=getwebdomainsitebypage&page=" + page + "&pagesize=10";
    if (StringUtils.isNotEmpty(letter)) {
        restUrl += "&letter=" + letter;
    }
    StringBuffer strBuf;
    strBuf = new StringBuffer();

    try {
        URL url = new URL(restUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));//?  
        String line = null;
        while ((line = reader.readLine()) != null)
            strBuf.append(line + " ");
        reader.close();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println(strBuf.toString());
    return strBuf.toString();
}

From source file:com.leosys.core.utils.HttpServiceImpl.java

public static String getZiku(String domin) {
    Properties p = new Properties();
    String urls = getXmlPath();/*from  w ww. j  av  a  2  s  . co m*/
    try {
        p.load(new FileInputStream(urls));
    } catch (Exception e) {
        e.printStackTrace();
    }
    String subUrl = p.getProperty("path");
    String restUrl = subUrl + "model=myservice&action=getwebsiteziku&domain=" + domin + "&pagesize=1000";
    StringBuffer strBuf;
    strBuf = new StringBuffer();

    try {
        URL url = new URL(restUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");

        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));//?  
        String line = null;
        while ((line = reader.readLine()) != null)
            strBuf.append(line + " ");
        reader.close();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println(strBuf.toString());

    return strBuf.toString();
}

From source file:org.esupportail.twitter.services.OAuthTwitterApplicationOnlyService.java

private static String readResponse(HttpURLConnection connection) {
    try {//from   w  w  w . j  av a  2s. com
        StringBuilder str = new StringBuilder();

        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line = "";
        while ((line = br.readLine()) != null) {
            str.append(line + System.getProperty("line.separator"));
        }
        return str.toString();
    } catch (IOException e) {
        return new String();
    }
}

From source file:Main.java

public static String loadImageFromUrl(Context context, String imageURL, File file) {
    try {//from  w w  w.  j a v  a  2s .c o  m
        URL url = new URL(imageURL);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setConnectTimeout(5 * 1000);
        con.setDoInput(true);
        con.connect();
        if (con.getResponseCode() == 200) {
            InputStream inputStream = con.getInputStream();

            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024 * 20];
            int length = -1;
            while ((length = inputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, length);
            }
            byteArrayOutputStream.close();
            inputStream.close();

            FileOutputStream outputStream = new FileOutputStream(file);
            outputStream.write(byteArrayOutputStream.toByteArray());
            outputStream.close();
            return file.getPath();
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:gov.nist.healthcare.ttt.webapp.common.controller.GetCCDADocumentsController.java

public static JSONObject getHTML(String urlToRead) throws Exception {
    StringBuilder result = new StringBuilder();
    URL url = new URL(urlToRead);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;/*from w w  w.  ja v  a2 s  .  c  o  m*/
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }
    rd.close();
    return new JSONObject(result.toString());
}

From source file:org.kontalk.upload.KontalkBoxUploadConnection.java

public static String responseToString(HttpURLConnection conn, final Charset charset) throws IOException {
    final InputStream instream = conn.getInputStream();
    if (instream == null) {
        return null;
    }/*  ww w.  ja v  a2  s .  c  o m*/
    try {
        int i = conn.getContentLength();
        if (i < 0) {
            i = 4096;
        }
        final Reader reader = new InputStreamReader(instream, charset);
        final StringBuilder buffer = new StringBuilder(i);
        final char[] tmp = new char[1024];
        int l;
        while ((l = reader.read(tmp)) != -1) {
            buffer.append(tmp, 0, l);
        }
        return buffer.toString();
    } finally {
        instream.close();
    }
}

From source file:com.javielinux.utils.Translate.java

/**
 * Forms an HTTP request and parses the response for a translation.
 *
 * @param text The String to translate./*from   w  ww.j  ava  2  s.  c o m*/
 * @param from The language code to translate from.
 * @param to The language code to translate to.
 * @return The translated String.
 * @throws Exception
 */
private static String retrieveTranslation(String text, String from, String to) throws Exception {
    try {
        StringBuilder url = new StringBuilder();
        url.append(URL_STRING).append(from).append("%7C").append(to);
        url.append(TEXT_VAR).append(URLEncoder.encode(text, ENCODING));

        Log.d(Utils.TAG, "Conectadndo a " + url.toString());
        HttpURLConnection uc = (HttpURLConnection) new URL(url.toString()).openConnection();
        uc.setDoInput(true);
        uc.setDoOutput(true);
        try {
            InputStream is = uc.getInputStream();
            String result = toString(is);

            JSONObject json = new JSONObject(result);
            return ((JSONObject) json.get("responseData")).getString("translatedText");
        } finally { // http://java.sun.com/j2se/1.5.0/docs/guide/net/http-keepalive.html
            uc.getInputStream().close();
            if (uc.getErrorStream() != null)
                uc.getErrorStream().close();
        }
    } catch (Exception ex) {
        throw ex;
    }
}