Get Http Stream : Http Connection « Network « Android






Get Http Stream

    
//package com.dedrake.kswine;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

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

class Utilities {

  public static InputStream getHttpStream(String urlString) throws IOException {
    InputStream in = null;
    int response = -1;
               
    URL url = new URL(urlString); 
    URLConnection conn = url.openConnection();
                 
    if (!(conn instanceof HttpURLConnection)) { throw new IOException("Not an HTTP connection"); }
        
  try {
    HttpURLConnection hcn = (HttpURLConnection) conn;
    hcn.setAllowUserInteraction(false);
    hcn.setInstanceFollowRedirects(true);
    hcn.setRequestMethod("GET");
    hcn.connect();

    response = hcn.getResponseCode();
    if (response == HttpURLConnection.HTTP_OK) { in = hcn.getInputStream(); }
  }
    catch (Exception ex) {
      throw new IOException("Error connecting");            
    }
    
    return in;     
  }
  
  public static Bitmap getUrlImage(String urlString) {        
    Bitmap bmp = null;
    InputStream in = null;
    
    try {
      in = getHttpStream(urlString);
      bmp = BitmapFactory.decodeStream(in);
      in.close();
    } 
    catch (IOException e) {

    }
    
    return bmp;                
  }
  
  public static String getUrlText(String urlString) {
    InputStream in = null;
      
    try {
      in = getHttpStream(urlString);
    } 
    catch (IOException e) {
      return "";
    }
      
    InputStreamReader isr = new InputStreamReader(in);
    int charRead;
    StringBuilder sb = new StringBuilder();
    char[] ib = new char[2000];          
    
    try {
      while ((charRead = isr.read(ib)) > 0) {                    
        String readString = String.copyValueOf(ib, 0, charRead);                    
        sb.append(readString);
        ib = new char[2000];
      }
      in.close();
    } 
    catch (IOException e) {
      return "";
    }    
    return sb.toString();        
  }
}

   
    
    
    
  








Related examples in the same category

1.Http Connection
2.Using HttpGet to get the web response
3.Create Http connection
4.Http connection with
5.HttpGet and DefaultHttpClient
6.Http Post
7.Simple HTTP Request
8.Http Request Class
9.Http Get and Http Post
10.Get Text from HttpResponse
11.Http Request
12.Http Downloader
13.Is Http Url Available
14.Http Retriever
15.Receive Response from HttpURLConnection
16.Print http headers. Useful for debugging.
17.Return the base URL from the given URL. Example: http://foo.org/abc.html -> http://foo.org/
18.Send message with HttpPost
19.Generic Object Http Loader
20.Http Get and DefaultHttpClient
21.Gets http output from URL
22.Util for Http Get
23.do Http Get Request and return the status code
24.Http Get
25.implements HttpClient
26.Get File From HTTP
27.Make all redirects to go to http in stead of https
28.New Http Client Manager
29.Http Client Manager
30.Multipart Post
31.Get Server Data
32.Yahoo News Crawler
33.Send Packet
34.Read a web page
35.parse Request Header
36.Data Send Utils
37.This class is in charge of synchronizing the events (with due dates) with Google Calendar
38.Update Favicon
39.Converts key-value pair to appropriate signature for Facebook.