List of usage examples for java.net HttpURLConnection connect
public abstract void connect() throws IOException;
From source file:Main.java
/** * Makes HttpURLConnection and returns InputStream *///from w ww .j a v a2 s . c o m private static InputStream getHttpConnection(String urlString) throws IOException { InputStream stream = null; URL url = new URL(urlString); URLConnection connection = url.openConnection(); try { HttpURLConnection httpConnection = (HttpURLConnection) connection; httpConnection.setRequestMethod("GET"); httpConnection.connect(); if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { stream = httpConnection.getInputStream(); } } catch (Exception ex) { ex.printStackTrace(); } return stream; }
From source file:dev.meng.wikidata.util.http.HttpUtils.java
public static JSONObject queryForJSONResponse(URL url, String encoding) throws ProtocolException, IOException, StringConvertionException { JSONObject response = null;//from ww w.ja va 2s.c om HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.connect(); if (connection.getResponseCode() == 200) { response = new JSONObject(StringUtils.inputStreamToString(connection.getInputStream(), encoding)); } else { throw new IOException("Error in opening: " + url + ", " + connection.getResponseCode() + " " + connection.getResponseMessage()); } return response; }
From source file:Main.java
public static InputStream getHTTPInputStream(String url) { try {/* w ww .j a v a 2s.c om*/ URL urlObj = null; urlObj = new URL(url); HttpURLConnection urlConnection = null; urlConnection = (HttpURLConnection) urlObj.openConnection(); urlConnection.connect(); int response = urlConnection.getResponseCode(); if (response != 200) { return null; } return urlConnection.getInputStream(); } catch (IOException e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static byte[] downloadImage(final URL url) { InputStream in = null;//from w ww. ja v a2 s. com int responseCode = -1; try { final HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setDoInput(true); con.connect(); responseCode = con.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { in = con.getInputStream(); return getByteFromInputStream(in); } } catch (final IOException e) { Log.e(LOG_TAG, "Failed to download image from: " + url.toString(), e); } return null; }
From source file:Main.java
/** * @param url//from w w w . j av a 2 s.c om * @return */ public static Bitmap getBitMapFromURL(URL url) { Bitmap mBitmap = null; try { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.connect(); conn.getContentLength(); InputStream is = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); mBitmap = BitmapFactory.decodeStream(bis); bis.close(); is.close(); } catch (Exception e) { Log.e("Exception in MapScrollActivity.getBitmapFromURL", "" + e.getMessage()); e.printStackTrace(); } return mBitmap; }
From source file:Main.java
public static Bitmap getBitmapFromURL(String urlStr) { Bitmap bitmap = null;//from ww w .ja v a2 s . c o m try { URL url = new URL(urlStr); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.connect(); InputStream input = connection.getInputStream(); bitmap = BitmapFactory.decodeStream(input); } catch (IOException e) { Log.e(LOG_TAG, "cannot get the image from link :" + urlStr); } return bitmap; }
From source file:net.ftb.data.news.RSSReader.java
public static List<NewsArticle> readRSS() { try {/*from w w w . j av a2 s . c o m*/ List<NewsArticle> news = Lists.newArrayList(); DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); URL u = new URL(Locations.feedURL); HttpURLConnection conn = (HttpURLConnection) u.openConnection(); conn.connect(); if (conn.getResponseCode() != 200) { Logger.logWarn("News download failed"); AppUtils.debugConnection(conn); conn.disconnect(); return null; } Document doc = builder.parse(conn.getInputStream()); NodeList nodes = doc.getElementsByTagName("item"); for (int i = 0; i < nodes.getLength(); i++) { Element element = (Element) nodes.item(i); NewsArticle article = new NewsArticle(); article.setTitle(getTextValue(element, "title")); article.setHyperlink(getTextValue(element, "link")); article.setBody(getTextValue(element, "content:encoded")); article.setDate(getTextValue(element, "pubDate")); news.add(article); } return news; } catch (Exception ex) { Logger.logWarn("News download failed", ex); return null; } }
From source file:Main.java
/** * download and return bitmap for the given url * DO not call this method from main thread * @param src//from www. ja va 2 s . co m * @return */ public static Bitmap getBitmapFromURL(String imageUrl) { try { URL url = new URL(imageUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.connect(); InputStream input = connection.getInputStream(); Bitmap myBitmap = BitmapFactory.decodeStream(input); if (myBitmap != null) { myBitmap = getResizedBitmap(myBitmap, 200, 200); } return myBitmap; } catch (IOException e) { e.printStackTrace(); return null; } }
From source file:Main.java
@Nullable private static InputStream getStreamFromUri(Uri selectedImageURI, Context theContext) throws IOException { switch (selectedImageURI.getScheme()) { case "content": return theContext.getContentResolver().openInputStream(selectedImageURI); case "file": return new FileInputStream(new File(URI.create(selectedImageURI.toString()))); case "http": // Fall through case "https": final URL url = new URL(selectedImageURI.toString()); final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true);// w w w .ja v a 2 s . com connection.connect(); return connection.getInputStream(); default: Log.w(TAG, "getStreamFromUri(): unsupported Uri scheme: " + selectedImageURI.getScheme()); return null; } }
From source file:Main.java
public static final Bitmap fetchAndRescaleBitmap(String uri, int width, int height) throws IOException { URL url = new URL(uri); HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection(); httpConnection.setDoInput(true);//from w w w . j av a2 s. c o m httpConnection.connect(); InputStream inputStream = httpConnection.getInputStream(); int scaleFactor = findScaleFactor(width, height, inputStream); httpConnection = (HttpURLConnection) url.openConnection(); httpConnection.setDoInput(true); httpConnection.connect(); inputStream = httpConnection.getInputStream(); Bitmap bitmap = scaleBitmap(scaleFactor, inputStream); return bitmap; }