List of usage examples for java.net HttpURLConnection connect
public abstract void connect() throws IOException;
From source file:edu.xjtu.qxcamerabridge.LiveviewImageExtractor.java
private static InputStream getLiveviewInputStream(String liveviewURL) throws MalformedURLException, IOException { URL url = new URL(liveviewURL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setReadTimeout(2000);//from w ww .ja va2s. c om connection.connect(); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { return connection.getInputStream(); } return null; }
From source file:com.android.feedmeandroid.HTTPClient.java
public static Bitmap downloadFile(final String fileUrl) { final Bitmap[] bitmap = new Bitmap[1]; Thread t = new Thread(new Runnable() { public void run() { URL myFileUrl = null; try { myFileUrl = new URL(fileUrl); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace();// w ww .ja va2s . co m } try { HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection(); conn.setDoInput(true); conn.connect(); InputStream is = conn.getInputStream(); Bitmap bmImg = BitmapFactory.decodeStream(is); bitmap[0] = bmImg; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); t.start(); try { t.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return bitmap[0]; }
From source file:Main.java
public static boolean isConnectionAvailable() { class NetworkCheckTask extends AsyncTask<Void, Void, Boolean> { @Override//w w w . j av a 2s . c o m protected Boolean doInBackground(Void... params) { try { URL url = new URL(MAD_SERVER + "/sync.html"); HttpURLConnection conn = null; conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(999); conn.setRequestMethod("GET"); conn.setDoInput(true); conn.connect(); if (conn.getResponseCode() == 200) return true; else return false; } catch (Exception e) { return false; } } } try { NetworkCheckTask async = new NetworkCheckTask(); async.execute(); return async.get(); } catch (Exception e) { return false; } }
From source file:com.mopaas_mobile.http.BaseHttpRequester.java
public static String doGET(String urlstr, List<BasicNameValuePair> params) throws IOException { String result = null;/*from ww w. j a v a 2s .c o m*/ String content = ""; for (int i = 0; i < params.size(); i++) { content = content + "&" + URLEncoder.encode(((NameValuePair) params.get(i)).getName(), "UTF-8") + "=" + URLEncoder.encode(((NameValuePair) params.get(i)).getValue(), "UTF-8"); } URL url = new URL(urlstr + "?" + content.substring(1)); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.setRequestMethod("GET"); connection.setUseCaches(false); connection.connect(); InputStream is = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8")); StringBuffer b = new StringBuffer(); int ch; while ((ch = br.read()) != -1) { b.append((char) ch); } result = b.toString().trim(); connection.disconnect(); return result; }
From source file:Main.java
public static String jsonGetRequest(String urlQueryString) { String json = null;//from w w w . j a v a 2s . c o m try { URL url = new URL(urlQueryString); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setInstanceFollowRedirects(false); connection.setRequestMethod("GET"); connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("charset", "utf-8"); connection.connect(); InputStream inStream = connection.getInputStream(); json = streamToString(inStream); // input stream to string } catch (IOException ex) { ex.printStackTrace(); } return json; }
From source file:com.quackware.handsfreemusic.Utility.java
public static String getSourceCode(URL url) { Object content = null;//from w w w. j a va 2s.c o m try { HttpURLConnection uc = (HttpURLConnection) url.openConnection(); uc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.4) Gecko/20100513 Firefox/3.6.4"); uc.connect(); InputStream stream = uc.getInputStream(); if (stream != null) { content = readStream(uc.getContentLength(), stream); } else if ((content = uc.getContent()) != null && content instanceof java.io.InputStream) content = readStream(uc.getContentLength(), (java.io.InputStream) content); uc.disconnect(); } catch (Exception ex) { return null; } if (content != null && content instanceof String) { String html = (String) content; return html; } else { return null; } }
From source file:jfix.util.Urls.java
/** * Returns true if given url can be connected via HTTP within given timeout * (specified in seconds). Otherwise the url might be broken. *///from www . ja va 2 s . c om public static boolean isConnectable(String url, int timeout) { try { URLConnection connection = new URL(url).openConnection(); if (connection instanceof HttpURLConnection) { HttpURLConnection httpConnection = (HttpURLConnection) connection; httpConnection.setConnectTimeout(timeout * 1000); httpConnection.setReadTimeout(timeout * 1000); httpConnection.connect(); int response = httpConnection.getResponseCode(); httpConnection.disconnect(); return response == HttpURLConnection.HTTP_OK; } } catch (Exception e) { return false; } return false; }
From source file:jfix.util.Urls.java
/** * Returns the status code for connecting given url with given timeout. * Returns 0 if an IOException occurs./*from w w w . j a v a 2s .co m*/ */ public static int getStatus(String url, int timeout) { try { URLConnection connection = new URL(url).openConnection(); if (connection instanceof HttpURLConnection) { HttpURLConnection httpConnection = (HttpURLConnection) connection; httpConnection.setConnectTimeout(timeout * 1000); httpConnection.setReadTimeout(timeout * 1000); httpConnection.connect(); int response = httpConnection.getResponseCode(); httpConnection.disconnect(); return response; } } catch (IOException e) { // pass } return 0; }
From source file:com.teleca.jamendo.util.download.DownloadTask.java
public static Boolean downloadFile(DownloadJob job) throws IOException { // TODO rewrite to apache client PlaylistEntry mPlaylistEntry = job.getPlaylistEntry(); String mDestination = job.getDestination(); URL u = new URL(mPlaylistEntry.getTrack().getStream()); HttpURLConnection c = (HttpURLConnection) u.openConnection(); c.setRequestMethod("GET"); c.setDoOutput(true);/*from ww w .j a v a2 s .c om*/ c.connect(); job.setTotalSize(c.getContentLength()); Log.i(JamendoApplication.TAG, "creating file"); String path = DownloadHelper.getAbsolutePath(mPlaylistEntry, mDestination); String fileName = DownloadHelper.getFileName(mPlaylistEntry, job.getFormat()); try { // Create multiple directory boolean success = (new File(path)).mkdirs(); if (success) { Log.i(JamendoApplication.TAG, "Directory: " + path + " created"); } } catch (Exception e) {//Catch exception if any Log.e(JamendoApplication.TAG, "Error creating folder", e); return false; } FileOutputStream f = new FileOutputStream(new File(path, fileName)); InputStream in = c.getInputStream(); if (in == null) { // When InputStream is a NULL return false; } byte[] buffer = new byte[1024]; int lenght = 0; while ((lenght = in.read(buffer)) > 0) { f.write(buffer, 0, lenght); job.setDownloadedSize(job.getDownloadedSize() + lenght); } f.close(); downloadCover(job); return true; }
From source file:Main.java
private static void downloadResource(String from, String to) { OutputStream outputStream = null; BufferedInputStream inputStream = null; HttpURLConnection connection = null; URL url;/*w w w . ja va 2 s. c om*/ byte[] buffer = new byte[1024]; try { url = new URL(from); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setDoOutput(true); connection.connect(); outputStream = new FileOutputStream(to); inputStream = new BufferedInputStream(url.openStream()); int read; while ((read = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, read); } } catch (IOException e) { e.printStackTrace(); } finally { if (outputStream != null) try { outputStream.close(); } catch (IOException e) { } if (inputStream != null) try { inputStream.close(); } catch (IOException e) { } if (connection != null) connection.disconnect(); } }