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 android.os.AsyncTask
|
||||||
import com.google.gson.JsonObject
|
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>>()
|
class HandleResponseAsyncTask(val onUpdateMapMarkers : ((Map<String, MapMarkerRecord>) -> Unit)) : AsyncTask<JsonObject, Int, Map<String, MapMarkerRecord>>()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
override fun doInBackground(vararg response : JsonObject): Map<String, MapMarkerRecord> {
|
override fun doInBackground(vararg response : JsonObject): Map<String, MapMarkerRecord> {
|
||||||
|
|
||||||
val newMapMarkers = mutableMapOf<String, MapMarkerRecord>()
|
val newMapMarkers = mutableMapOf<String, MapMarkerRecord>()
|
||||||
@ -19,17 +22,12 @@ class HandleResponseAsyncTask(val onUpdateMapMarkers : ((Map<String, MapMarkerRe
|
|||||||
try {
|
try {
|
||||||
|
|
||||||
for (businessElement in response[0].getAsJsonArray("businesses")) {
|
for (businessElement in response[0].getAsJsonArray("businesses")) {
|
||||||
val businessJson = businessElement.asJsonObject
|
|
||||||
|
|
||||||
val id = businessJson.get("id").asString
|
val mapMarkerRecordType = object : TypeToken<MapMarkerRecord>() {}.type
|
||||||
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
|
|
||||||
|
|
||||||
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
|
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
|
val mapMarkerRecord = inMapMarkerRecord
|
||||||
|
|
||||||
|
@ -5,10 +5,10 @@ package fishrungames.yelpmapapp
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
data class MapMarkerRecord (
|
data class MapMarkerRecord (
|
||||||
val id: String,
|
val id: String,
|
||||||
val name: String,
|
val name: String,
|
||||||
val imageUrl: String,
|
val image_url: String,
|
||||||
val lat: Double,
|
val coordinates: CoordinatesRecord
|
||||||
val lon: Double
|
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user