List of usage examples for java.net URLConnection setReadTimeout
public void setReadTimeout(int timeout)
From source file:Main.java
public static void main(String args[]) throws Exception { URL u = new URL("http://www.java2s.com"); URLConnection uc = u.openConnection(); uc.setReadTimeout(1000); System.out.println(uc.getReadTimeout()); }
From source file:com.util.finalProy.java
/** * @param args the command line arguments *//*from w w w.j a v a 2s . co m*/ public static void main(String[] args) throws JSONException { // TODO code application logic her System.out.println("OBTENER SOLO UN ARRAY DE CADENA JSON"); String myURL = "http://192.168.5.44/app_dev.php/cus/getaccount/50241109321.json"; System.out.println("Requested URL:" + myURL); StringBuilder sb = new StringBuilder(); URLConnection urlConn = null; InputStreamReader in = null; try { URL url = new URL(myURL); urlConn = url.openConnection(); if (urlConn != null) { urlConn.setReadTimeout(60 * 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 (Exception e) { e.printStackTrace(); throw new RuntimeException("Exception while calling URL:" + myURL, e); } String jsonResult = sb.toString(); System.out.println(sb.toString()); System.out.println( "\n\n--------------------OBTENEMOS OBJETO JSON NATIVO DE LA PAGINA, USAMOS EL ARRAY DATA---------------------------\n\n"); JSONObject objJason = new JSONObject(jsonResult); JSONArray dataJson = new JSONArray(); dataJson = objJason.getJSONArray("data"); System.out.println("objeto normal 1 " + dataJson.toString()); // // System.out.println( "\n\n--------------------CREAMOS UN STRING JSON2 REEMPLAZANDO LOS CORCHETES QUE DAN ERROR---------------------------\n\n"); String jsonString2 = dataJson.toString(); String temp = dataJson.toString(); temp = jsonString2.replace("[", ""); jsonString2 = temp.replace("]", ""); System.out.println("new json string" + jsonString2); JSONObject objJson2 = new JSONObject(jsonString2); System.out.println("el objeto simple json es " + objJson2.toString()); System.out.println( "\n\n--------------------CREAMOS UN OBJETO JSON CON EL ARRAR ACCOUN---------------------------\n\n"); String account1 = objJson2.optString("account"); System.out.println(account1); JSONObject objJson3 = new JSONObject(account1); System.out.println("el ULTIMO OBJETO SIMPLE ES " + objJson3.toString()); System.out.println( "\n\n--------------------EMPEZAMOS A RECIBIR LOS PARAMETROS QUE HAY EN EL OBJETO JSON---------------------------\n\n"); String firstName = objJson3.getString("first_name"); System.out.println(firstName); System.out.println(objJson3.get("id")); System.out.println( "\n\n--------------------TRATAMOS DE PASAR TODO EL ACCOUNT A OBJETO JAVA---------------------------\n\n"); Gson gson = new Gson(); Account account = gson.fromJson(objJson3.toString(), Account.class); System.out.println(account.getFirst_name()); System.out.println(account.getCreation()); }
From source file:org.dlut.mycloudserver.service.performancemonitor.PerformanceListener.java
public static void main(String[] args) { long t1 = System.currentTimeMillis(); String url = "http://127.0.0.1:8001"; URLConnection conn; try {//from ww w. j a v a2 s . com conn = new URL(url).openConnection(); conn.setConnectTimeout(1000); conn.setReadTimeout(1000); InputStream is = conn.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String res = br.readLine(); br.close(); is.close(); JSONObject jsonRes = JSON.parseObject(res); double loadAverage = jsonRes.getDoubleValue("loadAverage"); System.out.println(loadAverage); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } long t2 = System.currentTimeMillis(); System.out.println((t2 - t1) + "ms"); }
From source file:Main.java
private static void setTimeout(URLConnection conn, int milliseconds) { conn.setConnectTimeout(milliseconds); conn.setReadTimeout(milliseconds); }
From source file:Main.java
public static File getFileFromCacheOrURL(File cacheDir, URL url) throws IOException { Log.i(TAG, "getFileFromCacheOrURL(): url = " + url.toExternalForm()); String filename = url.getFile(); int lastSlashPos = filename.lastIndexOf('/'); String fileNameNoPath = new String(lastSlashPos == -1 ? filename : filename.substring(lastSlashPos + 1)); File file = new File(cacheDir, fileNameNoPath); if (file.exists()) { if (file.length() > 0) { Log.i(TAG, "File exists in cache as: " + file.getAbsolutePath()); return file; } else {//w ww . j a v a 2 s .c o m Log.i(TAG, "Deleting zero length file " + file.getAbsolutePath()); file.delete(); } } Log.i(TAG, "File " + file.getAbsolutePath() + " does not exists."); URLConnection ucon = url.openConnection(); ucon.setReadTimeout(5000); ucon.setConnectTimeout(30000); InputStream is = ucon.getInputStream(); BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5); FileOutputStream outStream = new FileOutputStream(file); byte[] buff = new byte[5 * 1024]; // Read bytes (and store them) until there is nothing more to read(-1) int len; while ((len = inStream.read(buff)) != -1) { outStream.write(buff, 0, len); } // Clean up outStream.flush(); outStream.close(); inStream.close(); return file; }
From source file:org.enderstone.server.uuid.ServerRequest.java
public static JSONObject parseDataFromURL(String connect) throws IOException { URL url = new URL(connect); URLConnection uc = url.openConnection(); uc.setConnectTimeout(5000);/*from ww w . j av a 2s . c o m*/ uc.setReadTimeout(5000); uc.connect(); String encoding = uc.getContentEncoding(); encoding = encoding == null ? "UTF-8" : encoding; try (Scanner scanner = new Scanner(uc.getInputStream(), encoding)) { scanner.useDelimiter("\\A"); return new JSONObject(scanner.next()); } catch (NoSuchElementException noskin) { return new JSONObject(); } }
From source file:org.bimserver.utils.NetUtils.java
public static String getContent(URL url, int timeOut) throws IOException { URLConnection openConnection = url.openConnection(); openConnection.setConnectTimeout(timeOut); openConnection.setReadTimeout(timeOut); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); InputStream in = openConnection.getInputStream(); IOUtils.copy(in, byteArrayOutputStream); in.close();// w w w.j av a2 s . c om return new String(byteArrayOutputStream.toByteArray(), Charsets.UTF_8); }
From source file:org.bimserver.utils.NetUtils.java
public static byte[] getContentAsBytes(URL url, int timeOut) throws IOException { URLConnection openConnection = url.openConnection(); openConnection.setConnectTimeout(timeOut); openConnection.setReadTimeout(timeOut); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); InputStream in = openConnection.getInputStream(); IOUtils.copy(in, byteArrayOutputStream); in.close();//from www.j a va 2 s .com return byteArrayOutputStream.toByteArray(); }
From source file:com.bdaum.zoom.net.core.internal.Activator.java
public static InputStream openStream(String url, int timeout) throws MalformedURLException, IOException, HttpException { URL u = new URL(url); URLConnection con = u.openConnection(); con.setConnectTimeout(timeout);/*from w w w . j ava 2 s. c o m*/ con.setReadTimeout(timeout); try { return new BufferedInputStream(con.getInputStream()); } catch (IOException e) { int responseCode = -1; if (con instanceof HttpURLConnection) responseCode = ((HttpURLConnection) con).getResponseCode(); else if (con instanceof HttpsURLConnection) responseCode = ((HttpsURLConnection) con).getResponseCode(); if (responseCode >= 0 && responseCode != HttpStatus.SC_OK) throw new HttpException(getStatusText(responseCode), e); throw e; } }
From source file:skinsrestorer.utils.DataUtils.java
public static SkinProperty getProp(String id) throws IOException, ParseException, SkinFetchFailedException { URL url = new URL(skullbloburl + id + "?unsigned=false"); URLConnection connection = url.openConnection(); connection.setConnectTimeout(10000); connection.setReadTimeout(10000); connection.setUseCaches(false);//from w ww . jav a2 s .c om InputStream is = connection.getInputStream(); String result = IOUtils.toString(is, Charsets.UTF_8); IOUtils.closeQuietly(is); JSONArray properties = (JSONArray) ((JSONObject) new JSONParser().parse(result)).get("properties"); for (int i = 0; i < properties.size(); i++) { JSONObject property = (JSONObject) properties.get(i); String name = (String) property.get("name"); String value = (String) property.get("value"); String signature = (String) property.get("signature"); if (name.equals("textures")) { return new SkinProperty(name, value, signature); } } throw new SkinFetchFailedException(SkinFetchFailedException.Reason.NO_SKIN_DATA); }