Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Cannot resolve symbol Gson' and it won't allow me to import

I get an error on Gson(), jsonString and TypeToken in the following code:

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

The actual method is: 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() and jsonString are defined in the following code in my Message class:

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

My Application's build 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
}
}

my App's dependencies are in the following code:

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

I'm ultimately attempting to using Gson to make my json work because my json code isn't working right now so if you guys have advice for that please help but i'm also looking for help on just importing Gson/ importing the library. Thanks!

Sorry if i messed up with the formatting or if this is a repeated thread im a little new to stack overflow and posting! SorrY!

like image 624
Teo Avatar asked Nov 30 '17 05:11

Teo


People also ask

How do you get string from GSON?

String myJSONString = "{'test': '100.00'}"; JsonObject jobj = new Gson(). fromJson(myJSONString, JsonObject. class); String result = jobj. get("test").

How does GSON from JSON work?

Gson can work with arbitrary Java objects including objects for which you do not have the source. For this purpose, Gson provides several built in serializers and deserializers. A serializer allows to convert a Json string to corresponding Java type. A deserializers allows to convert from Java to a JSON representation.


Video Answer


2 Answers

Add following line in activity where you are using Gson:

import com.google.gson.Gson;

Also remove following file you dont need it :

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

And in your dependecies instead of compile files command use following:

compile 'com.google.code.gson:gson:2.6.2'
like image 90
Muhammad Saad Rafique Avatar answered Sep 17 '22 03:09

Muhammad Saad Rafique


You put your Gson dependency in wrong place. Remove Gson dependency from Project build gradle

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

}

and put it in app module dependency like below

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'

}

like image 20
Bek Avatar answered Sep 18 '22 03:09

Bek