您好!
我正在使用Google Maps,使用他们的SDK和API(Place API等(。当代码到达val parentObject = JSONObject(result)
时,它会停止执行下面的行。我调试了它,发现我传递的URL和Maps是可以的,而且我已经在Android Studio之外检查了URL,它给了我一个JSON数组。我不明白为什么它就停在那条线上。AsyncTask
的代码如下:
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) {
}
}
}
非常感谢您的帮助!
google Place Api响应中没有带"候选者"键的json数组,请确保您正确地反序列化了响应,
所以我重新制作了这个类,制作了一个新的类(PlacesAPI
(来分离AsyncTask和HTTP连接。此外,我在新的实例中添加了更多的东西,如requestMethod
、bufferReader
等。我没有使用类的新实例(我用它来传递数据(,而是制作了其他类中需要使用的方法的参数。有一个新类别的代码:
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
}
}
现在我的AsyncTask
看起来像:
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
}
}
}
它成功了!现在它向我展示了我需要的一切!感谢大家的帮助。