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

Android项目中org.json.JSONTokener无法识别Reader类型构造器的问题咨询

Hey there, let's break down what's happening here and work through the fixes!

The Root Cause

This isn't a problem with the org.json:json:20210307 version you added—Android ships with its own built-in org.json implementation, and it takes priority over your third-party dependency.

Android's native org.json library has existed for years, but its JSONTokener only includes the constructor that accepts a String parameter. The JSONTokener(Reader) overload you're trying to use does exist in the 20210307 standalone org.json library, but your code is actually calling the system's older, limited version instead of the one you added to your build.gradle.

Fix Options

Here are a few solid ways to resolve this:

1. Adapt to the system's built-in implementation

Since the system's JSONTokener only works with strings, convert your Reader to a string first:

Reader reader = new InputStreamReader(responseBody.byteStream());
// Convert Reader content to a single String
String jsonContent = new BufferedReader(reader)
        .lines()
        .collect(Collectors.joining(System.lineSeparator()));
org.json.JSONTokener jst = new org.json.JSONTokener(jsonContent);

⚠️ Heads up: This loads the entire JSON payload into memory at once, which isn't ideal for very large files.

2. Force your app to use the third-party org.json library

You can configure your Gradle build to prioritize the standalone org.json library over Android's native one. Add this to your module-level build.gradle file:

android {
    packagingOptions {
        pickFirst 'org/json/**'
    }

    configurations.all {
        resolutionStrategy {
            force 'org.json:json:20210307'
        }
    }
}

⚠️ Note: Be cautious with this approach—some system APIs might rely on Android's native org.json implementation, so forcing a replacement could introduce unexpected compatibility issues.

If you don't have a hard requirement to use org.json, consider switching to a more Android-friendly JSON parsing library like Gson, Moshi, or Jackson. These libraries avoid conflicts with Android's native code and offer better APIs for streaming parsing:
Example with Gson:

Reader reader = new InputStreamReader(responseBody.byteStream());
Gson gson = new Gson();
YourDataModel dataModel = gson.fromJson(reader, YourDataModel.class);
How to Verify Which Library You're Using

To confirm whether you're using Android's native org.json or your third-party one, add this debug line to your code:

Log.d("JSONTokenerCheck", org.json.JSONTokener.class.getClassLoader().toString());
  • If the output is something like dalvik.system.PathClassLoader, you're using the system's built-in version.
  • If it shows a URLClassLoader or similar, you're successfully using the third-party library you added.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 13:29:07