Get Video from URL : Url « Network « Android






Get Video from URL

 

//package androtv.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

class SiteClientUtil {

  private SiteClientUtil() {
  }

  public static String getSiteUrl() {
    return "http://json.protv.md/featured?ctr=_" + new Date().getTime();
  }

  public static String getNews() {
    HttpClient httpclient = new DefaultHttpClient();
    // TODO: format url wit current date
    String url = getSiteUrl();
    HttpGet httpget = new HttpGet(url);
    HttpResponse response;
    String result = null;

    try {
      response = httpclient.execute(httpget);

      // Get hold of the response entity
      HttpEntity entity = response.getEntity();
      // If the response does not enclose an entity, there is no need
      // to worry about connection release

      if (entity != null) {
        // A Simple Response Read
        InputStream instream = entity.getContent();
        result = convertStreamToString(instream);

        // Closing the input stream will trigger connection release
        instream.close();
      }
    } catch (ClientProtocolException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return result;

  }

  public static Bitmap getImage(String url) throws MalformedURLException,
      IOException {

    Bitmap bitmap = null;
    InputStream in = null;
    try {
      in = OpenHttpConnection(url);
      bitmap = BitmapFactory.decodeStream(in, null,
          new BitmapFactory.Options());
      in.close();
    } catch (IOException e1) {
    }
    return bitmap;
  }

  public static String getVideoUrl(String url) throws IOException {
    InputStream stream = OpenHttpConnection(url);
    String html = convertStreamToString(stream);
    int startIndex = html.indexOf("$('#video').flash(");

    if (startIndex == -1) {
      return null;
    }

    startIndex = html.indexOf("file:", startIndex);
    int endIndex = html.indexOf(",", startIndex);

    if (endIndex == -1) {
      return null;
    }

    return html.substring(startIndex + 5, endIndex);
  }

  private static InputStream OpenHttpConnection(String url)
      throws IOException {
    InputStream inputStream = null;
    URL _url = new URL(url);
    URLConnection conn = _url.openConnection();

    try {
      HttpURLConnection httpConn = (HttpURLConnection) conn;
      httpConn.setRequestMethod("GET");
      httpConn.connect();

      if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
        inputStream = httpConn.getInputStream();
      }
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return inputStream;
  }

  private static String convertStreamToString(InputStream is) {
    /*
     * To convert the InputStream to String we use the
     * BufferedReader.readLine() method. We iterate until the BufferedReader
     * return null which means there's no more data to read. Each line will
     * appended to a StringBuilder and returned as String.
     */
    BufferedReader reader = new BufferedReader(new InputStreamReader(is),
        8192);
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
      while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        is.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    return sb.toString();
  }

}

   
  








Related examples in the same category

1.Using Intent to open a URL
2.Process xml document from a Url
3.Suggest Url Provider
4.Showing android:autoLink property, which linkifies things like URLs and phone numbers found in the text.
5.extends ArrayAdapter to create URL history
6.Used to compress URL using the bit.ly service.
7.URL encode and decode
8.Is valid URL
9.Download from URLConnection
10.Request from an URL
11.Get Url From Img Tag
12.Returns the contents of the given URL as an array of strings
13.Read from a URL
14.Pull the raw text content of the given URL.
15.Gets data from URL
16.get Url Response
17.URL Encode Utils
18.Downloads a file given URL to specified destination
19.get Host From Url
20.encode Url
21.decode Url
22.Convert a byte array to a URL encoded string
23.get Url content with max retries
24.get Url By Post
25.Take a base url and a {@link Map} of parameters to build a valid url