微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – ‘无法解析符号Gson’,它不允许我导入

我在以下代码中的Gson(),jsonString和Typetoken上出错:

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

实际的方法是:
     public void sendButton(View v){
        消息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;
}

我的应用程序的构建gradle:

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的活动中添加以下行:

import com.google.gson.Gson;

同时删除以下文件,你不需要它:

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

在您的dependecies而不是编译文件命令使用以下:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐