List of usage examples for java.net HttpURLConnection getInputStream
public InputStream getInputStream() throws IOException
From source file:org.cytoscape.app.internal.net.server.AddAccessControlAllowOriginHeaderAfterResponseTest.java
private static String readConnection(HttpURLConnection connection) throws Exception { final StringBuffer buffer = new StringBuffer(); final BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); while (true) { final String line = reader.readLine(); if (line == null) break; buffer.append(line);/*from w w w . ja v a2 s . c om*/ } return buffer.toString(); }
From source file:Main.java
public static byte[] getHtmlByteArray(final String url) { URL htmlUrl = null;/*from w w w.j ava 2 s . c o m*/ InputStream inStream = null; try { htmlUrl = new URL(url); URLConnection connection = htmlUrl.openConnection(); HttpURLConnection httpConnection = (HttpURLConnection) connection; int responseCode = httpConnection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { inStream = httpConnection.getInputStream(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } byte[] data = inputStreamToByte(inStream); return data; }
From source file:eu.hansolo.tilesfx.weather.Sun.java
public static ZonedDateTime[] getSunriseSunsetAt(final double LATITUDE, final double LONGITUDE, final ZoneId ZONE_ID) { StringBuilder response = new StringBuilder(); try {/*w w w . j a v a 2s . c o m*/ final String URL_STRING = new StringBuilder(SUNRISE_SUNSET_URL).append("lat=").append(LATITUDE) .append("&lng=").append(LONGITUDE).append("&date=today&formatted=0").toString(); final HttpURLConnection CONNECTION = (HttpURLConnection) new URL(URL_STRING).openConnection(); final BufferedReader IN = new BufferedReader(new InputStreamReader(CONNECTION.getInputStream())); String inputLine; while ((inputLine = IN.readLine()) != null) { response.append(inputLine).append("\n"); } IN.close(); return parseJsonData(response.toString(), ZONE_ID); } catch (IOException ex) { return null; } }
From source file:com.tnc.android.graphite.utils.GraphiteConnection.java
public static DrawableGraph getGraph(String serverUrl, String ps) throws Exception { URL url = new URL(serverUrl + GRAPH_PARAM_STRING + ps); HttpURLConnection http = (HttpURLConnection) url.openConnection(); http.setConnectTimeout(45000);//w ww.j a v a 2 s. c om http.setReadTimeout(45000); Drawable image = Drawable.createFromStream(http.getInputStream(), null); DrawableGraph dg = new DrawableGraph(); dg.setImage(image); return dg; }
From source file:Main.java
public static void getNewsJSON(final String url, final Handler handler) { new Thread(new Runnable() { @Override/*from w ww . ja v a2 s . c om*/ public void run() { HttpURLConnection conn; InputStream is; try { conn = (HttpURLConnection) new URL(url).openConnection(); conn.setRequestMethod("GET"); is = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String line = ""; StringBuffer result = new StringBuffer(); while ((line = reader.readLine()) != null) { result.append(line); } Message msg = new Message(); msg.obj = result.toString(); handler.sendMessage(msg); } catch (Exception e) { e.printStackTrace(); } } }).start(); }
From source file:me.malladi.dashcricket.Util.java
/** * Fetch the current live scores.// w w w. j ava 2 s. co m * * @return An array of the current live scores. */ public static LiveScore[] getLiveScores() { try { URL url = new URL(LIVE_SCORES_URL); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setUseCaches(false); BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); StringBuilder json = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { json.append(line); } JSONArray jsonArray = new JSONArray(json.toString()); LiveScore[] liveScores = new LiveScore[jsonArray.length()]; for (int i = 0; i < jsonArray.length(); ++i) { JSONObject liveScoreJson = (JSONObject) jsonArray.opt(i); liveScores[i] = new LiveScore(liveScoreJson); } return liveScores; } catch (Exception e) { Log.e(TAG, "exception while fetching live scores", e); } return null; }
From source file:com.adanac.module.blog.api.HttpApiHelper.java
public static String getCity(String ip) { String city = "?"; if (ip.equals("127.0.0.1")) { return city; }/*from w w w . j a v a 2 s. co m*/ String url = "http://api.map.baidu.com/location/ip?ak=" + BAIDU_AK + "&ip=" + ip; try { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); String json = IOUtil.read(connection.getInputStream()); if (logger.isInfoEnabled()) { logger.info("baidu-ip-api json : " + json); } JSONObject resultJsonObject = JSONObject.fromObject(json); if (resultJsonObject == null) { return city; } JSONObject contentJsonObject = resultJsonObject.containsKey("content") ? resultJsonObject.getJSONObject("content") : null; if (contentJsonObject == null) { return city; } JSONObject addressDetailJsonObject = contentJsonObject.containsKey("address_detail") ? contentJsonObject.getJSONObject("address_detail") : null; if (addressDetailJsonObject == null) { return city; } String cityInResult = addressDetailJsonObject.getString("city"); String provinceInResult = addressDetailJsonObject.getString("province"); String address = StringUtils.isEmpty(cityInResult) ? provinceInResult : cityInResult; if (!StringUtils.isEmpty(address)) { city = address; } } catch (Exception e) { logger.error("get city failed for ip : " + ip, e); } return city; }
From source file:Main.java
public static String SimpleHttp(String urlStr) throws Exception { URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); int resCode = conn.getResponseCode(); InputStream input = null;//from www. j a v a 2s . c o m String result = null; if (resCode == 200) { input = conn.getInputStream(); result = toString(input); input.close(); conn.disconnect(); } else { throw new Exception("connect failed"); } return result; }
From source file:Main.java
public static String get(String url) { try {//from w w w . j a va 2 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(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String response = ""; String readLine = ""; while ((readLine = br.readLine()) != null) { response += readLine; } br.close(); return response; } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:com.adguard.compiler.UrlUtils.java
public static String downloadString(URL url, String encoding) throws IOException { HttpURLConnection connection = null; InputStream inputStream = null; try {//from www . j a v a 2s . c o m connection = (HttpURLConnection) url.openConnection(); connection.connect(); inputStream = connection.getInputStream(); return IOUtils.toString(inputStream, encoding); } finally { IOUtils.closeQuietly(inputStream); if (connection != null) { connection.disconnect(); } } }