URLDecoder

URLDecoder declares the following class method for decoding an encoded string:


String decode(String s, String enc)
 
import java.net.URLDecoder;
import java.net.URLEncoder;

public class Main {
  public static void main(String[] argv) throws Exception {
    String line = URLEncoder.encode("name1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");

    String[] pairs = line.split("\\&");
    for (int i = 0; i < pairs.length; i++) {
      String[] fields = pairs[i].split("=");
      String name = URLDecoder.decode(fields[0], "UTF-8");
      System.out.println(name);
      String value = URLDecoder.decode(fields[1], "UTF-8");
      System.out.println(value);
    }
  }
}
  

Encoding and decoding an encoded string


import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class Main {
  public static void main(String[] args) throws UnsupportedEncodingException {
    String encodedData = URLEncoder.encode("The string ", "UTF-8");
    System.out.println(encodedData);
    System.out.println(URLDecoder.decode(encodedData, "UTF-8"));
  }
}
Home 
  Java Book 
    Networking