Use GSON reflection to parse JSON to model
This commit is contained in:
parent
621d4fef2c
commit
437a4359f8
8
app/src/main/java/fishrungames/yelpmapapp/CoordinatesRecord.kt
Executable file
8
app/src/main/java/fishrungames/yelpmapapp/CoordinatesRecord.kt
Executable file
@ -0,0 +1,8 @@
|
||||
package fishrungames.yelpmapapp
|
||||
|
||||
/**
|
||||
* Created by mephi on 10.06.2017.
|
||||
*/
|
||||
|
||||
data class CoordinatesRecord(val latitude: Double,
|
||||
val longitude: Double)
|
@ -2,6 +2,8 @@ package fishrungames.yelpmapapp
|
||||
|
||||
import android.os.AsyncTask
|
||||
import com.google.gson.JsonObject
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.reflect.TypeToken
|
||||
|
||||
|
||||
/**
|
||||
@ -12,6 +14,7 @@ import com.google.gson.JsonObject
|
||||
class HandleResponseAsyncTask(val onUpdateMapMarkers : ((Map<String, MapMarkerRecord>) -> Unit)) : AsyncTask<JsonObject, Int, Map<String, MapMarkerRecord>>()
|
||||
{
|
||||
|
||||
|
||||
override fun doInBackground(vararg response : JsonObject): Map<String, MapMarkerRecord> {
|
||||
|
||||
val newMapMarkers = mutableMapOf<String, MapMarkerRecord>()
|
||||
@ -19,17 +22,12 @@ class HandleResponseAsyncTask(val onUpdateMapMarkers : ((Map<String, MapMarkerRe
|
||||
try {
|
||||
|
||||
for (businessElement in response[0].getAsJsonArray("businesses")) {
|
||||
val businessJson = businessElement.asJsonObject
|
||||
|
||||
val id = businessJson.get("id").asString
|
||||
val name = businessJson.get("name").asString
|
||||
val image_url = businessJson.get("image_url").asString
|
||||
val latitude = businessJson.get("coordinates").asJsonObject.get("latitude").asDouble
|
||||
val longitude = businessJson.get("coordinates").asJsonObject.get("longitude").asDouble
|
||||
val mapMarkerRecordType = object : TypeToken<MapMarkerRecord>() {}.type
|
||||
|
||||
var mapMarker = MapMarkerRecord(id, name, image_url, latitude, longitude)
|
||||
val mapMarkerRecord = Gson().fromJson<MapMarkerRecord>(businessElement, mapMarkerRecordType)
|
||||
|
||||
newMapMarkers[name] = mapMarker
|
||||
newMapMarkers[mapMarkerRecord.name] = mapMarkerRecord
|
||||
|
||||
}
|
||||
|
||||
|
@ -11,7 +11,7 @@ import com.google.android.gms.maps.model.LatLng
|
||||
|
||||
class MapMarkerClusterItem(inMapMarkerRecord : MapMarkerRecord) : ClusterItem
|
||||
{
|
||||
private val mPosition = LatLng(inMapMarkerRecord.lat, inMapMarkerRecord.lon)
|
||||
private val mPosition = LatLng(inMapMarkerRecord.coordinates.latitude, inMapMarkerRecord.coordinates.longitude)
|
||||
|
||||
val mapMarkerRecord = inMapMarkerRecord
|
||||
|
||||
|
@ -5,10 +5,10 @@ package fishrungames.yelpmapapp
|
||||
*/
|
||||
|
||||
|
||||
|
||||
data class MapMarkerRecord (
|
||||
val id: String,
|
||||
val name: String,
|
||||
val imageUrl: String,
|
||||
val lat: Double,
|
||||
val lon: Double
|
||||
val image_url: String,
|
||||
val coordinates: CoordinatesRecord
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user