0

I need convert String to UTF-8. Now in my code I have this:

sb.append(URLEncoder.encode(smsAnswerText, "UTF-8"))

but encode method throw exception UnsupportedEncodingException

I rewrite: sb.append(smsAnswerText) becouse write this

and I have wrong tуxt -unreadable characters

Then I tried new String(smsAnswerText.getBytes(),StandardCharsets.UTF_8) And this method throw exception too UnsupportedEncodingException

How can I convert Sting to String+UTF-8 without UnsupportedEncodingException?

I need:

public static String generateBodyResponse(String smsAnswerText){
    return// smsAnswerText in UTF-8
}

I have

public static String generateBodyResponse(String smsAnswerText) throws UnsupportedEncodingException{
        return URLEncoder.encode(smsAnswerText, "UTF-8");
    }
Community
  • 1
  • 1
user5620472
  • 2,722
  • 8
  • 44
  • 97
  • Do you mean it throws exception in runtime or that it is needed to wrap it into try...catch because of checked exception? – Andremoniy Feb 16 '17 at 10:49
  • I often use this method and do not want to throw this exception. perhaps there is a way to convert without release of exceptions – user5620472 Feb 16 '17 at 10:53
  • https://stackoverflow.com/questions/10402048/is-there-a-common-java-library-that-will-handle-url-encoding-decoding-for-a-coll – Josh Lee Feb 16 '17 at 14:35

3 Answers3

0

I don't think any provider is swallowing the exception.The alternative you can try is using

<dependency>                               
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>0.10.0</version>
<scope>compile</scope>
</dependency>

Here @SneakyThrows annotation is available.By Using this You can avoid throws and throw keywords

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
0

Why will not you do try cache block?

     public static String generateBodyResponse(String smsAnswerText) {
String defaultval= "some default value which will be returned if your encod is wrong.";
        try {
            defaultval = URLEncoder.encode(smsAnswerText, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return defaultval;

    }

or do some class like wrapper around this function:

public class URLEncoderDecorator {
public static String encode(String smsAnswerText) {
    try {
        return URLEncoder.encode(smsAnswerText, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return smsAnswerText;
}

}

and then:

public static String generateBodyResponse(String smsAnswerText) {
    return URLEncoderDecorator.encode(smsAnswerText, "UTF-8");
}

But both sutiations are not good becouse this exception is need to understand how it can be handled when Encoding is wrong. You must exactly understand what you must do when this situation is come, because exception is supressed.

Boris Zhguchev
  • 313
  • 4
  • 12
  • e.printStackTrace(); is bad practice. Yes I can catch ecxeption into my method and add to Log this ecxeption. But if I get error I return message in not utf-8. – user5620472 Feb 16 '17 at 11:31
  • e.printStackTrace() is not a practice. It is a template autogenerated by ide. :) . or logger or someting else. I do not understand your actions when you get wrong encoding. What will you do? if you know you can put it in catch block or put default value in return. Otherwise you must throw exception and handle it in other place, where your app understands. – Boris Zhguchev Feb 16 '17 at 12:23
0
import java.nio.charset.StandardCharsets;
import org.apache.commons.codec.net.URLCodec;

byte[] urlEscape(String s) {
    return new URLCodec().encode(s.getBytes(StandardCharsets.UTF_8));
}
Josh Lee
  • 171,072
  • 38
  • 269
  • 275