Я пытаюсь использовать некоторые ссылки API.Поэтому, чтобы сделать это, я создал базовый класс AsyncTask
, который отличается на SOURCE
и RESULT
для каждого действия в приложении с использованием шаблонов.
Проблема заключается в том, что я пытаюсь инициализировать один изподклассы, он возвращает ClassCastException
ошибку.
Вот базовый асинхронный класс
abstract class MvpBackgroundInteraction<SOURCE, RESULT> : AsyncTask<SOURCE, Int, RESULT?>(), MvpFeedContract.MvpFeedInteraction<SOURCE, RESULT> {
protected var isWorking: Boolean = true
protected var completeListener : MvpFeedContract.MvpCompleteListener<RESULT>? = null
override fun start(source: SOURCE) {
super.execute(source)
}
override fun stop() {
isWorking = false
}
override fun addCompleteListener(completeListener: MvpFeedContract.MvpCompleteListener<RESULT>?) {
this.completeListener = completeListener
}
override fun onPostExecute(result: RESULT?) {
completeListener?.apply {
if (result != null) onComplete(result) else onFailure("Result is null")
}
super.onPostExecute(result)
}
}
Вот MvpFeedContract
interface MvpFeedContract {
interface MvpFeedInteraction<SOURCE, RESULT> {
fun start(source : SOURCE)
fun stop()
fun addCompleteListener(completeListener: MvpCompleteListener<RESULT>?)
}
interface MvpCompleteListener<RESULT> {
fun onComplete(result : RESULT)
fun onFailure(message : String?)
}
}
Вот один из подклассов, который возвращает эту ошибку
class MvpDownloader : MvpBackgroundInteraction<String, InputStream>() {
override fun doInBackground(vararg p0: String): InputStream? {
return try {
if (isWorking) downloadUrl(p0[0]) else null
} catch (e : Exception) {
Log.e("MvpParser", e.message ?: "error")
null
}
}
companion object {
@Throws(IOException::class)
private fun downloadUrl(urlString: String): InputStream? {
val url = URL(urlString)
return (url.openConnection() as? HttpURLConnection)?.run {
readTimeout = 10000
connectTimeout = 15000
requestMethod = "GET"
doInput = true
connect()
inputStream
}
}
}
}
Вот как я его инициализирую и некоторые похожие классы
private val downloader : MvpDownloader by lazy { MvpDownloader() }
private val parser : MvpParser<OBJECT> by lazy { MvpParser<OBJECT>() }
private val cashier : MvpCashier<OBJECT> by lazy { MvpCashier<OBJECT>(baseDao) }
Вот возвращенный logcat
2019-07-10 14:55:11.962 1708-1729/com.tamimattafi.degitalnomads E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.tamimattafi.degitalnomads, PID: 1708
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:330)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:255)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:776)
Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[]
at com.tamimattafi.degitalnomads.repository.feed.MvpDownloader.doInBackground(MvpDownloader.kt:10)
at android.os.AsyncTask$2.call(AsyncTask.java:316)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:255)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:776)
РЕДАКТИРОВАТЬ Вот мой менеджер каналов
class FeedManager<OBJECT>(baseDao: BaseDao<OBJECT>) : MvpFeedContract.MvpFeedInteraction<String, Boolean> {
private var isWorking = true
private var completeListener: MvpFeedContract.MvpCompleteListener<Boolean>? = null
private val downloader : MvpDownloader by lazy { MvpDownloader() }
private val parser : MvpParser<OBJECT> by lazy { MvpParser<OBJECT>() }
private val cashier : MvpCashier<OBJECT> by lazy { MvpCashier<OBJECT>(baseDao) }
override fun start(source: String) {
downloadFeed(source)
}
private fun downloadFeed(link : String) {
downloader.apply {
addCompleteListener(object :
MvpFeedContract.MvpCompleteListener<InputStream> {
override fun onComplete(result: InputStream) {
parseFeed(result)
}
override fun onFailure(message: String?) {
completeListener?.onFailure(message)
}
})
start(link)
}
}
private fun parseFeed(result: InputStream) {
parser.apply {
addCompleteListener(object :
MvpFeedContract.MvpCompleteListener<List<OBJECT>> {
override fun onComplete(result: List<OBJECT>) {
cashFeed(result)
}
override fun onFailure(message: String?) {
completeListener?.onFailure(message)
}
})
start(result)
}
}
private fun cashFeed(result: List<OBJECT>) {
cashier.apply {
addCompleteListener(object :
MvpFeedContract.MvpCompleteListener<Boolean> {
override fun onComplete(result: Boolean) {
completeListener?.onComplete(result)
}
override fun onFailure(message: String?) {
completeListener?.onFailure(message)
}
})
start(result)
}
}
override fun stop() {
isWorking = false
downloader.stop()
parser.stop()
cashier.stop()
}
override fun addCompleteListener(completeListener: MvpFeedContract.MvpCompleteListener<Boolean>?) {
this.completeListener = completeListener
}
}