URLEncodedUtils deprecated in Android

2019-03-03 14:06发布

问题:

URLEncodedUtils is deprecated in Android API 22+. What could I use instead in this code?

I need to change URLEncode.Utils.Format() line.

public String construct() {
  return (iftrue? HOSTING_NAME : _NAME) + yDomain
         + (param.size() > 0 ? "?" 
         + URLEncodedUtils.format(yParam, "utf-8") : "");
}

回答1:

You can use java inbuilt method java.net.URLEncoder to construct url with param data. URLEncoder.encode(yParam, "UTF-8")

Change your method and check it.

`public String construct() {
    return (iftrue? HOSTING_NAME : _NAME) + yDomain
            + (param.size() > 0 ? "?" + URLEncoder.encode(yParam, "UTF-8"): "");}`