get Host From Url : Url « Network « Android

get Host From Url

//package com.retain;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.Date;

import android.content.Context;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

 * @author Nazmul Idris
 * @version 1.0
 * @since Jul 8, 2008, 2:35:39 PM
class AppUtils {

   * in the emulator points back to itself. Use this if you want to
   * access your host OS
  public static String EmulatorLocalhost = "";

  static public String getHostFromUrl(String url) {
    try {
      URI uri = new URI(url);
      String host = uri.getHost();
      if (host == null)
        return null;

      host = host.replaceAll("^www\\.", "");

      int i = host.lastIndexOf('.');
      if (i >= 0) {
        int j = host.lastIndexOf('.', i - 1);
        if (j >= 0)
          host = host.substring(j + 1, host.length());
      return host;
    } catch (URISyntaxException urie) {
      Log.e("getHostFromUrl", urie.getMessage());

    return url;


}// end class AppUtils


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 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.Get Video from URL
16.Gets data from URL
17.get Url Response
18.URL Encode Utils
19.Downloads a file given URL to specified destination
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