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

首次打开Android Studio现有项目遇JSON格式错误求助

Fixing the ERROR: Use JsonReader.setLenient(true) to accept malformed JSON Issue in Android Studio 3.3.1

Hey there, sorry to hear you’ve been stuck on this frustrating error for a week! Let’s walk through the most likely causes and fixes tailored to your project setup.

First: Rule Out Gradle Sync Issues

Since this pops up when you first open the project, the problem might be tied to Gradle’s sync process rather than your app code. Here’s what to try first:

  • Clear Gradle Cache: Gradle caches metadata from remote repos, and sometimes that cache gets corrupted. Head to your local Gradle cache folder (usually ~/.gradle/caches on macOS/Linux or C:\Users\<YourName>\.gradle\caches on Windows), delete all its contents, then go back to Android Studio and click File > Sync Project with Gradle Files.
  • Simplify Repository Config: Your root build.gradle has both google() and maven { url "https://maven.google.com" }—these are the same repository, so the duplicate entry could cause conflicts. Remove the maven { url "https://maven.google.com" } line and re-sync.

If Gradle Sync Looks Good: Check Your JSON Parsing Code

Your project uses Gson and Android Async Http, both of which could be tripping over malformed JSON. Here’s how to adjust them:

For Gson

If you’re using Gson to parse JSON responses that aren’t strictly formatted, create a lenient Gson instance:

Gson lenientGson = new GsonBuilder()
        .setLenient() // Enables lenient parsing
        .create();
// Use this instance instead of the default Gson
YourModel parsedModel = lenientGson.fromJson(jsonResponse, YourModel.class);

For Android Async Http

If your AsyncHttpClient calls return wonky JSON, enable lenient mode directly on the JsonReader when processing the response:

AsyncHttpClient client = new AsyncHttpClient();
client.get("your-api-url", new AsyncHttpResponseHandler() {
    @Override
    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
        try {
            String jsonString = new String(responseBody);
            JsonReader reader = new JsonReader(new StringReader(jsonString));
            reader.setLenient(true); // Allow malformed JSON
            // Proceed to parse with this reader
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
        error.printStackTrace();
    }
});

Check Local JSON Files

If your project includes local JSON files (in res/raw or assets), double-check their formatting:

  • Make sure all quotes are English double quotes (no smart quotes)
  • Remove trailing commas from arrays/objects (e.g., ["item1", "item2",] is invalid)
  • Delete any comments—standard JSON doesn’t support them

Bonus: Tweak Gradle & MultiDex Settings

  • Update Gradle Plugin: Your project uses com.android.tools.build:gradle:3.3.0, which is slightly older than your Android Studio 3.3.1. Try upgrading to 3.3.2 (the latest compatible version for AS 3.3.1) in your root build.gradle:
classpath 'com.android.tools.build:gradle:3.3.2'
  • Verify MultiDex Setup: Since you have multiDexEnabled true, ensure your app’s Application class either extends MultiDexApplication or initializes MultiDex in attachBaseContext:
public class YourApp extends Application {
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}

Start with the Gradle cache and repository fixes—those are the most common culprits when opening an existing project for the first time. If the error persists, dig into your JSON parsing logic or local JSON files. Good luck!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 04:28:15