18

由于删除了缺少 apache 的东西,Lvl 库不再在 Android Marshmallow 上编译。您可以添加useLibrary 'org.apache.http.legacy,但这只是一种临时解决方法。问题是这种方法:

private Map<String, String> decodeExtras(String extras) {
  Map<String, String> results = new HashMap<String, String>();
  try {
     URI rawExtras = new URI("?" + extras);
     List<NameValuePair> extraList = URLEncodedUtils.parse(rawExtras, "UTF-8");
     for (NameValuePair item : extraList) {
        results.put(item.getName(), item.getValue());
     }

  } catch (URISyntaxException ignored) {

  }
  return results;
}

NameValuePair并且URLEncodedUtils没有找到。

什么是新的"way"?如何用与新 Android 版本兼容的新代码替换此调用?

4

4 回答 4

10

我编写了自己的类,将原始代码视为临时解决方法:

public class URLUtils {
    private static final String PARAMETER_SEPARATOR = "&";
    private static final String NAME_VALUE_SEPARATOR = "=";
    private static final String DEFAULT_CONTENT_CHARSET = "ISO-8859-1";

    public static List<Item> parse(final URI uri, final String encoding) {
        List<Item> result = Collections.emptyList();
        final String query = uri.getRawQuery();
        if (query != null && query.length() > 0) {
            result = new ArrayList<>();
            parse(result, new Scanner(query), encoding);
        }
        return result;
    }

    public static void parse(final List<Item> parameters, final Scanner scanner, final String encoding) {
        scanner.useDelimiter(PARAMETER_SEPARATOR);
        while (scanner.hasNext()) {
            final String[] nameValue = scanner.next().split(NAME_VALUE_SEPARATOR);
            if (nameValue.length == 0 || nameValue.length > 2)
                throw new IllegalArgumentException("bad parameter");

            final String name = decode(nameValue[0], encoding);
            String value = null;
            if (nameValue.length == 2)
                value = decode(nameValue[1], encoding);
            parameters.add(new Item(name, value));
        }
    }


    private static String decode (final String content, final String encoding) {
        try {
            return URLDecoder.decode(content, encoding != null ? encoding : DEFAULT_CONTENT_CHARSET);
        } catch (UnsupportedEncodingException problem) {
            throw new IllegalArgumentException(problem);
        }
    }
}

public class Item {
    private String name;
    private String value;

    public Item(String n, String v) {
        name = n;
        value = v;
    }

    public String getName() {
        return name;
    }

    public String getValue() {
        return value;
    }
}
于 2015-08-23T08:17:43.337 回答
6

名为 decodeExtras() 的方法出现在 LVL 中的两个位置,并且它们的代码不同。无需提供冗长的自定义代码 - android.net.Uri提供了类似的功能,并且下面的替换适用于 API 级别 11 (Honeycomb) 及更高级别。

对于 APKExpansionPolicy:

private Map<String, String> decodeExtras(String extras) {
    Map<String, String> results = new HashMap<>();
    Uri uri = new Uri.Builder().encodedQuery(extras).build();
    Set<String> parameterNames = uri.getQueryParameterNames();
    for (String parameterName : parameterNames) {
        List<String> values = uri.getQueryParameters(parameterName);
        int count = values.size();
        if (count >= 1) {
            results.put(parameterName, values.get(0));
            for (int i = 1; i < count; ++i) {
                results.put(parameterName + i, values.get(i));
            }
        }
    }
    return results;
}

对于 ServerManagedPolicy:

private Map<String, String> decodeExtras(String extras) {
    Map<String, String> results = new HashMap<>();
    Uri uri = new Uri.Builder().encodedQuery(extras).build();
    Set<String> parameterNames = uri.getQueryParameterNames();
    for (String parameterName : parameterNames) {
        results.put(parameterName, uri.getQueryParameter(parameterName));
    }
    return results;
}
于 2016-06-23T05:00:10.910 回答
3

我在https://github.com/google/play-licensing找到了更新版本,该问题已通过URIQueryDecoder 修复

于 2017-03-09T09:54:26.703 回答
3

我快速而肮脏的解决方案是用

    Uri uri = Uri.parse("?" + extras);
    for (String itemName : uri.getQueryParameterNames())
        results.put(itemName, uri.getQueryParameter(itemName));
于 2015-09-09T15:02:24.977 回答