18

Lvl library doesn't compile anymore on Android Marshmallow due to the lack apache stuff removed. You can add useLibrary 'org.apache.http.legacy but it's only a temporary workaround. The problem is this method:

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 and URLEncodedUtils are not found.

What is the new "way"? How can I replace this call with new code compliant with the new Android version?

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
greywolf82
  • 21,813
  • 18
  • 54
  • 108

4 Answers4

10

I wrote my own class looking at the original code as temporary workaround:

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;
    }
}
greywolf82
  • 21,813
  • 18
  • 54
  • 108
  • 2
    Whew! Thanks! This workaround worked for me. Although I'm not entirely certain why Google's QA didn't detect the LVL doesn't even _compile_ during Marshmallow's preview let alone post official launch. Fortunately, LVL is open source. Now to get the third-parties to catch up too. – DevByStarlight Oct 26 '15 at 23:09
  • Ugh! LVL works now ... But the Google Download library chokes up a hairball. Maybe I'll just stay in API 22 compatibility mode until Android N ... – DevByStarlight Oct 26 '15 at 23:29
  • @greywolf82 can you show a concrete example of how to use this class? – DiscDev Jan 05 '16 at 20:22
  • 1
    I think there are lot of issues in L and M, and google is busy with releasing new OS N... Didn't seems weird ? – Smeet Jul 05 '16 at 15:49
6

Methods named decodeExtras() show up in two places in the LVL, and their code differs. No need to come up with verbose custom code - android.net.Uri offers similar functionality, and the replacements below work for API level 11 (Honeycomb) and above.

For 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;
}

For 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;
}
Uli
  • 2,828
  • 20
  • 23
3

my quick and dirty solution was to replace the legacy code with

    Uri uri = Uri.parse("?" + extras);
    for (String itemName : uri.getQueryParameterNames())
        results.put(itemName, uri.getQueryParameter(itemName));
materemias
  • 131
  • 1
  • 2
  • 7
3

I found updated version at https://github.com/google/play-licensing, the issue is fixed with URIQueryDecoder

alders
  • 235
  • 1
  • 4