【问题标题】:Trying to get JSON with koush/ion and get a DexException尝试使用 koush/ion 获取 JSON 并获取 DexException
【发布时间】:2014-09-13 06:52:01
【问题描述】:

我正在使用 koush/ion 库,我使用它从 url 获取 JSON,问题是当我尝试运行我的应用程序时它会抛出这个

Error:Execution failed for task ':app:dexDebug'.
com.android.ide.common.internal.LoggedErrorException: Failed to run command:
/Applications/Android Studio.app/sdk/build-tools/android-4.4W/dx --dex --num-threads=4 --output 
    /Users/Ger/Desktop/Slide/app/build/intermediates/dex/debug 
 /Users/Ger/Desktop/Slide/app/build/intermediates/classes/debug 
 /Users/Ger/Desktop/Slide/app/build/intermediates/dependency-cache/debug 
 /Users/Ger/Desktop/Slide/app/build/intermediates/pre-dexed/debug/androidasync-1.3.8-a258b3b9f5350460cf9a1a47d3844ac30e9c5eba.jar 
 /Users/Ger/Desktop/Slide/app/build/intermediates/pre-dexed/debug/gson-2.2-3174cdb2bcc21df781020dd287f6d8f9b30fbcf4.jar
  /Users/Ger/Desktop/Slide/app/build/intermediates/pre-dexed/debug/gson-2.3-2c927fa37b669aaa0ff9991dd7c990e2d88a7487.jar 
 /Users/Ger/Desktop/Slide/app/build/intermediates/pre-dexed/debug/ion-1.3.8-df2772cd07adc55daba28b3d0ce1374cf5a4edd5.jar
  /Users/Ger/Desktop/Slide/app/build/intermediates/pre-dexed/debug/support-v4-18.0.0-2484d88715cbcdaf306e7f8449d6546e84c1520d.jar
  Error Code:
    2
  Output:
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Lcom/google/gson/JsonSerializer;
    at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:594)
    at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:552)
    at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:533)
    at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:170)
    at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
    at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:287)
    at com.android.dx.command.dexer.Main.run(Main.java:230)
    at com.android.dx.command.dexer.Main.main(Main.java:199)
    at com.android.dx.command.Main.main(Main.java:103)

这是我的代码:

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.ion.Ion;

public class NuevosEpisodios extends Activity {

String resultado = "A";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.temporada);

    TextView tv = (TextView) findViewById(R.id.descripcion_temp);
    Ion.with(NuevosEpisodios.this)
            .load("http://example.com/thing.json")
            .asJsonObject()
            .setCallback(new FutureCallback<JsonObject>() {
                @Override
                public void onCompleted(Exception e, JsonObject result) {
                    // do stuff with the result or error
                    Gson gson = new Gson();
                    resultado = gson.toJson(result);
                }
            });

    tv.setText(resultado);
}
}

我已经看到了很多关于这个问题的答案,但没有人为我工作,请帮帮我 我的 lib 文件夹中有 gson-2.2 和 koush/ion jar,它们都在我的构建 gradle 中编译:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:18.0.+'
compile 'com.koushikdutta.ion:ion:1.+'
compile files('libs/gson-2.2.jar')
}

提前致谢!

【问题讨论】:

    标签: java android json android-studio gson


    【解决方案1】:

    当我在测试项目中复制您的 dependencies 块时,我没有收到您的错误。我认为您的 libs 目录中有一个额外的 jar 文件导致了问题。您的构建文件中的此语句:

    compile fileTree(dir: 'libs', include: ['*.jar'])
    

    让它自动拾取您放在该目录中的任何 .jar 文件,因此请查看您是否在其中有不需要的东西。无论如何,使用该通配符包含,此语句是多余的:

    compile files('libs/gson-2.2.jar')
    

    另外,无需下载 GSON 的 jar 并将其手动放入您的 libs 目录,您可以让它自动下载和管理它,就像您对 com.koushikdutta.ion:ion 依赖项所做的一样:

    compile 'com.google.code.gson:gson:2.2'
    

    这个库有比 2.2 更新的版本;您可能需要考虑升级。 http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22com.google.code.gson%22%20AND%20a%3A%22gson%22

    【讨论】:

    • 您好,我不知道该怎么做,所以我使用了 asynctask 类,但感谢您的提问
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-07
    • 2023-03-22
    • 1970-01-01
    相关资源
    最近更新 更多