1

I need to send data to Algolia from my Android, the data sent should be in JSONObject format (import org.json.JSONObject)

the data in Algolia should be in this format

"_geoloc": {
  "lat": 40.639751,
  "lng": -73.778925
}

so in Android, I set the code like this

val jsonObject = JSONObject()

val locHashMap = hashMapOf(
   "lat" to coordinate.latitude,
   "lng" to coordinate.longitude
)


jsonObject.put("_geoloc",locHashMap)

 index.addObjectAsync(jsonObject)

but unfortunately, I get this error:

java.lang.NoSuchMethodError: No virtual method put(Ljava/lang/String;Ljava/util/Map;)Lorg/json/JSONObject; in class Lorg/json/JSONObject; or its super classes (declaration of 'org.json.JSONObject' appears in /system/framework/core-libart.jar)

in this line of code jsonObject.put("_geoloc",locHashMap)

so what should I do in order to send hashmap data in JSONObject format ?

Alexa289
  • 8,089
  • 10
  • 74
  • 178
  • Try this https://stackoverflow.com/questions/35390928/how-to-send-json-object-to-the-server-from-my-android-app – Ankita Nov 15 '19 at 06:52
  • https://www.algolia.com/doc/api-reference/api-methods/save-objects/?language=android#examples – Ankita Nov 15 '19 at 06:55

1 Answers1

3

I might be wrong but try in this way.

Error saying there is no method to put a Map as JSON value but you can put a String (JSON object as string) and it's fine so YourHasMap.toString() and you are done

In code java : jsonObject.put("_geoloc",locHashMap.toString()) index.addObjectAsync(jsonObject)

Pratyush
  • 401
  • 3
  • 11
  • thank you very much pratyus, indeed the crash and error message above will go away, but it seems the double will also become string, and then I have this exception " _geoloc attribute cannot contains a string near line:1 column:633 " – Alexa289 Nov 15 '19 at 07:38
  • Hello sorry for late response. Your double is becoming string because you are passing it as string in simple "25" is string but 25 is int so firebase save it as you sent . Hopefully you got my mean . – Pratyush Nov 18 '19 at 12:44