Back to project page streaming_project.
The source code is released under:
GNU General Public License
If you think the Android project streaming_project listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.example.streaming.streaming; // w ww . j av a 2 s.c om import android.content.Context; import android.net.Uri; import android.util.Log; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.Properties; // Handles the networking in the Streaming application // NOTE 1: networking permission is added to manifest // NOTE 2: getURL() cannot be directly called from an Activity. The networking code must be // run from a background thread (created by AsyncTask) because Android has disallowed network // operations on the main thread starting with Honeycomb public class DataFetcher { // For debugging public static final String TAG = "Fetcher"; private String mUriString; private Context mContext; private Properties mProperties; public DataFetcher(Context context) { mContext = context; AssetsPropertyReader propertiesReader = new AssetsPropertyReader(mContext); mProperties = propertiesReader.getProperties("app.properties"); mUriString = mProperties.getProperty("server_url"); } // Fetches raw data from a URL and returns it as an array of bytes byte[] getUrlBytes(String urlSpec) throws IOException { // Create a URL object from a string (e.g.. http://www.google.com) URL url = new URL(urlSpec); // openConnection() creates a connection object pointed at the URL // NOTE 1: Cast the the URLConnection object (as returned by openConnection()) to // HttpsURLConnection since we are connection to an https URL // This gives us HTTP-specific interfaces for working with request methods, response codes, // streaming methods, and more // NOTE 2: HttpsURLConnection represents a connection, but it will not actually connect to // the endpoint until getInputStream() is called (or getOutputStream() for POST calls). // Until then, you cannot get a valid response code. HttpURLConnection connection = (HttpURLConnection)url.openConnection(); // TODO: add this timeout in the properties file connection.setConnectTimeout(10000); // Once the URL object is created and the connection is opened, we can read the incoming data try { ByteArrayOutputStream out = new ByteArrayOutputStream(); // InputStream returns bytes as they are available InputStream in = connection.getInputStream(); if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { String message = "Response code is " + connection.getResponseCode(); Log.e(TAG, message); throw new IOException(message); } int bytesRead; byte[] buffer = new byte[1024]; while ((bytesRead = in.read(buffer)) > 0) { out.write(buffer, 0, bytesRead); } out.close(); return out.toByteArray(); } catch (IOException e) { Log.e(TAG, e.getMessage()); throw new IOException(e.getMessage()); } finally { connection.disconnect(); } } // Converts the bytes fetched by getUrlBytes() into a String public String getUrl(String urlSpec) throws IOException { byte[] result = getUrlBytes(urlSpec); return new String(result); } // Builds an appropriate API request URL and fetches its contents as a String public String fetchData(String query) throws IOException { String str; // Build the complete URL for the getListFiles request // NOTE 1: Uri.Builder is a convenience class for creating properly escaped parameterized // URLs. // NOTE 2: appendQueryParameter() automatically escapes query strings String url = Uri.parse(mUriString).buildUpon() .appendPath(query) .build().toString(); str = getUrl(url); Log.i(TAG, "Received data"); return str; } }