AsyncTask sin fugas

Esta class AsyncTask debe ser estática o pueden producirse filtraciones

¿Por qué mi AsyncTask no funciona?

Llamada: PlacesTask(this).execute(...)

Código:

 private class PlacesTask internal constructor(activity: MainActivity) : AsyncTask<String, Int, String>() { var data: String? = null private val mRef: WeakReference<MainActivity> = WeakReference(activity) override fun doInBackground(vararg url: String): String? { try { data = MainActivity().downloadUrl(url[0]) } catch (e: Exception) { Log.d("Background Task", e.toString()) } return data } override fun onPostExecute(result: String) { val asyncTaskLeak = mRef.get() if (asyncTaskLeak != null) SetPlaceTask().execute(result) } } private class SetPlaceTask : AsyncTask<String, Int, List<HashMap<String, String>>>() { var places: List<HashMap<String, String>>? = null override fun doInBackground(vararg jsonData: String): List<HashMap<String, String>>? { try { places = PlaceJSONParser().parse(JSONObject(jsonData[0])) } catch (e: Exception) { Log.d("Exception", e.toString()) } return places } override fun onPostExecute(list: List<HashMap<String, String>>) { ... } } @Throws(IOException::class) private fun downloadUrl(strUrl: String): String { var data = "" var iStream: InputStream? = null var urlConnection: HttpURLConnection? = null try { urlConnection = URL(strUrl).openConnection() as HttpURLConnection urlConnection.connect() iStream = urlConnection.inputStream val br = BuffenetworkingReader(InputStreamReader(iStream!!)) val sb = StringBuilder() var line: String? = null while ({line = br.readLine(); line }() != null) sb.append(line) data = sb.toString() br.close() } catch (e: Exception) { Log.d("downloading url", e.toString()) } finally { if (iStream != null) iStream.close() if (urlConnection != null) urlConnection.disconnect() } return data } 

Error: java.lang.IllegalArgumentException: el parámetro especificado como no nulo es nulo: método kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, resultado del parámetro

Por favor ayuda. ¿Qué me estoy perdiendo?

Como dice la exception, el result del parámetro es null mientras usted lo haya definido como no anulable.

Como ese parámetro es el valor devuelto por doInBackground , sucede porque downloadUrl lanzó una exception, dejando la variable de data como null .

Para solucionarlo, realice alguna de estas opciones:

  1. Defina un valor de retorno pnetworkingeterminado no nulo cuando se produzca una exception, asegurándose de que doInBackground nunca devuelva un valor nulo.
  2. ¿Cambiar el tipo de parámetro de onPostExecute a String? y maneje el caso donde podría ser nulo cuando ocurre una exception.

Entendí.

  1. AsyncTask debe ser static
  2. AsyncTask mueve a -> companion object {} .
  3. WeakReference – Para AsyncTask sin fugas (Bueno exp.)

Ejemplo: AsyncTask sin fugas

 override fun onPause() { super.onPause() ... when { placesTask != null -> { placesTask!!.cancel(true) placesTask = null } setPlacesTask != null -> { setPlacesTask!!.cancel(true) setPlacesTask = null } } } ... @Throws(IOException::class) private fun downloadUrl(strUrl: String): String { var data = "" var iStream: InputStream? = null var urlConnection: HttpURLConnection? = null try { urlConnection = URL(strUrl).openConnection() as HttpURLConnection urlConnection.connect() iStream = urlConnection.inputStream val br = BuffenetworkingReader(InputStreamReader(iStream!!)) val sb = StringBuilder() var line: String? = null while ({line = br.readLine(); line }() != null) sb.append(line) data = sb.toString() br.close() } catch (e: Exception) { Log.d("downloading url", e.toString()) } finally { if (iStream != null) iStream.close() if (urlConnection != null) urlConnection.disconnect() } return data } companion object { ... var placesTask: PlacesTask? = null var setPlacesTask: SetPlaceTask? = null class PlacesTask(mainActivity: MainActivity) : AsyncTask<String, Int, String>() { private val mRef: WeakReference<MainActivity> = WeakReference(mainActivity) override fun doInBackground(vararg url: String): String? { var data: String? = null try { data = mRef.get()?.downloadUrl(url[0]) } catch (e: Exception) { Log.d("Background Task", e.toString()) } return data } override fun onPostExecute(result: String) { setPlacesTask = SetPlaceTask(mRef.get()!!).execute(result) as SetPlaceTask? } } class SetPlaceTask(mainActivity: MainActivity) : AsyncTask<String, Int, List<HashMap<String, String>>>() { private val mRef: WeakReference<MainActivity> = WeakReference(mainActivity) override fun doInBackground(vararg jsonData: String): List<HashMap<String, String>>? { var places: List<HashMap<String, String>>? = null try { places = PlaceJSONParser().parse(JSONObject(jsonData[0])) } catch (e: Exception) { Log.d("Exception", e.toString()) } return places } override fun onPostExecute(list: List<HashMap<String, String>>) { for (i in list.indices) { val hmPlace = list[i] mRef.get()?.parkingLatLng = LatLng(hmPlace["lat"]!!.toDouble(), hmPlace["lng"]!!.toDouble()) mRef.get()?.mMarkerPlaceLink!!.put(mRef.get()?.map!!.addMarker(MarkerOptions().icon(mRef.get()?.vectorToBitmap(R.drawable.ic_local_parking_black_12dp, ContextCompat.getColor(mRef.get()?.applicationContext, R.color.colorPrimaryDark))).position(mRef.get()?.parkingLatLng!!).title(hmPlace["place_name"] + "\n" + hmPlace["vicinity"])).id, hmPlace["reference"].toString()) mRef.get()?.map!!.addCircle(CircleOptions().center(mRef.get()?.parkingLatLng).radius(1.0).strokeColor(Color.BLACK).fillColor(Color.WHITE).strokeWidth(1f).zIndex(1f)) } } } } 

Llamada: placesTask = PlacesTask(this).execute("https://maps.googleapis.com/maps/api/place/nearbysearch/json?location="...) as PlacesTask?

¡Buena suerte!