diff --git a/app/src/main/java/io/legado/app/help/http/CoroutinesCallAdapterFactory.kt b/app/src/main/java/io/legado/app/help/http/CoroutinesCallAdapterFactory.kt deleted file mode 100644 index 70391773a..000000000 --- a/app/src/main/java/io/legado/app/help/http/CoroutinesCallAdapterFactory.kt +++ /dev/null @@ -1,102 +0,0 @@ -package io.legado.app.help.http - -import kotlinx.coroutines.CompletableDeferred -import kotlinx.coroutines.Deferred -import retrofit2.* -import java.lang.reflect.ParameterizedType -import java.lang.reflect.Type - -class CoroutinesCallAdapterFactory private constructor() : CallAdapter.Factory() { - companion object { - fun create(): CoroutinesCallAdapterFactory { - return CoroutinesCallAdapterFactory() - } - } - - override fun get( - returnType: Type, - annotations: Array, - retrofit: Retrofit - ): CallAdapter<*, *>? { - if (Deferred::class.java != getRawType(returnType)) { - return null - } - check(returnType is ParameterizedType) { "Deferred return type must be parameterized as Deferred or Deferred" } - val responseType = getParameterUpperBound(0, returnType) - - val rawDeferredType = getRawType(responseType) - return if (rawDeferredType == Response::class.java) { - check(responseType is ParameterizedType) { "Response must be parameterized as Response or Response" } - ResponseCallAdapter( - getParameterUpperBound( - 0, - responseType - ) - ) - } else { - BodyCallAdapter(responseType) - } - } - - private class BodyCallAdapter( - private val responseType: Type - ) : CallAdapter> { - - override fun responseType() = responseType - - override fun adapt(call: Call): Deferred { - val deferred = CompletableDeferred() - - deferred.invokeOnCompletion { - if (deferred.isCancelled) { - call.cancel() - } - } - - call.enqueue(object : Callback { - override fun onFailure(call: Call, t: Throwable) { - deferred.completeExceptionally(t) - } - - override fun onResponse(call: Call, response: Response) { - if (response.isSuccessful) { - deferred.complete(response.body()!!) - } else { - deferred.completeExceptionally(HttpException(response)) - } - } - }) - - return deferred - } - } - - private class ResponseCallAdapter( - private val responseType: Type - ) : CallAdapter>> { - - override fun responseType() = responseType - - override fun adapt(call: Call): Deferred> { - val deferred = CompletableDeferred>() - - deferred.invokeOnCompletion { - if (deferred.isCancelled) { - call.cancel() - } - } - - call.enqueue(object : Callback { - override fun onFailure(call: Call, t: Throwable) { - deferred.completeExceptionally(t) - } - - override fun onResponse(call: Call, response: Response) { - deferred.complete(response) - } - }) - - return deferred - } - } -} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt index 7ba5c4058..b01be8604 100644 --- a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt +++ b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt @@ -55,8 +55,6 @@ object HttpHelper { return Retrofit.Builder().baseUrl(baseUrl) //增加返回值为字符串的支持(以实体类返回) .addConverterFactory(EncodeConverter(encode)) - //增加返回值为Observable的支持 - .addCallAdapterFactory(CoroutinesCallAdapterFactory.create()) .client(client) .build() } @@ -64,8 +62,6 @@ object HttpHelper { fun getByteRetrofit(baseUrl: String): Retrofit { return Retrofit.Builder().baseUrl(baseUrl) .addConverterFactory(ByteConverter()) - //增加返回值为Observable的支持 - .addCallAdapterFactory(CoroutinesCallAdapterFactory.create()) .client(client) .build() } diff --git a/app/src/main/java/io/legado/app/help/http/api/HttpGetApi.kt b/app/src/main/java/io/legado/app/help/http/api/HttpGetApi.kt index 46346d091..9b44901bc 100644 --- a/app/src/main/java/io/legado/app/help/http/api/HttpGetApi.kt +++ b/app/src/main/java/io/legado/app/help/http/api/HttpGetApi.kt @@ -1,6 +1,5 @@ package io.legado.app.help.http.api -import kotlinx.coroutines.Deferred import retrofit2.Call import retrofit2.Response import retrofit2.http.GET @@ -15,17 +14,17 @@ import retrofit2.http.Url interface HttpGetApi { @GET - fun getAsync( + suspend fun getAsync( @Url url: String, @HeaderMap headers: Map - ): Deferred> + ): Response @GET - fun getMapAsync( + suspend fun getMapAsync( @Url url: String, @QueryMap(encoded = true) queryMap: Map, @HeaderMap headers: Map - ): Deferred> + ): Response @GET fun get( diff --git a/app/src/main/java/io/legado/app/help/http/api/HttpPostApi.kt b/app/src/main/java/io/legado/app/help/http/api/HttpPostApi.kt index 8dab48762..86d91d30c 100644 --- a/app/src/main/java/io/legado/app/help/http/api/HttpPostApi.kt +++ b/app/src/main/java/io/legado/app/help/http/api/HttpPostApi.kt @@ -1,6 +1,5 @@ package io.legado.app.help.http.api -import kotlinx.coroutines.Deferred import okhttp3.RequestBody import retrofit2.Call import retrofit2.Response @@ -15,18 +14,18 @@ interface HttpPostApi { @FormUrlEncoded @POST - fun postMapAsync( + suspend fun postMapAsync( @Url url: String, @FieldMap(encoded = true) fieldMap: Map, @HeaderMap headers: Map - ): Deferred> + ): Response @POST - fun postBodyAsync( + suspend fun postBodyAsync( @Url url: String, @Body body: RequestBody, @HeaderMap headers: Map - ): Deferred> + ): Response @FormUrlEncoded @POST @@ -45,9 +44,9 @@ interface HttpPostApi { @FormUrlEncoded @POST - fun postMapByteAsync( + suspend fun postMapByteAsync( @Url url: String, @FieldMap(encoded = true) fieldMap: Map, @HeaderMap headers: Map - ): Deferred> + ): Response } diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index e9d1c07a1..f1c92e798 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -286,22 +286,18 @@ class AnalyzeUrl( HttpHelper .getApiService(baseUrl, charset) .postMapAsync(url, fieldMap, headerMap) - .await() } else { HttpHelper .getApiService(baseUrl, charset) .postBodyAsync(url, body!!, headerMap) - .await() } } fieldMap.isEmpty() -> HttpHelper .getApiService(baseUrl, charset) .getAsync(url, headerMap) - .await() else -> HttpHelper .getApiService(baseUrl, charset) .getMapAsync(url, fieldMap, headerMap) - .await() } return Res(NetworkUtils.getUrl(res), res.body()) } diff --git a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt index 798307652..5d34a345c 100644 --- a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt @@ -73,8 +73,7 @@ class HttpReadAloudService : BaseReadAloudService(), .postMapByteAsync( "http://tts.baidu.com/text2audio", getAudioBody(contentList[index]), mapOf() - ).await() - .body() + ).body() if (bytes != null && isActive) { val file = getSpeakFile(index) file.writeBytes(bytes)