remove Parameter from query url - Android java.net

Android examples for java.net:URL

Description

remove Parameter from query url

Demo Code

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.StringTokenizer;

public class Main{

    public static String removeParameter(String url, List<String> excepts) {
        if (url == null) {
            return null;
        }/*from w ww .j  a v  a 2s . co  m*/
        StringBuffer sb = new StringBuffer();

        try {
            URL mUrl = new URL(url);
            sb.append(mUrl.getProtocol()).append("://")
                    .append(mUrl.getHost()).append(mUrl.getPath())
                    .append("?");
            String[] params = split(mUrl.getQuery(), "&");
            for (String p : params) {
                String[] arr = split(p, "=");
                if (!excepts.contains(arr[0])) {
                    sb.append("&").append(p);
                }
            }
        } catch (MalformedURLException e) {
            return null;
        }

        return sb.toString();
    }
    public static String[] split(String str, String separator) {
      if (str == null) {
          return null;
      }
      String[] result;
      int i = 0; 

      StringTokenizer st = new StringTokenizer(str, separator);
      result = new String[st.countTokens()];

      while (st.hasMoreTokens()) {
          result[i++] = st.nextToken();
      }

      return result;
  }
}

Related Tutorials