From 6629ee821b6fb8fa91b723b3a998f595b60c9a8f Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 4 Sep 2019 11:09:33 +0800 Subject: [PATCH] up --- .../io/legado/app/data/api/IHttpGetApi.kt | 13 ++++++++++ .../io/legado/app/data/api/IHttpPostApi.kt | 14 ++++++++++ .../java/io/legado/app/help/JsExtensions.java | 9 +++++-- .../app/model/analyzeRule/AnalyzeUrl.kt | 26 +++++++++++++++++++ 4 files changed, 60 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/api/IHttpGetApi.kt b/app/src/main/java/io/legado/app/data/api/IHttpGetApi.kt index 774cf6c89..a5b0e60c6 100644 --- a/app/src/main/java/io/legado/app/data/api/IHttpGetApi.kt +++ b/app/src/main/java/io/legado/app/data/api/IHttpGetApi.kt @@ -1,6 +1,7 @@ package io.legado.app.data.api import kotlinx.coroutines.Deferred +import retrofit2.Call import retrofit2.Response import retrofit2.http.GET import retrofit2.http.HeaderMap @@ -26,4 +27,16 @@ interface IHttpGetApi { @HeaderMap headers: Map ): Deferred> + @GET + fun get( + @Url url: String, + @HeaderMap headers: Map + ): Call> + + @GET + fun getMap( + @Url url: String, + @QueryMap(encoded = true) queryMap: Map, + @HeaderMap headers: Map + ): Call> } diff --git a/app/src/main/java/io/legado/app/data/api/IHttpPostApi.kt b/app/src/main/java/io/legado/app/data/api/IHttpPostApi.kt index 21fb2622c..113a77291 100644 --- a/app/src/main/java/io/legado/app/data/api/IHttpPostApi.kt +++ b/app/src/main/java/io/legado/app/data/api/IHttpPostApi.kt @@ -28,4 +28,18 @@ interface IHttpPostApi { @HeaderMap headers: Map ): Deferred> + @FormUrlEncoded + @POST + fun postMap( + @Url url: String, + @FieldMap(encoded = true) fieldMap: Map, + @HeaderMap headers: Map + ): Call> + + @POST + fun postBody( + @Url url: String, + @Body body: RequestBody, + @HeaderMap headers: Map + ): Call> } diff --git a/app/src/main/java/io/legado/app/help/JsExtensions.java b/app/src/main/java/io/legado/app/help/JsExtensions.java index 93dc3ba92..7b45b0bc2 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.java +++ b/app/src/main/java/io/legado/app/help/JsExtensions.java @@ -6,6 +6,8 @@ import java.util.regex.Pattern; import io.legado.app.model.analyzeRule.AnalyzeUrl; import io.legado.app.utils.Encoder; import io.legado.app.utils.StringUtils; +import retrofit2.Call; +import retrofit2.Response; public class JsExtensions { @@ -16,8 +18,11 @@ public class JsExtensions { public String ajax(String urlStr) { try { AnalyzeUrl analyzeUrl = new AnalyzeUrl(urlStr, null, null, null, null, null); -/* Response response = analyzeUrl.getResponseAsync().await(); - return response.body().toString();*/ + Call call = analyzeUrl.getResponse(); + Response response = call.execute(); + if (response.body() != null) { + return response.body().toString(); + } } catch (Exception e) { return e.getLocalizedMessage(); } 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 075c72aca..29b6fe1f4 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 @@ -15,6 +15,7 @@ import okhttp3.FormBody import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.RequestBody import okhttp3.RequestBody.Companion.toRequestBody +import retrofit2.Call import retrofit2.Response import java.net.URLEncoder import java.util.* @@ -199,6 +200,31 @@ class AnalyzeUrl( GET, POST } + fun getResponse(): Call> { + return when { + method == Method.POST -> { + if (fieldMap.isNotEmpty()) { + HttpHelper.getApiService( + baseUrl + ).postMap(url, fieldMap, headerMap) + } else { + HttpHelper.getApiService( + baseUrl + ).postBody( + url, + body, + headerMap + ) + } + } + fieldMap.isEmpty() -> HttpHelper.getApiService( + baseUrl + ).get(url, headerMap) + else -> HttpHelper.getApiService(baseUrl) + .getMap(url, fieldMap, headerMap) + } + } + fun getResponseAsync(): Deferred> { return when { method == Method.POST -> {