Issue
Good day!
I'm working with Google Maps, using their SDK and APIs (Place API, etc.). I faced the problem that when code gets to val parentObject = JSONObject(result)
it stops executing the lines below. I debugged it and saw, that the URL and Maps I'm passing is ok, also I've already checked the URL outside Android Studio and it's ok, it gives me an array of JSONs. I don't understand why it just stops on that line. The code of the AsyncTask
is below:
class MapsAsyncTasks : AsyncTask<Any, String, String>() {
companion object {
private lateinit var map: GoogleMap
private lateinit var url: String
fun dataTransfer(map: GoogleMap, url: String): MapsAsyncTasks {
val asyncTasks = MapsAsyncTasks()
this.map = map
this.url = url
return asyncTasks
}
}
override fun doInBackground(vararg params: Any?): String? {
try {
val mUrl = URL(url)
val httpURLConnection = mUrl.openConnection() as HttpURLConnection
httpURLConnection.connect()
} catch (e: MalformedURLException) {
} catch (e: IOException) {
}
return url
}
override fun onPostExecute(result: String?) {
super.onPostExecute(result)
try {
if(result != null) {
//It comes here
val parentObject = JSONObject(result) //It do this line
val resultsArray = parentObject.getJSONArray("candidates") //It does not executing this and other lines below
for (i in 0 until resultsArray.length()) {
val jsonObject = resultsArray.getJSONObject(i)
val locationObject = jsonObject.getJSONObject("geometry").getJSONObject("location")
val latitude = locationObject.getString("lat").toDouble()
val longitude = locationObject.getString("lng").toDouble()
val nameObject = resultsArray.getJSONObject(i)
val name = nameObject.getString("name")
val latLng = LatLng(latitude, longitude)
val markerOptions = MarkerOptions()
markerOptions.title(name)
markerOptions.position(latLng)
map.addMarker(markerOptions)
}
}
} catch (e: JSONException) {
}
}
}
Many thanks for helping!
Solution
So I remade this class making a new one (PlacesAPI
) to separate AsyncTask and HTTP connection. Also, I added some more stuff to the new one, like requestMethod
, bufferReader
, etc. Instead of the new instance of the class (I used it to pass data), I made the parameters of the methods I needed to be used in the other classes. There is a code of a new class:
class PlacesAPIs {
lateinit var result: String
fun getPlacesJSON(c: Context,
lat: Double,
lng: Double,
keyword: String,
type: String,
radius: Int,
language: String
): String {
try {
val key = c.resources.getString(R.string.google_maps_key)
val urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?keyword=$keyword&type=$type&location=$lat,$lng&radius=$radius&language=$language&key=$key"
val url = URL(urlString)
val httpURLConnection = url.openConnection() as HttpURLConnection
httpURLConnection.setRequestProperty("Content-Type", "application/json")
httpURLConnection.requestMethod = "GET"
httpURLConnection.doInput = true
val br = httpURLConnection.inputStream.bufferedReader()
result = br.use { br.readText() }
httpURLConnection.disconnect()
} catch (e: Exception) {
Log.e("Places API:", "the APIM didn't get JSON")
}
return result
}
}
And right now my AsyncTask
looks like:
class MapsAsyncTasks(var c: Context, var lat: Double, var lng: Double,
var keyword: String, var type: String, var radius: Int,
var language: String, var map: GoogleMap) : AsyncTask<Any, String, String>() {
override fun doInBackground(vararg params: Any?): String? {
return PlacesAPIs().getPlacesJSON(c, lat, lng, keyword, type, radius, language)
}
override fun onPostExecute(result: String?) {
super.onPostExecute(result)
try {
if(result != null) {
val parentObject = JSONObject(result)
Log.i("ParentObject:",parentObject.toString())
val resultsArray = parentObject.getJSONArray("results")
for (i in 0 until resultsArray.length()) {
val jsonObject = resultsArray.getJSONObject(i)
val locationObject = jsonObject.getJSONObject("geometry").getJSONObject("location")
val latitude = locationObject.getString("lat").toDouble()
val longitude = locationObject.getString("lng").toDouble()
val name = jsonObject.getString("name")
val vicinity = jsonObject.getString("vicinity")
val latLng = LatLng(latitude, longitude)
val markerOptions = MarkerOptions()
markerOptions.title(name)
markerOptions.position(latLng)
markerOptions.snippet(
vicinity
)
map.addMarker(markerOptions)
}
}
} catch (e: JSONException) {
//cathing error here
}
}
}
And it worked! Right now it shows me everything I need! Thanks to everybody for helping.
Answered By - Igor Dzichkovskii
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.