【问题标题】:'Cannot resolve symbol Gson' and it won't allow me to import“无法解析符号 Gson”,它不允许我导入
【发布时间】:2017-11-30 05:47:30
【问题描述】:

在以下代码中,Gson()jsonStringTypeToken 出现错误:

Map<String, Object> jsonMap = new Gson().fromJson(jsonString, new TypeToken<HashMap<String, Object>>() {}.getType());

实际的方法是: 公共无效发送按钮(查看v){ Message myMessage = new Message(username, getTimeAndDate(), getMessage(), chatroomName, Incognito);

    String message = myMessage.messageData();
    Map<String, Object> jsonMap = new Gson().fromJson(jsonString, new TypeToken<HashMap<String, Object>>() {}.getType());
   // ref.setValue(myMessage.messageData());

messageData()jsonString 在我的 Message 类的以下代码中定义:

public String messageData() {

    JSONObject obj1 = new JSONObject();
    JSONObject obj2 = new JSONObject();

    //  Map<String, JSONObject> mapJSON = new HashMap<String, JSONObject>();
    try {
        obj1.put("Message", chatMessage);
        obj1.put("Username", username);
        obj1.put("isIncognito", Incognito);
        obj2.put(Long.toString(sendTime), obj1.toString());

    } catch (JSONException JE) {
        Log.e("Json Crash!!!", "Something went wrong here");
    }
    String jsonString = obj2.toString();

   //
    // mapJSON.put(chatRoomName, obj2);

            return jsonString;
}

我的应用程序的构建等级:

buildscript {
repositories {
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:2.3.3'
    classpath 'com.google.gms:google-services:3.1.0'
    classpath 'com.google.code.gson:gson:2.2.3'
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
}

我的App的依赖在以下代码中:

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'
compile 'com.android.support:design:25.3.1'
compile 'com.google.firebase:firebase-storage:10.2.0'
compile 'com.google.firebase:firebase-auth:10.2.0'
compile 'com.google.firebase:firebase-core:10.2.0'
compile 'com.google.firebase:firebase-database:10.2.0'
compile 'com.google.android.gms:play-services-ads:10.2.0'
compile files('libs/gson-2.2.4.jar')

testCompile 'junit:junit:4.12'

}
apply plugin: 'com.google.gms.google-service

我最终尝试使用 Gson 来使我的 json 工作,因为我的 json 代码现在无法正常工作,所以如果你们对此有任何建议,请提供帮助,但我也在寻求有关导入 Gson/导入的帮助图书馆。谢谢!

对不起,如果我搞砸了格式或者这是一个重复的线程,我对堆栈溢出和发布有点新!对不起!

【问题讨论】:

  • 你解决了吗

标签: java android json firebase gson


【解决方案1】:

在您使用 Gson 的活动中添加以下行:

import com.google.gson.Gson;

同时删除以下您不需要的文件:

classpath 'com.google.code.gson:gson:2.2.3'

并且在您的依赖项而不是编译文件命令中使用以下命令:

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

【讨论】:

  • 希望这个工作伙伴也让我知道以获得进一步的帮助。快乐的编码兄弟:)
【解决方案2】:

您将 Gson 依赖项放在错误的位置。从 Project build gradle 中移除 Gson 依赖

dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.google.gms:google-services:3.1.0'

classpath 'com.google.code.gson:gson:2.2.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files

}

并将其放入应用程序模块依赖项中,如下所示

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
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.0'
compile 'com.android.support:design:25.3.0'
compile 'com.android.support:support-v13:25.3.0'
compile 'com.android.support:support-v4:25.3.0'
compile 'com.android.support:recyclerview-v7:25.3.0'
compile 'com.google.code.gson:gson:2.8.1'
testCompile 'junit:junit:4.12'

}

【讨论】:

    【解决方案3】:
    dependencies {
        compile 'com.google.code.gson:gson:2.2.+'
        compile 'com.android.support:support-v4:13.0.+'
        compile 'com.android.support:appcompat-v7:18.0.+'
    }
    

    添加这个

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-13
      • 2021-01-13
      • 2020-01-22
      • 1970-01-01
      • 1970-01-01
      • 2022-01-26
      相关资源
      最近更新 更多