List of usage examples for java.net URL openConnection
public URLConnection openConnection() throws java.io.IOException
From source file:Main.java
/** * Uses http to post an XML String to a URL. * * @param url/* w w w.ja va2s . c om*/ * @param xmlString * @param return * @throws IOException * @throws UnknownHostException */ public static HttpURLConnection post(String url, String xmlString) throws IOException, UnknownHostException { // open connection URL urlObject = new URL(url); HttpURLConnection connection = (HttpURLConnection) urlObject.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "text/xml; charset=\"utf-8\""); connection.setDoOutput(true); OutputStreamWriter outStream = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); outStream.write(xmlString); outStream.close(); return connection; }
From source file:Clima.Clima.java
public static String 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 . j ava 2 s .co m*/ while ((line = rd.readLine()) != null) { result.append(line); } rd.close(); return result.toString(); }
From source file:com.microsoftopentechnologies.adinteractiveauth.Program.java
private static void httpRequest(URL url) throws IOException { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.getResponseCode();//from w w w .j a v a 2 s . c o m }
From source file:org.elegosproject.romupdater.DownloadManager.java
public static boolean sendAnonymousData() { String link = "http://www.elegosproject.org/android/upload.php"; String data;/* ww w.j a va 2 s.c o m*/ SharedData shared = SharedData.getInstance(); String romName = shared.getRepositoryROMName(); String romVersion = shared.getDownloadVersion(); String romPhone = shared.getRepositoryModel(); String romRepository = shared.getRepositoryUrl(); if (romName.equals("") || romVersion.equals("") || romPhone.equals("") || romRepository.equals("")) { Log.e(TAG, "Internal error - missing system variables."); return false; } if (!checkHttpFile(link)) return false; try { data = URLEncoder.encode("phone", "UTF-8") + "=" + URLEncoder.encode(romPhone, "UTF-8"); data += "&" + URLEncoder.encode("rom_name", "UTF-8") + "=" + URLEncoder.encode(romName, "UTF-8"); data += "&" + URLEncoder.encode("rom_version", "UTF-8") + "=" + URLEncoder.encode(romVersion, "UTF-8"); data += "&" + URLEncoder.encode("rom_repository", "UTF-8") + "=" + URLEncoder.encode(romRepository, "UTF-8"); HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, 3000); URL url = new URL(link); url.openConnection(); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("User-Agent", "ROMUpdater"); conn.setDoOutput(true); PrintWriter out = new PrintWriter(conn.getOutputStream()); out.println(data); out.close(); int status = Integer.parseInt(conn.getHeaderField("ROMUpdater-status")); if (status == 1) return true; Log.e(TAG, "It was impossible to send data to the stastistics server (" + conn.getHeaderField("ROMUpdater-error") + ")."); return false; } catch (Exception e) { Log.e(TAG, "It was impossible to send data to the stastistics server."); Log.e(TAG, "Error: " + e.toString()); return false; } }
From source file:com.googlecode.gmaps4jsf.services.ReverseGeocoderServiceImpl.java
private static String readURL(URL url) throws Exception { URLConnection connection = url.openConnection(); StringBuilder builder = new StringBuilder(); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line;//from w ww.j ava 2 s .co m while ((line = reader.readLine()) != null) { builder.append(line); } return builder.toString(); }
From source file:Main.java
public static Bitmap tryToGetBitmapFromInternet(String bitmapUrl, Context context, int imageSize) { if (null != bitmapUrl) { InputStream inputStream = null; try {//from w w w. j av a 2 s. c o m URL url = new URL(bitmapUrl); URLConnection connection = url.openConnection(); connection.connect(); inputStream = connection.getInputStream(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int read; while ((read = inputStream.read(buffer)) != -1) { byteArrayOutputStream.write(buffer, 0, read); } inputStream.close(); byteArrayOutputStream.close(); buffer = byteArrayOutputStream.toByteArray(); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeByteArray(buffer, 0, buffer.length, options); int maxSize = Math.max(options.outWidth, options.outHeight); float newImageScale = 1f; if (-1 != imageSize) { newImageScale = maxSize / (imageSize * context.getResources().getDisplayMetrics().density); } options.inJustDecodeBounds = false; options.inSampleSize = Math.round(newImageScale); return BitmapFactory.decodeByteArray(buffer, 0, buffer.length, options); } catch (Throwable e) { // pass } finally { if (null != inputStream) { try { inputStream.close(); } catch (IOException e) { // pass } inputStream = null; } System.gc(); } } return null; }
From source file:com.microsoft.webapp.util.WebAppUtils.java
public static void sendGet(String sitePath) throws Exception { URL url = new URL(sitePath); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("GET"); con.setRequestProperty("User-Agent", "AzureToolkit for Eclipse"); con.getResponseCode();//w ww . j av a2s .c o m }
From source file:de.ifgi.mosia.wpswfs.Util.java
public static Set<String> readConfigFilePerLine(String resourcePath) throws IOException { URL resURL = Util.class.getResource(resourcePath); URLConnection resConn = resURL.openConnection(); resConn.setUseCaches(false);//from w w w . ja v a 2 s . c om InputStream contents = resConn.getInputStream(); Scanner sc = new Scanner(contents); Set<String> result = new HashSet<String>(); String line; while (sc.hasNext()) { line = sc.nextLine(); if (line != null && !line.isEmpty() && !line.startsWith("#")) { result.add(line.trim()); } } sc.close(); return result; }
From source file:Main.java
public static ArrayList autocomplete(String input) { ArrayList resultList = null;/*w w w. j a v a 2 s . c o m*/ HttpURLConnection conn = null; StringBuilder jsonResults = new StringBuilder(); try { StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON); sb.append("?key=" + API_KEY); //sb.append("&components=country:gr"); sb.append("&sensor=false"); sb.append("&input=" + URLEncoder.encode(input, "utf8")); URL url = new URL(sb.toString()); conn = (HttpURLConnection) url.openConnection(); InputStreamReader in = new InputStreamReader(conn.getInputStream()); // Load the results into a StringBuilder int read; char[] buff = new char[1024]; while ((read = in.read(buff)) != -1) { jsonResults.append(buff, 0, read); } } catch (MalformedURLException e) { //Log.e(LOG_TAG, "Error processing Places API URL", e); return resultList; } catch (IOException e) { // Log.e(LOG_TAG, "Error connecting to Places API", e); return resultList; } finally { if (conn != null) { conn.disconnect(); } } try { // Create a JSON object hierarchy from the results JSONObject jsonObj = new JSONObject(jsonResults.toString()); JSONArray predsJsonArray = jsonObj.getJSONArray("predictions"); // Extract the Place descriptions from the results resultList = new ArrayList(predsJsonArray.length()); for (int i = 0; i < predsJsonArray.length(); i++) { System.out.println(predsJsonArray.getJSONObject(i).getString("description")); System.out.println("============================================================"); resultList.add(predsJsonArray.getJSONObject(i).getString("description")); } } catch (JSONException e) { //Log.e(LOG_TAG, "Cannot process JSON results", e); } return resultList; }
From source file:software.uncharted.util.HTTPUtil.java
public static String get(String url) throws IOException { URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // optional default is GET con.setRequestMethod("GET"); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine;//from w w w .ja va 2 s . co m StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); return response.toString(); }