简体   繁体   English

尝试使用koush / ion获取JSON并获取DexException

[英]Trying to get JSON with koush/ion and get a DexException

I'm working with the koush/ion library that I use it for get a JSON from a url, the problem is that when I'm trying to run my app it throws this 我正在使用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)

And this is my code: 这是我的代码:

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);
}
}

I've seen many answers to this problem but no one worked for me, please give me a hand I have the gson-2.2 and the koush/ion jar in my lib folder, both of them are compiled in my build gradle: 我已经看到了许多解决此问题的答案,但是没有人为我工作,请帮帮我,我的lib文件夹中有gson-2.2和koush / ion jar,它们都在build 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')
}

Thanks in advanced! 提前致谢!

When I duplicate your dependencies block inside a test project, I don't get your error. 当我在测试项目中复制您的dependencies块时,我没有收到您的错误。 I think you have an extra jar file inside your libs directory that's causing the problem. 我认为您的libs目录中有一个额外的jar文件,这引起了问题。 This statement in your build file: 您的构建文件中的以下语句:

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

makes it automatically pick up any .jar files you put in that directory, so see if you have something in there you don't need. 使其自动获取放置在该目录中的所有.jar文件,因此请查看是否有不需要的文件。 At any rate, with that wildcard include, this statement is redundant: 无论如何,使用该通配符include,此语句是多余的:

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

Also, instead of downloading GSON's jar and putting it manually into your libs directory, you can have it automatically download and manage it the same as you do for the com.koushikdutta.ion:ion dependency: 另外,您可以像使用com.koushikdutta.ion:ion依赖项一样自动下载并管理GSON的jar,而不是将其手动放入您的libs目录中:

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

There are newer versions of this library than 2.2; 该库的版本比2.2高。 you might want to consider upgrading. 您可能要考虑升级。 http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22com.google.code.gson%22%20AND%20a%3A%22gson%22 http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22com.google.code.gson%22%20AND%20a%3A%22gson%22

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM