首次打开Android Studio现有项目遇JSON格式错误求助
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/cacheson macOS/Linux orC:\Users\<YourName>\.gradle\cacheson Windows), delete all its contents, then go back to Android Studio and clickFile > Sync Project with Gradle Files. - Simplify Repository Config: Your root
build.gradlehas bothgoogle()andmaven { url "https://maven.google.com" }—these are the same repository, so the duplicate entry could cause conflicts. Remove themaven { 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 to3.3.2(the latest compatible version for AS 3.3.1) in your rootbuild.gradle:
classpath 'com.android.tools.build:gradle:3.3.2'
- Verify MultiDex Setup: Since you have
multiDexEnabled true, ensure your app’sApplicationclass either extendsMultiDexApplicationor initializes MultiDex inattachBaseContext:
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

