List of usage examples for java.net URLConnection setReadTimeout
public void setReadTimeout(int timeout)
From source file:de.schildbach.wallet.goldcoin.ExchangeRatesProvider.java
private static Map<String, ExchangeRate> getBlockchainInfo() { try {// w w w . j a va 2 s .co m //double btcRate = getGoldCoinValueBTC(); Double btcRate = 0.0; Object result = getGoldCoinValueBTC(); if (result == null) return null; else btcRate = (Double) result; final URL URL = new URL("https://blockchain.info/ticker"); final URLConnection connection = URL.openConnection(); connection.setConnectTimeout(TIMEOUT_MS); connection.setReadTimeout(TIMEOUT_MS); connection.connect(); final StringBuilder content = new StringBuilder(); Reader reader = null; try { reader = new InputStreamReader(new BufferedInputStream(connection.getInputStream(), 1024)); IOUtils.copy(reader, content); final Map<String, ExchangeRate> rates = new TreeMap<String, ExchangeRate>(); //Add Bitcoin information rates.put("BTC", new ExchangeRate("BTC", Utils.toNanoCoins(String.format("%.8f", btcRate).replace(",", ".")), "pubapi.cryptsy.com")); final JSONObject head = new JSONObject(content.toString()); for (final Iterator<String> i = head.keys(); i.hasNext();) { final String currencyCode = i.next(); final JSONObject o = head.getJSONObject(currencyCode); double gldInCurrency = o.getDouble("15m") * btcRate; final String rate = String.format("%.8f", gldInCurrency); //o.optString("15m", null); if (rate != null) rates.put(currencyCode, new ExchangeRate(currencyCode, Utils.toNanoCoins(rate.replace(",", ".")), URL.getHost())); } return rates; } finally { if (reader != null) reader.close(); } } catch (final IOException x) { x.printStackTrace(); } catch (final JSONException x) { x.printStackTrace(); } return null; }
From source file:org.blazr.extrastorage.ExtraStorage.java
private static String getText(String myURL, boolean main_thread) { StringBuilder sb = new StringBuilder(); URLConnection urlConn = null; InputStreamReader in = null;/* w ww . j av a 2 s . co m*/ try { URL url = new URL(StringEscapeUtils.escapeHtml(myURL)); urlConn = url.openConnection(); if (urlConn != null) if (main_thread) urlConn.setReadTimeout(5 * 1000); else urlConn.setReadTimeout(30 * 1000); if (urlConn != null && urlConn.getInputStream() != null) { in = new InputStreamReader(urlConn.getInputStream(), Charset.defaultCharset()); BufferedReader bufferedReader = new BufferedReader(in); if (bufferedReader != null) { int cp; while ((cp = bufferedReader.read()) != -1) { sb.append((char) cp); } bufferedReader.close(); } } in.close(); } catch (IOException e) { if (e.getMessage().contains("429")) return "wait"; return null; } catch (Exception e) { return null; } return sb.toString(); }
From source file:de.dal33t.powerfolder.util.ConfigurationLoader.java
/** * Loads a pre-configuration from the URL * * @param from//w ww.j ava2s .c o m * the URL to load from * @return the loaded properties WITHOUT those in config. * @throws IOException */ private static Properties loadPreConfiguration(URL from, String un, char[] pw) throws IOException { Reject.ifNull(from, "URL is null"); URLConnection con = from.openConnection(); if (StringUtils.isNotBlank(un)) { String s = un + ":" + Util.toString(pw); LoginUtil.clear(pw); String base64 = "Basic " + Base64.encodeBytes(s.getBytes("UTF-8")); con.setRequestProperty("Authorization", base64); } con.setConnectTimeout(1000 * URL_CONNECT_TIMEOUT_SECONDS); con.setReadTimeout(1000 * URL_CONNECT_TIMEOUT_SECONDS); con.connect(); InputStream in = con.getInputStream(); try { return loadPreConfiguration(in); } finally { try { in.close(); } catch (Exception e) { } } }
From source file:de.schildbach.wallet.goldcoin.ExchangeRatesProvider.java
private static Double getGoldCoinValueLTC() { Date date = new Date(); long now = date.getTime(); if ((now < (lastLitecoinValueCheck + 10 * 60)) && lastLitecoinValue > 0.0) return lastLitecoinValue; //final Map<String, ExchangeRate> rates = new TreeMap<String, ExchangeRate>(); // Keep the LTC rate around for a bit Double ltcRate = 0.0;//from w w w. j a va2s. c o m String currencyCryptsy = "LTC"; String urlCryptsy = "http://pubapi.cryptsy.com/api.php?method=marketdata"; try { // final String currencyCode = currencies[i]; final URL URLCryptsy = new URL(urlCryptsy); final URLConnection connectionCryptsy = URLCryptsy.openConnection(); connectionCryptsy.setConnectTimeout(TIMEOUT_MS); connectionCryptsy.setReadTimeout(TIMEOUT_MS); connectionCryptsy.connect(); final StringBuilder contentCryptsy = new StringBuilder(); Reader reader = null; try { reader = new InputStreamReader(new BufferedInputStream(connectionCryptsy.getInputStream(), 1024)); IOUtils.copy(reader, contentCryptsy); final JSONObject head = new JSONObject(contentCryptsy.toString()); JSONObject returnObject = head.getJSONObject("return"); JSONObject markets = returnObject.getJSONObject("markets"); JSONObject GLD = markets.getJSONObject("GLD"); Double lastTrade = GLD.getDouble("lasttradeprice"); //String euros = String.format("%.7f", lastTrade); // Fix things like 3,1250 //euros = euros.replace(",", "."); //rates.put(currencyCryptsy, new ExchangeRate(currencyCryptsy, Utils.toNanoCoins(euros), URLCryptsy.getHost())); if (currencyCryptsy.equalsIgnoreCase("LTC")) ltcRate = lastTrade; lastLitecoinValue = ltcRate; lastLitecoinValueCheck = now; } finally { if (reader != null) reader.close(); } return ltcRate; } catch (final IOException x) { x.printStackTrace(); } catch (final JSONException x) { x.printStackTrace(); } return null; }
From source file:de.schildbach.wallet.goldcoin.ExchangeRatesProvider.java
private static Map<String, ExchangeRate> getLitecoinChartsOld() { final Map<String, ExchangeRate> rates = new TreeMap<String, ExchangeRate>(); // Keep the BTC rate around for a bit Double ltcRate = getGoldCoinValueLTC(); Double btcRate = 0.0;/*from w w w . j a v a 2s .co m*/ try { String currencies[] = { "USD", "BTC" };//, "RUR"}; String urls[] = { "https://btc-e.com/api/2/14/ticker", "https://btc-e.com/api/2/10/ticker", "https://btc-e.com/api/2/ltc_rur/ticker" }; for (int i = 0; i < currencies.length; ++i) { final String currencyCode = currencies[i]; final URL URL = new URL(urls[i]); final URLConnection connection = URL.openConnection(); connection.setConnectTimeout(TIMEOUT_MS); connection.setReadTimeout(TIMEOUT_MS); connection.connect(); final StringBuilder content = new StringBuilder(); Reader reader = null; try { reader = new InputStreamReader(new BufferedInputStream(connection.getInputStream(), 1024)); IOUtils.copy(reader, content); final JSONObject head = new JSONObject(content.toString()); JSONObject ticker = head.getJSONObject("ticker"); Double avg = ticker.getDouble("avg") * ltcRate; String euros = String.format("%.7f", avg); // Fix things like 3,1250 euros = euros.replace(",", "."); rates.put(currencyCode, new ExchangeRate(currencyCode, Utils.toNanoCoins(euros), URL.getHost())); if (currencyCode.equalsIgnoreCase("BTC")) btcRate = avg; } finally { if (reader != null) reader.close(); } } // Handle LTC/EUR special since we have to do maths final URL URL = new URL("https://btc-e.com/api/2/btc_eur/ticker"); final URLConnection connection = URL.openConnection(); connection.setConnectTimeout(TIMEOUT_MS); connection.setReadTimeout(TIMEOUT_MS); connection.connect(); final StringBuilder content = new StringBuilder(); Reader reader = null; try { reader = new InputStreamReader(new BufferedInputStream(connection.getInputStream(), 1024)); IOUtils.copy(reader, content); final JSONObject head = new JSONObject(content.toString()); JSONObject ticker = head.getJSONObject("ticker"); Double avg = ticker.getDouble("avg"); // This is bitcoins priced in euros. We want GLD! avg *= btcRate; String s_avg = String.format("%.8f", avg).replace(',', '.'); rates.put("EUR", new ExchangeRate("EUR", Utils.toNanoCoins(s_avg), URL.getHost())); //Add LTC information s_avg = String.format("%.8f", ltcRate); rates.put("LTC", new ExchangeRate("LTC", Utils.toNanoCoins(s_avg.replace(",", ".")), URL.getHost())); } finally { if (reader != null) reader.close(); } return rates; } catch (final IOException x) { x.printStackTrace(); } catch (final JSONException x) { x.printStackTrace(); } return null; }
From source file:org.kde.necessitas.ministro.MinistroActivity.java
public static double downloadVersionXmlFile(Context c, boolean checkOnly) { if (!isOnline(c)) return -1; try {//ww w .j ava 2 s . c o m DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document dom = null; Element root = null; URLConnection connection = getVersionUrl(c).openConnection(); connection.setConnectTimeout(CONNECTION_TIMEOUT); connection.setReadTimeout(READ_TIMEOUT); dom = builder.parse(connection.getInputStream()); root = dom.getDocumentElement(); root.normalize(); double version = Double.valueOf(root.getAttribute("latest")); if (MinistroService.instance().getVersion() >= version) return MinistroService.instance().getVersion(); if (checkOnly) return version; String supportedFeatures = null; if (root.hasAttribute("features")) supportedFeatures = root.getAttribute("features"); connection = getLibsXmlUrl(c, version + deviceSupportedFeatures(supportedFeatures)).openConnection(); File file = new File(MinistroService.instance().getVersionXmlFile()); file.delete(); FileOutputStream outstream = new FileOutputStream(MinistroService.instance().getVersionXmlFile()); InputStream instream = connection.getInputStream(); byte[] tmp = new byte[2048]; int downloaded; while ((downloaded = instream.read(tmp)) != -1) outstream.write(tmp, 0, downloaded); outstream.close(); MinistroService.instance().refreshLibraries(false); return version; } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return -1; }
From source file:de.schildbach.wallet.goldcoin.ExchangeRatesProvider.java
private static Map<String, ExchangeRate> getLitecoinCharts() { final Map<String, ExchangeRate> rates = new TreeMap<String, ExchangeRate>(); // Keep the BTC rate around for a bit Double btcRate = 0.0;//from w w w.j av a2 s . co m Object result = getGoldCoinValueBTC(); if (result == null) { return null; } else btcRate = (Double) result; //Double btcRate = 0.0; try { rates.put("BTC", new ExchangeRate("BTC", Utils.toNanoCoins(String.format("%.8f", btcRate).replace(",", ".")), "pubapi.cryptsy.com")); String currencies[] = { "USD" };//, "BTC"};//, "RUR"}; String urls[] = { "https://btc-e.com/api/2/1/ticker" };//, "https://btc-e.com/api/2/14/ticker", "https://btc-e.com/api/2/ltc_rur/ticker"}; for (int i = 0; i < currencies.length; ++i) { final String currencyCode = currencies[i]; final URL URL = new URL(urls[i]); final URLConnection connection = URL.openConnection(); connection.setConnectTimeout(TIMEOUT_MS); connection.setReadTimeout(TIMEOUT_MS); connection.connect(); final StringBuilder content = new StringBuilder(); Reader reader = null; try { reader = new InputStreamReader(new BufferedInputStream(connection.getInputStream(), 1024)); IOUtils.copy(reader, content); final JSONObject head = new JSONObject(content.toString()); JSONObject ticker = head.getJSONObject("ticker"); Double avg = ticker.getDouble("avg") * btcRate; String euros = String.format("%.8f", avg); // Fix things like 3,1250 euros = euros.replace(",", "."); rates.put(currencyCode, new ExchangeRate(currencyCode, Utils.toNanoCoins(euros), URL.getHost())); if (currencyCode.equalsIgnoreCase("BTC")) btcRate = avg; } finally { if (reader != null) reader.close(); } } // Handle LTC/EUR special since we have to do maths final URL URL = new URL("https://btc-e.com/api/2/btc_eur/ticker"); final URLConnection connection = URL.openConnection(); connection.setConnectTimeout(TIMEOUT_MS); connection.setReadTimeout(TIMEOUT_MS); connection.connect(); final StringBuilder content = new StringBuilder(); Reader reader = null; try { reader = new InputStreamReader(new BufferedInputStream(connection.getInputStream(), 1024)); IOUtils.copy(reader, content); final JSONObject head = new JSONObject(content.toString()); JSONObject ticker = head.getJSONObject("ticker"); Double avg = ticker.getDouble("avg"); // This is bitcoins priced in euros. We want GLD! avg *= btcRate; String s_avg = String.format("%.8f", avg).replace(',', '.'); rates.put("EUR", new ExchangeRate("EUR", Utils.toNanoCoins(s_avg), URL.getHost())); //Add LTC information //s_avg = String.format("%.8f", ltcRate); //rates.put("LTC", new ExchangeRate("LTC", Utils.toNanoCoins(s_avg), URL.getHost())); } finally { if (reader != null) reader.close(); } return rates; } catch (final IOException x) { x.printStackTrace(); } catch (final JSONException x) { x.printStackTrace(); } return null; }
From source file:de.schildbach.wallet.goldcoin.ExchangeRatesProvider.java
private static Object getGoldCoinValueBTC() { Date date = new Date(); long now = date.getTime(); if ((now < (lastBitcoinValueCheck + 10 * 60)) && lastBitcoinValue > 0.0) return lastBitcoinValue; //final Map<String, ExchangeRate> rates = new TreeMap<String, ExchangeRate>(); // Keep the LTC rate around for a bit Double btcRate = 0.0;//from ww w .j a v a 2 s. c o m String currencyCryptsy = "BTC"; String urlCryptsy2 = "http://pubapi.cryptsy.com/api.php?method=marketdata"; String urlCryptsy = "http://pubapi.cryptsy.com/api.php?method=singlemarketdata&marketid=30"; try { // final String currencyCode = currencies[i]; final URL URLCryptsy = new URL(urlCryptsy); final URLConnection connectionCryptsy = URLCryptsy.openConnection(); connectionCryptsy.setConnectTimeout(TIMEOUT_MS * 2); connectionCryptsy.setReadTimeout(TIMEOUT_MS * 2); connectionCryptsy.connect(); final StringBuilder contentCryptsy = new StringBuilder(); Reader reader = null; try { reader = new InputStreamReader(new BufferedInputStream(connectionCryptsy.getInputStream(), 1024)); IOUtils.copy(reader, contentCryptsy); final JSONObject head = new JSONObject(contentCryptsy.toString()); JSONObject returnObject = head.getJSONObject("return"); JSONObject markets = returnObject.getJSONObject("markets"); JSONObject GLD = markets.getJSONObject("GLD"); JSONArray recenttrades = GLD.getJSONArray("recenttrades"); double btcTraded = 0.0; double gldTraded = 0.0; for (int i = 0; i < recenttrades.length(); ++i) { JSONObject trade = (JSONObject) recenttrades.get(i); btcTraded += trade.getDouble("total"); gldTraded += trade.getDouble("quantity"); } Double averageTrade = btcTraded / gldTraded; //Double lastTrade = GLD.getDouble("lasttradeprice"); //String euros = String.format("%.7f", averageTrade); // Fix things like 3,1250 //euros = euros.replace(",", "."); //rates.put(currencyCryptsy, new ExchangeRate(currencyCryptsy, Utils.toNanoCoins(euros), URLCryptsy.getHost())); if (currencyCryptsy.equalsIgnoreCase("BTC")) btcRate = averageTrade; lastBitcoinValue = btcRate; lastBitcoinValueCheck = now; } finally { if (reader != null) reader.close(); } return btcRate; } catch (final IOException x) { x.printStackTrace(); } catch (final JSONException x) { x.printStackTrace(); } return null; }
From source file:de.schildbach.wallet.goldcoin.ExchangeRatesProvider.java
private static Object getGoldCoinValueBTC_ccex() { Date date = new Date(); long now = date.getTime(); if ((now < (lastBitcoinValueCheck + 10 * 60)) && lastBitcoinValue > 0.0) return lastBitcoinValue; //final Map<String, ExchangeRate> rates = new TreeMap<String, ExchangeRate>(); // Keep the LTC rate around for a bit Double btcRate = 0.0;//w w w . j av a 2 s.com String currencyCryptsy = "BTC"; String urlCryptsy = "https://c-cex.com/t/gld-btc.json"; try { // final String currencyCode = currencies[i]; final URL URLCryptsy = new URL(urlCryptsy); final URLConnection connectionCryptsy = URLCryptsy.openConnection(); connectionCryptsy.setConnectTimeout(TIMEOUT_MS * 2); connectionCryptsy.setReadTimeout(TIMEOUT_MS * 2); connectionCryptsy.connect(); final StringBuilder contentCryptsy = new StringBuilder(); Reader reader = null; try { reader = new InputStreamReader(new BufferedInputStream(connectionCryptsy.getInputStream(), 1024)); IOUtils.copy(reader, contentCryptsy); final JSONObject head = new JSONObject(contentCryptsy.toString()); //JSONObject returnObject = head.getJSONObject("return"); //JSONObject markets = returnObject.getJSONObject("markets"); JSONObject GLD = head.getJSONObject("ticker"); //JSONArray recenttrades = GLD.getJSONArray("recenttrades"); double btcTraded = 0.0; double gldTraded = 0.0; /*for(int i = 0; i < recenttrades.length(); ++i) { JSONObject trade = (JSONObject)recenttrades.get(i); btcTraded += trade.getDouble("total"); gldTraded += trade.getDouble("quantity"); } Double averageTrade = btcTraded / gldTraded; */ Double averageTrade = head.getDouble("buy"); //Double lastTrade = GLD.getDouble("lasttradeprice"); //String euros = String.format("%.7f", averageTrade); // Fix things like 3,1250 //euros = euros.replace(",", "."); //rates.put(currencyCryptsy, new ExchangeRate(currencyCryptsy, Utils.toNanoCoins(euros), URLCryptsy.getHost())); if (currencyCryptsy.equalsIgnoreCase("BTC")) btcRate = averageTrade; lastBitcoinValue = btcRate; lastBitcoinValueCheck = now; } finally { if (reader != null) reader.close(); } return btcRate; } catch (final IOException x) { x.printStackTrace(); } catch (final JSONException x) { x.printStackTrace(); } return null; }
From source file:mas.MAS.java
/** * @param args the command line arguments *///from w w w . j av a2 s . c o m public static String getData_old(String url_org, int start) { try { String complete_url = url_org + "&$skip=" + start; // String url_str = generateURL(url_org, prop); URL url = new URL(complete_url); URLConnection yc = url.openConnection(); yc.setConnectTimeout(25 * 1000); yc.setReadTimeout(25 * 1000); BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream())); String inputLine; StringBuffer result = new StringBuffer(); while ((inputLine = in.readLine()) != null) { // System.out.println(inputLine); result.append(inputLine); } in.close(); return result.toString(); } catch (MalformedURLException ex) { Logger.getLogger(MAS.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(MAS.class.getName()).log(Level.SEVERE, null, ex); } return null; }