Ime having as problem with AsyncTask (I know it is...
# getting-started
b
Ime having as problem with AsyncTask (I know it is depreciated, but I am doing an exercise). Basically, doInBackground is running fine but onPostExecute never runs.
Copy code
@file:Suppress("DEPRECATION")

package com.funkytwig.flickerbrowser

import android.os.AsyncTask
import android.util.Log
import java.io.IOException
import java.net.MalformedURLException
import java.net.URL

enum class DowsloadStatus {
    OK, IDLE, NOT_INSTALLED, FAILED_OR_EMPTY, PERMISSIONS_ERROR, ERROR
}
private const val TAG = "GetRawData"

@Suppress("DEPRECATION")
class GetRawData : AsyncTask<String, Void, String>() { // class
    private var downloadStatus = DowsloadStatus.IDLE

    @Deprecated("Deprecated in Java")
    override fun doInBackground(vararg params: String?): String {
        // params[0] = JSON URL
        val funct = "doInBackground"
        Log.d(TAG, funct)
        Log.d(TAG, "$funct ${params[0]}")
        if (params[0] == null) {
            Log.d(TAG, "$funct No URL")
            downloadStatus = DowsloadStatus.NOT_INSTALLED
            return "No URL Specified"
        }

        try {
            downloadStatus = DowsloadStatus.OK
            Log.d(TAG, "$funct: DowsloadStatus.OK")
            return URL(params[0]).readText()
        } catch (e: Exception) {
            val errorMessage = when (e) {
                is MalformedURLException -> {
                    downloadStatus = DowsloadStatus.NOT_INSTALLED
                    "Invalid URL ${e.message}"
                }
                is IOException -> {
                    downloadStatus = DowsloadStatus.FAILED_OR_EMPTY
                    "IO Exception ${e.message}"
                }
                is SecurityException -> {
                    downloadStatus = DowsloadStatus.PERMISSIONS_ERROR
                    "Needs Permission ${e.message}"
                }
                else -> {
                    downloadStatus = DowsloadStatus.ERROR
                    "Unknown Error ${e.message}"
                }

            }
            Log.d(TAG, "$funct dome errorMessage")

            return errorMessage
        } // try/catch
    } // doInBackground

    @Deprecated("Deprecated in Java")
    override fun onPostExecute(result: String?) {
        val funct = "onPostExecute"
        Log.d(TAG, funct)
        super.onPostExecute(result)
    }

    @Deprecated("Deprecated in Java")
    override fun onCancelled() {
        val funct = "onCancelled"
        Log.d(TAG, funct)
        super.onCancelled()
    }

    @Deprecated("Deprecated in Java")
    override fun onPreExecute() {
        val funct = "onPreExecute"
        Log.d(TAG, funct)
        super.onPreExecute()
    }

    @Deprecated("Deprecated in Java")
    override fun onProgressUpdate(vararg values: Void?) {
        val funct = "onProgressUpdate"
        Log.d(TAG, funct)
        super.onProgressUpdate(*values)
    }
}
Any ideas?
m
what is the emulator/device version ? if it is android 11 and above it will not work
b
@Mustafa Ozhan thanks for replying. I've been using SDK 17,26 & 29 on emulater and up to now everything has been fine, including with
Copy code
return URL(params[0]).readText()
which is the line discovered is where the problem is. I did some running in debug and when it runs that line it hangs. I tried with a real phone and it works. Is there a particular phone SDSK I should use?
m
yes as i said before it will not work android 11 and above that’s why it is deprecated