pull/32/head
kunfei 5 years ago
parent d779f11fcc
commit 6629ee821b
  1. 13
      app/src/main/java/io/legado/app/data/api/IHttpGetApi.kt
  2. 14
      app/src/main/java/io/legado/app/data/api/IHttpPostApi.kt
  3. 9
      app/src/main/java/io/legado/app/help/JsExtensions.java
  4. 26
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.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<String, String>
): Deferred<Response<String>>
@GET
fun get(
@Url url: String,
@HeaderMap headers: Map<String, String>
): Call<Response<String>>
@GET
fun getMap(
@Url url: String,
@QueryMap(encoded = true) queryMap: Map<String, String>,
@HeaderMap headers: Map<String, String>
): Call<Response<String>>
}

@ -28,4 +28,18 @@ interface IHttpPostApi {
@HeaderMap headers: Map<String, String>
): Deferred<Response<String>>
@FormUrlEncoded
@POST
fun postMap(
@Url url: String,
@FieldMap(encoded = true) fieldMap: Map<String, String>,
@HeaderMap headers: Map<String, String>
): Call<Response<String>>
@POST
fun postBody(
@Url url: String,
@Body body: RequestBody,
@HeaderMap headers: Map<String, String>
): Call<Response<String>>
}

@ -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();
}

@ -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<Response<String>> {
return when {
method == Method.POST -> {
if (fieldMap.isNotEmpty()) {
HttpHelper.getApiService<IHttpPostApi>(
baseUrl
).postMap(url, fieldMap, headerMap)
} else {
HttpHelper.getApiService<IHttpPostApi>(
baseUrl
).postBody(
url,
body,
headerMap
)
}
}
fieldMap.isEmpty() -> HttpHelper.getApiService<IHttpGetApi>(
baseUrl
).get(url, headerMap)
else -> HttpHelper.getApiService<IHttpGetApi>(baseUrl)
.getMap(url, fieldMap, headerMap)
}
}
fun getResponseAsync(): Deferred<Response<String>> {
return when {
method == Method.POST -> {

Loading…
Cancel
Save