【问题标题】:MalformedJsonException when I use volley and gson当我使用 volley 和 gson 时出现 MalformedJsonException
【发布时间】:2018-08-15 14:31:59
【问题描述】:

我正在尝试使用 volley 和 gson 做一个小例子。我在 gradle 文件中有以下发布的依赖项。但是当我制作构建项目时,我收到了 发布错误。

你能告诉我如何解决它

错误

Error:com.google.common.util.concurrent.UncheckedExecutionException: java.lang.RuntimeException: com.google.gson.stream.MalformedJsonException: Use 
JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1
Error:Execution failed for task ':app:mergeDebugResources'.
> Error: com.google.common.util.concurrent.UncheckedExecutionException: java.lang.RuntimeException: com.google.gson.stream.MalformedJsonException: Use 
JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1

分级

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile 'dev.dworks.libs:volleyplus:0.1.4'
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.android.support:support-v4:25.0.+'
}

【问题讨论】:

  • 它读取use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 ...这意味着,输入可能包含除 JSON 之外的任何内容。除非发布 JSON,否则这个问题只是推测性的。
  • 我没有看到你在build.gradle文件中导入gson库。所以先检查一下。
  • @orzangleli 我只是忘记将 gson 的依赖项添加到问题中..现在我更新了问题..请看看..无论如何我有同样的错误
  • @LetsamrIt 我想看看你是怎么用gson的。

标签: android android-layout gson android-volley


【解决方案1】:

某些情况可能会出现此错误。

从服务器发送 application/json 作为内容类型。

Gson gson = new GsonBuilder()
        .setLenient()
        .create();

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(BASE_URL)
        .client(client)
        .addConverterFactory(GsonConverterFactory.create(gson))
        .build();

implementation 'com.google.code.gson:gson:2.6.1'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    • 2017-12-10
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多