List of usage examples for java.net URL openConnection
public URLConnection openConnection() throws java.io.IOException
From source file:com.uk_postcodes.api.Postcode.java
/** * Get the postcode closest to the location. * @param location The device's location * @return the closest postcode/*from ww w . j a v a 2 s .c o m*/ * @throws Exception */ public static String forLocation(Location location) throws Exception { String address = String.format(CALL, location.getLatitude(), location.getLongitude()); Log.d("Postcode", "Calling: " + address); URL callUrl = new URL(address); HttpURLConnection callConnection = (HttpURLConnection) callUrl.openConnection(); // The ten-second rule: // If there's no data in 10s (or TIMEOUT), assume the worst. callConnection.setReadTimeout(10000); // Set the request method to GET. callConnection.setRequestMethod("GET"); int code = callConnection.getResponseCode(); if (code != HttpURLConnection.HTTP_OK) { throw new Exception("A non-200 code was returned: " + code); } String responseStr; responseStr = IOUtils.toString(callConnection.getInputStream()); callConnection.disconnect(); JsonParser jp = new JsonParser(); JsonElement response = jp.parse(responseStr); return response.getAsJsonObject().get("postcode").getAsString(); }
From source file:Main.java
/** * Return '' or error message if error occurs during URL connection. * /*from w ww . j a v a 2s. c om*/ * @param url The URL to ckeck * @return */ public static String getUrlStatus(String url) { URL u; URLConnection conn; int connectionTimeout = 500; try { u = new URL(url); conn = u.openConnection(); conn.setConnectTimeout(connectionTimeout); // TODO : set proxy if (conn instanceof HttpURLConnection) { HttpURLConnection httpConnection = (HttpURLConnection) conn; httpConnection.setInstanceFollowRedirects(true); httpConnection.connect(); httpConnection.disconnect(); // FIXME : some URL return HTTP200 with an empty reply from server // which trigger SocketException unexpected end of file from server int code = httpConnection.getResponseCode(); if (code == HttpURLConnection.HTTP_OK) { return ""; } else { return "Status: " + code; } } // TODO : Other type of URLConnection } catch (Exception e) { e.printStackTrace(); return e.toString(); } return ""; }
From source file:test.LocationCrawler.java
private static String ProcessLocationRequest(String QueryItem) { try {/*from w w w .j a va2 s .com*/ 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:com.pwned.utils.VersionCheck.java
private static void _versionCheck() throws IOException { URL myURL = new URL(Constants.VERSIONCHECK_URL); URLConnection ucon = myURL.openConnection(); InputStream is = ucon.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(50); int current = 0; while ((current = bis.read()) != -1) { baf.append((byte) current); }/* w ww . j a v a2s. c om*/ Logger.log("start version check", new String(baf.toByteArray())); try { currentVersionName = Integer.parseInt(new String(baf.toByteArray())); } catch (Exception e) { currentVersionName = 1; } ((Activity) context).runOnUiThread(returnRes); }
From source file:org.elegosproject.romupdater.DownloadManager.java
public static boolean checkHttpFile(String url) { try {/*from w w w . j a v a2 s . co m*/ HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, 3000); Log.i(TAG, "Testing " + url + "..."); URL theUrl = new URL(url); HttpURLConnection connection = (HttpURLConnection) theUrl.openConnection(); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { connection.disconnect(); } else { Log.i(TAG, "HTTP Response code: " + connection.getResponseCode()); return false; } } catch (IOException e) { Log.e(TAG, e.toString()); return false; } return true; }
From source file:ee.ria.xroad.common.conf.globalconf.ConfigurationLocation.java
/** * @return the connection used to connect to the download url * @throws IOException/*from w w w . j a v a 2 s .co m*/ */ public static URLConnection getDownloadURLConnection(String urlStr) throws IOException { URL url = new URL(urlStr); URLConnection connection = url.openConnection(); connection.setReadTimeout(READ_TIMEOUT); return connection; }
From source file:me.sonarbeserk.lockup.utils.UUIDFetcher.java
private static HttpURLConnection createConnection(int page) throws Exception { URL url = new URL(PROFILE_URL + page); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setUseCaches(false);/* w w w. j a va 2s. c o m*/ connection.setDoInput(true); connection.setDoOutput(true); return connection; }
From source file:Main.java
public static byte[] getHtmlByteArray(final String url) { URL htmlUrl = null; InputStream inStream = null;//from www . j a v a 2s . c o m 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:Main.java
public static String getPosthtml(String posturl, String postData, String encode) { String html = ""; URL url; try {//from w ww. j ava 2s . co m url = new URL(posturl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("User-Agent", userAgent); String postDataStr = postData; byte[] bytes = postDataStr.getBytes("utf-8"); connection.setRequestProperty("Content-Length", "" + bytes.length); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Cache-Control", "no-cache"); connection.setDoOutput(true); connection.setReadTimeout(timeout); connection.setFollowRedirects(true); connection.connect(); OutputStream outStrm = connection.getOutputStream(); outStrm.write(bytes); outStrm.flush(); outStrm.close(); InputStream inStrm = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(inStrm, encode)); String temp = ""; while ((temp = br.readLine()) != null) { html += (temp + '\n'); } br.close(); connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } return html; }
From source file:com.meetingninja.csse.database.BaseDatabaseAdapter.java
protected static String updateHelper(String jsonPayload) throws IOException { // Server URL setup String _url = getBaseUri().build().toString(); // Establish connection URL url = new URL(_url); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // add request header conn.setRequestMethod(IRequest.PUT); addRequestHeader(conn, true);/*from w ww .j a v a 2s.c o m*/ int responseCode = sendPostPayload(conn, jsonPayload); String response = getServerResponse(conn); conn.disconnect(); return response; }