-3

I'm trying to create a link with URLEncoder but I obtain the UnsupportedEncodingException and I don't know why?

String param1="a";
String param2="b";
String key="key";
String h="h";

public static void main(String[]args){
    try {
         URLEncoder.encode(param1,key);
         URLEncoder.encode(param2, h);
    } catch (Exception e) {

        e.printStackTrace();
    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
Rubberino
  • 1
  • 1

1 Answers1

0

URLEncoder.encode takes two parameters.

1st: Value to be encoded.

2nd: Name of the encoding (e.g UTF-8)

So in your case, you would want to do something like this:

    static String param1 = "a";
    static String param2 = "b";

    public static void main(String[] args) {
        try {
            StringBuilder url = new StringBuilder();
            url.append("&key=");
            url.append(URLEncoder.encode(param1, "UTF-8"));
            url.append("&h=");
            url.append(URLEncoder.encode(param2, "UTF-8"));
            System.out.println(url);
        } catch (Exception e) {

            e.printStackTrace();
        }
    }
CodeMonkeyKing
  • 4,556
  • 1
  • 32
  • 34
Onur Aktaş
  • 410
  • 4
  • 8