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

Android解析XML-Plist遇阻:导入多库均编译失败求助

Android XML-Plist 解析编译失败?试试这些靠谱方案

Hey there! Let's work through your XML-Plist parsing issue on Android. Compile failures when importing plist libraries usually boil down to dependency conflicts, incorrect setup, or using outdated libraries. Here's a step-by-step fix that should get you up and running:

1. 选择稳定的Plist解析库

我最推荐dd-plist,它是维护活跃、适配Android的老牌库,很少出现编译兼容问题。

添加Gradle依赖

在你的模块级build.gradle(或build.gradle.kts)里添加:

dependencies {
    implementation 'com.googlecode.plist:dd-plist:1.20'
}

如果遇到Java版本相关的编译错误,确保你的build.gradle里配置了Java 8+:

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    // 如果你用Kotlin,还要加上这段
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

2. 解析目标Plist的代码示例

你提供的Plist是一个包含多个dict的array结构,这里给出完整的解析流程(包括网络获取文件):

第一步:添加网络权限

在AndroidManifest.xml里加上:

<uses-permission android:name="android.permission.INTERNET" />

第二步:解析代码(用OkHttp做网络请求,也可以用HttpURLConnection)

import com.googlecode.plist.NSArray;
import com.googlecode.plist.NSDictionary;
import com.googlecode.plist.PropertyListParser;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.io.InputStream;
import java.io.IOException;

// 注意:网络请求不能在主线程,用Coroutine、AsyncTask或者RxJava处理
public void parsePlistFromNetwork() {
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder()
            .url("http://www.ctbsolutions.com.my/rapidpg/eta.aspx?bid=B0377")
            .build();

    client.newCall(request).enqueue(new okhttp3.Callback() {
        @Override
        public void onFailure(okhttp3.Call call, IOException e) {
            e.printStackTrace();
        }

        @Override
        public void onResponse(okhttp3.Call call, Response response) throws IOException {
            if (response.isSuccessful() && response.body() != null) {
                InputStream inputStream = response.body().byteStream();
                try {
                    // 解析Plist根节点(是NSArray类型)
                    NSArray plistArray = (NSArray) PropertyListParser.parse(inputStream);
                    
                    // 遍历数组里的每个字典项
                    for (int i = 0; i < plistArray.count(); i++) {
                        NSDictionary dict = (NSDictionary) plistArray.objectAtIndex(i);
                        
                        // 提取你需要的字段
                        String busstop = dict.objectForKey("busstop").toString();
                        String busstopID = dict.objectForKey("busstopID").toString();
                        String busstopLat = dict.objectForKey("busstopLat").toString();
                        
                        // 这里可以把数据传到主线程更新UI
                        runOnUiThread(() -> {
                            // 处理数据,比如添加到列表展示
                        });
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    inputStream.close();
                }
            }
        }
    });
}

3. 常见编译失败的排查点

如果还是编译失败,检查这几个地方:

  • 依赖冲突:用./gradlew app:dependencies查看依赖树,看看是否有其他库也依赖了plist相关的包,排除冲突的版本。
  • ProGuard混淆:如果开启了混淆,在proguard-rules.pro里添加规则:
    -keep class com.googlecode.plist.** { *; }
    
  • 仓库配置:确保你的settings.gradle里包含了MavenCentral(dd-plist托管在MavenCentral上):
    dependencyResolutionManagement {
        repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
        repositories {
            google()
            mavenCentral()
        }
    }
    

4. 替代方案(如果dd-plist还是有问题)

如果dd-plist不兼容你的项目,试试Android-Plist-Parser,依赖是:

implementation 'com.github.snowdream.android:android-plist-parser:1.0.0'

它的API风格和dd-plist类似,你可以根据官方文档调整解析代码。


内容的提问来源于stack exchange,提问作者ImJustChew

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 08:55:27