List of usage examples for java.net URLConnection getInputStream
public InputStream getInputStream() throws IOException
From source file:main.java.gov.wa.wsdot.candidate.evaluation.App.java
/** * Reads in WSDOT highway camera data from Traveler Information API URL * using the StringBuilder Class./* w ww . jav a 2 s . com*/ * * @return JSON string read from Traveler Information API. * @throws Exception */ private String getAllCameras() throws Exception { StringBuilder sb = new StringBuilder(); URL url = new URL(URL); URLConnection connection = url.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); int cp; while ((cp = in.read()) != -1) { sb.append((char) cp); } in.close(); return sb.toString(); }
From source file:com.juick.android.Utils.java
public static BINResponse getBinary(Context context, final String url, final Notification progressNotification, int timeout) { try {/*from w w w . j a v a2s .c om*/ if (url == null) return new BINResponse("NULL url", false, null); URLConnection urlConnection = new URL(url).openConnection(); InputStream inputStream = urlConnection.getInputStream(); BINResponse binResponse = streamToByteArray(inputStream, progressNotification); inputStream.close(); String location = urlConnection.getHeaderField("Location"); if (location != null) { return getBinary(context, location, progressNotification, timeout); } return binResponse; } catch (Exception e) { if (progressNotification instanceof DownloadErrorNotification) { ((DownloadErrorNotification) progressNotification) .notifyDownloadError("HTTP connect: " + e.toString()); } Log.e("getBinary", e.toString()); return new BINResponse(e.toString(), true, null); } }
From source file:me.neatmonster.spacertk.plugins.PluginsRequester.java
@Override @SuppressWarnings("unchecked") public void run() { try {/*from ww w. j a va2 s . c o m*/ final URLConnection connection = new URL("http://api.bukget.org/api2/bukkit/plugins").openConnection(); final BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(connection.getInputStream())); final StringBuffer stringBuffer = new StringBuffer(); String line; while ((line = bufferedReader.readLine()) != null) stringBuffer.append(line); bufferedReader.close(); List<JSONObject> apiResponse = (JSONArray) JSONValue.parse(stringBuffer.toString()); for (JSONObject o : apiResponse) PluginsManager.pluginsNames.add((String) o.get("name")); } catch (Exception e) { System.out.println("[SpaceBukkit] Unable to connect to BukGet, BukGet features will be disabled"); } }
From source file:net.meltdowntech.steamstats.SteamUser.java
public void fetchLevel() { try {// w w w .j a v a 2 s . c o m String url = String.format("http://steamcommunity.com/profiles/%s/badges", getCommunityId() + ""); URL steam = new URL(url); URLConnection yc = steam.openConnection(); Reader reader = new InputStreamReader(yc.getInputStream()); HTMLEditorKit.Parser parser = new ParserDelegator(); parser.parse(reader, new HTMLEditorKit.ParserCallback() { private boolean foundIt; @Override public void handleText(char[] data, int pos) { if (foundIt) values.put("level", Integer.parseInt(new String(data))); } @Override public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) { if (t == HTML.Tag.SPAN) { Object o = a.getAttribute(HTML.Attribute.CLASS); if (o != null && o.equals("friendPlayerLevelNum")) foundIt = true; } } @Override public void handleEndTag(HTML.Tag t, int pos) { if (t == HTML.Tag.SPAN) foundIt = false; } }, true); } catch (IOException ex) { Util.printError("Invalid data"); } }
From source file:logic.ZybezQuery.java
private String getJsonString() throws MalformedURLException, IOException { URL url = new URL(apiURL + itemRequested); URLConnection urlConnection = url.openConnection(); urlConnection.addRequestProperty("User-Agent", "Mozilla/5.0"); BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); //Put JSON data into String message String message = org.apache.commons.io.IOUtils.toString(in); return message; }
From source file:com.phonegap.plugins.videoplayer.VideoPlayer.java
private void playVideo(String url) throws IOException { if (url.contains("bit.ly/") || url.contains("goo.gl/") || url.contains("tinyurl.com/") || url.contains("youtu.be/")) { //support for google / bitly / tinyurl / youtube shortens URLConnection con = new URL(url).openConnection(); con.connect();/*from w w w .j ava 2s. co m*/ InputStream is = con.getInputStream(); //new redirected url url = con.getURL().toString(); is.close(); } // Create URI Uri uri = Uri.parse(url); Intent intent = null; // Check to see if someone is trying to play a YouTube page. if (url.contains(YOU_TUBE)) { // If we don't do it this way you don't have the option for youtube uri = Uri.parse("vnd.youtube:" + uri.getQueryParameter("v")); if (isYouTubeInstalled()) { intent = new Intent(Intent.ACTION_VIEW, uri); } else { intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("market://details?id=com.google.android.youtube")); } } else if (url.contains(ASSETS)) { // get file path in assets folder String filepath = url.replace(ASSETS, ""); // get actual filename from path as command to write to internal storage doesn't like folders String filename = filepath.substring(filepath.lastIndexOf("/") + 1, filepath.length()); // Don't copy the file if it already exists File fp = new File(this.cordova.getActivity().getFilesDir() + "/" + filename); if (!fp.exists()) { this.copy(filepath, filename); } // change uri to be to the new file in internal storage uri = Uri.parse("file://" + this.cordova.getActivity().getFilesDir() + "/" + filename); // Display video player intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "video/*"); } else { // Display video player intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "video/*"); } this.cordova.getActivity().startActivity(intent); }
From source file:com.example.android.dragonTV.data.VideoProvider.java
protected JSONObject parseUrl(String urlString) { Log.d(TAG, "Parse URL: " + urlString); InputStream is = null;/*from w w w. ja v a 2s . c om*/ sThumbPrefixUrl = sContext.getResources().getString(R.string.thumb_prefix_url); try { java.net.URL url = new java.net.URL(urlString); URLConnection urlConnection = url.openConnection(); is = new BufferedInputStream(urlConnection.getInputStream()); BufferedReader reader = new BufferedReader( new InputStreamReader(urlConnection.getInputStream(), "UTF-8"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line); } String json = sb.toString(); return new JSONObject(json); } catch (Exception e) { Log.d(TAG, "Failed to parse the json for media list", e); return null; } finally { if (null != is) { try { is.close(); } catch (IOException e) { Log.d(TAG, "JSON feed closed", e); } } } }
From source file:MainClass.java
private byte[] loadClassData(String name) throws ClassNotFoundException { byte[] buffer; InputStream theClassInputStream = null; int bufferLength = 128; try {/*from w w w. j a v a2 s . c om*/ URL classURL = new URL(url, name + ".class"); URLConnection uc = classURL.openConnection(); uc.setAllowUserInteraction(false); try { theClassInputStream = uc.getInputStream(); } catch (NullPointerException e) { System.err.println(e); throw new ClassNotFoundException(name + " input stream problem"); } int contentLength = uc.getContentLength(); // A lot of web servers don't send content-lengths // for .class files if (contentLength == -1) { buffer = new byte[bufferLength * 16]; } else { buffer = new byte[contentLength]; } int bytesRead = 0; int offset = 0; while (bytesRead >= 0) { bytesRead = theClassInputStream.read(buffer, offset, bufferLength); if (bytesRead == -1) break; offset += bytesRead; if (contentLength == -1 && offset == buffer.length) { // grow the array byte temp[] = new byte[offset * 2]; System.arraycopy(buffer, 0, temp, 0, offset); buffer = temp; } else if (offset > buffer.length) { throw new ClassNotFoundException(name + " error reading data into the array"); } } if (offset < buffer.length) { // shrink the array byte temp[] = new byte[offset]; System.arraycopy(buffer, 0, temp, 0, offset); buffer = temp; } // Make sure all the bytes were received if (contentLength != -1 && offset != contentLength) { throw new ClassNotFoundException("Only " + offset + " bytes received for " + name + "\n Expected " + contentLength + " bytes"); } } catch (Exception e) { throw new ClassNotFoundException(name + " " + e); } finally { try { if (theClassInputStream != null) theClassInputStream.close(); } catch (IOException e) { } } return buffer; }
From source file:com.android.tools.lint.checks.GradleDetector.java
@Nullable private static String readUrlData(@NonNull LintClient client, @NonNull GradleCoordinate dependency, @NonNull String query) {// w w w . ja v a 2 s . c om // For unit testing: avoid network as well as unexpected new versions if (sMockData != null) { String value = sMockData.get(query); assert value != null : query; return value; } try { URL url = new URL(query); URLConnection connection = client.openConnection(url); if (connection == null) { return null; } try { InputStream is = connection.getInputStream(); if (is == null) { return null; } BufferedReader reader = new BufferedReader(new InputStreamReader(is, UTF_8)); try { StringBuilder sb = new StringBuilder(500); String line; while ((line = reader.readLine()) != null) { sb.append(line); sb.append('\n'); } return sb.toString(); } finally { reader.close(); } } finally { client.closeConnection(connection); } } catch (IOException ioe) { client.log(ioe, "Could not connect to maven central to look up the " + "latest available version for %1$s", dependency); return null; } }