java - '无法解析符号 Gson',它不允许我导入

标签 java android json firebase gson

我在以下代码中收到有关 Gson()jsonStringTypeToken 的错误:

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

实际方法是: public void sendButton(View 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
}
}

我的应用程序的依赖项在以下代码中:

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/导入的帮助图书馆。谢谢!

抱歉,如果我弄乱了格式,或者这是一个重复的线程,我对堆栈溢出和发布有点陌生!对不起!

最佳答案

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

import com.google.gson.Gson;

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

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

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

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

关于java - '无法解析符号 Gson',它不允许我导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47566665/

相关文章:

java - 为什么类没有注入(inject)到模块中?

java - 在 Cucumber 中的步骤定义之间共享状态

java - java中日期格式的转换方法

android - 如何在 ListView 中将方法 "isChecked()"状态设置为默认为 true?

c# - Newtonsoft 对象序列化为字符串。预期 JObject 实例

java - Android应用程序中的JSON解析

java - jsonrpc4j : How to add type info to parameters?

java - Spring - 如何在 Transactional 方法中获取对 TransactionStatus 对象的引用

java - 在Eclipse中远程调试时如何单步进入JDK源代码

android - 如何解决 Android 中 Twitter 中的更新速率限制错误?