You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android:使用PolyUtil解码编码LatLngList时触发StringIndexOutOfBoundsException

Hey there! Let's troubleshoot that annoying StringIndexOutOfBoundsException you're getting when decoding some of your polyline routes from the database. I've run into similar issues before, so here are the most likely causes and fixes to try out:

1. First, Capture the Problematic Encoded Strings

Before diving into fixes, you need to know exactly which encoded strings are breaking things. Wrap your decoding code in a try-catch block and log the faulty string along with the exception. This will let you inspect the raw data that's causing the crash:

for (int i = 0; i < encodedLatLngList.length; i++) {
    String encoded = encodedLatLngList[i];
    if (encoded == null || encoded.trim().isEmpty()) {
        Log.w("PolyUtil", "Skipping empty/null encoded string at index " + i);
        continue;
    }
    try {
        List<LatLng> decodedLatLngList = PolyUtil.decode(encoded);
        // Proceed with your logic
    } catch (StringIndexOutOfBoundsException e) {
        Log.e("PolyUtilDecodeError", "Failed to decode string: " + encoded, e);
        // Optionally, save this string to a separate log table for deeper analysis
    }
}

Once you have the problematic strings, you can test them manually (e.g., writing a quick test snippet in your code) to confirm if the string itself is corrupted.

2. Check for Data Corruption During Storage/Upload

Most of the time, this exception happens because the encoded polyline string got messed up somewhere between encoding and decoding:

  • Database Field Limitations: If you're using a VARCHAR field instead of TEXT in your database, the encoded string might be truncated if it's too long. Switch to a TEXT type (or LONGTEXT for extra-long routes) to avoid truncation.
  • Special Character Handling: Polyline encoded strings include characters like +, /, and = which can be mangled if your upload process does automatic URL encoding/decoding incorrectly. Make sure you're not modifying the string during upload—if you do need to URL-encode it for transport, be sure to decode it fully before passing it to PolyUtil.decode().
  • Accidental Whitespace: Sometimes extra spaces, newlines, or tabs get appended to the string during storage. Add a .trim() call before decoding to clean this up:
    String encoded = encodedLatLngList[i].trim();
    

3. Update the Google Maps Utils Library

Older versions of the android-maps-utils library (which includes PolyUtil) had bugs that could cause decoding crashes for certain edge cases. Make sure you're using the latest version in your build.gradle file:

implementation 'com.google.maps.android:android-maps-utils:2.2.5' // Check for newer versions!

4. Validate Encoded Strings Before Decoding

Add a quick validation step to ensure the string is formatted correctly for polyline decoding. Valid polyline characters are between ASCII 63 (?) and 126 (~), so you can filter out any invalid strings:

private boolean isValidPolyline(String encoded) {
    if (encoded == null) return false;
    for (char c : encoded.toCharArray()) {
        if (c < 63 || c > 126) {
            return false;
        }
    }
    return true;
}

Use this before decoding:

if (!isValidPolyline(encoded)) {
    Log.w("PolyUtil", "Invalid polyline string: " + encoded);
    continue;
}

5. Add Integrity Checks During Encoding

To prevent corrupted data from being stored in the first place, generate a hash (like MD5 or SHA-256) of the encoded string and store it alongside the polyline. When retrieving, compare the hash of the stored string to the saved hash to confirm it hasn't been tampered with or truncated.

Give these steps a try—chances are the issue is either a truncated string or corrupted data during transport. Let me know if you find out what was causing it!

内容的提问来源于stack exchange,提问作者Kaan Apaydın

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:46:04