From 88527c5716ba3c8ca4c8122f0637ad224a669278 Mon Sep 17 00:00:00 2001
From: zlzw <583819556@qq.com>
Date: Sat, 15 Jun 2024 04:16:57 +0800
Subject: [PATCH] =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E4=B8=BAOkhttp=E8=B0=83?=
=?UTF-8?q?=E7=94=A8=E7=BD=91=E7=BB=9C=20=E4=BF=AE=E5=A4=8DBT=E8=AE=A2?=
=?UTF-8?q?=E9=98=85=E4=B8=8B=E8=BD=BD=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=97=AE?=
=?UTF-8?q?=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 15 +-
.../nas/Controllers/BTDownloadController.java | 1 -
.../java/com/yutou/nas/NasApplication.java | 2 +-
.../java/com/yutou/nas/okhttp/BaseBean.java | 6 +
.../yutou/nas/okhttp/GetRequestParams.java | 25 +++
.../java/com/yutou/nas/okhttp/HttpBody.java | 14 ++
.../com/yutou/nas/okhttp/HttpCallback.java | 33 +++
.../nas/okhttp/HttpLoggingInterceptor.java | 209 ++++++++++++++++++
.../com/yutou/nas/okhttp/IRequestParam.java | 9 +
.../com/yutou/nas/okhttp/ParamsContext.java | 31 +++
.../yutou/nas/okhttp/PostRequestParams.java | 69 ++++++
.../com/yutou/nas/okhttp/api/BaseApi.java | 101 +++++++++
.../com/yutou/nas/okhttp/api/ToolsNetApi.java | 14 ++
.../yutou/nas/okhttp/api/ToolsNetManager.java | 17 ++
.../nas/okhttp/converter/JsonCallAdapter.java | 19 ++
.../converter/JsonConverterFactory.java | 40 ++++
.../converter/JsonRequestBodyConverter.java | 36 +++
.../converter/JsonResponseBodyConverter.java | 42 ++++
.../yutou/nas/utils/BTDownloadManager.java | 19 +-
.../java/com/yutou/nas/utils/HttpTools.java | 6 +-
20 files changed, 692 insertions(+), 16 deletions(-)
create mode 100644 src/main/java/com/yutou/nas/okhttp/BaseBean.java
create mode 100644 src/main/java/com/yutou/nas/okhttp/GetRequestParams.java
create mode 100644 src/main/java/com/yutou/nas/okhttp/HttpBody.java
create mode 100644 src/main/java/com/yutou/nas/okhttp/HttpCallback.java
create mode 100644 src/main/java/com/yutou/nas/okhttp/HttpLoggingInterceptor.java
create mode 100644 src/main/java/com/yutou/nas/okhttp/IRequestParam.java
create mode 100644 src/main/java/com/yutou/nas/okhttp/ParamsContext.java
create mode 100644 src/main/java/com/yutou/nas/okhttp/PostRequestParams.java
create mode 100644 src/main/java/com/yutou/nas/okhttp/api/BaseApi.java
create mode 100644 src/main/java/com/yutou/nas/okhttp/api/ToolsNetApi.java
create mode 100644 src/main/java/com/yutou/nas/okhttp/api/ToolsNetManager.java
create mode 100644 src/main/java/com/yutou/nas/okhttp/converter/JsonCallAdapter.java
create mode 100644 src/main/java/com/yutou/nas/okhttp/converter/JsonConverterFactory.java
create mode 100644 src/main/java/com/yutou/nas/okhttp/converter/JsonRequestBodyConverter.java
create mode 100644 src/main/java/com/yutou/nas/okhttp/converter/JsonResponseBodyConverter.java
diff --git a/pom.xml b/pom.xml
index ab6f1c8..00a2e80 100644
--- a/pom.xml
+++ b/pom.xml
@@ -142,7 +142,18 @@
dom4j
2.1.4
+
+ com.squareup.okhttp3
+ okhttp
+ 4.12.0
+
+
+
+ com.squareup.retrofit2
+ retrofit
+ 2.11.0
+
@@ -191,8 +202,8 @@
maven-compiler-plugin
-
- 1.8
+
+ 9
${basedir}/libs
diff --git a/src/main/java/com/yutou/nas/Controllers/BTDownloadController.java b/src/main/java/com/yutou/nas/Controllers/BTDownloadController.java
index 8a59ae4..65f696f 100644
--- a/src/main/java/com/yutou/nas/Controllers/BTDownloadController.java
+++ b/src/main/java/com/yutou/nas/Controllers/BTDownloadController.java
@@ -30,7 +30,6 @@ public class BTDownloadController {
@ResponseBody
@RequestMapping("/bt/done.do")
public String done(String path, String filename, String hash1, String hash2, String tid) {
- Log.i("BTDownloadController.done", "path = " + path + ", filename = " + filename + ", hash1 = " + hash1 + ", hash2 = " + hash2 + ", tid = " + tid);
//BTDownloadManager.done(path, filename, hash1, hash2, tid);
try {
String exec = ConfigTools.load(ConfigTools.CONFIG, "tiny.exec", String.class);
diff --git a/src/main/java/com/yutou/nas/NasApplication.java b/src/main/java/com/yutou/nas/NasApplication.java
index 0f74fda..3e8bdb0 100644
--- a/src/main/java/com/yutou/nas/NasApplication.java
+++ b/src/main/java/com/yutou/nas/NasApplication.java
@@ -12,7 +12,7 @@ import org.springframework.context.annotation.Import;
@Import(DmhyRssDownloadManager.class)
@SpringBootApplication
public class NasApplication {
- public static final String version = "1.4";
+ public static final String version = "1.4.2";
public static void main(String[] args) {
SpringApplication.run(NasApplication.class, args);
diff --git a/src/main/java/com/yutou/nas/okhttp/BaseBean.java b/src/main/java/com/yutou/nas/okhttp/BaseBean.java
new file mode 100644
index 0000000..e8ad432
--- /dev/null
+++ b/src/main/java/com/yutou/nas/okhttp/BaseBean.java
@@ -0,0 +1,6 @@
+package com.yutou.nas.okhttp;
+
+import java.io.Serializable;
+
+public class BaseBean implements Serializable {
+}
diff --git a/src/main/java/com/yutou/nas/okhttp/GetRequestParams.java b/src/main/java/com/yutou/nas/okhttp/GetRequestParams.java
new file mode 100644
index 0000000..bf56aeb
--- /dev/null
+++ b/src/main/java/com/yutou/nas/okhttp/GetRequestParams.java
@@ -0,0 +1,25 @@
+package com.yutou.nas.okhttp;
+
+
+import okhttp3.HttpUrl;
+import okhttp3.Request;
+
+import java.util.HashMap;
+
+public class GetRequestParams implements IRequestParam {
+ /**
+ * 构建Request
+ *
+ * @param request
+ * @return
+ */
+ @Override
+ public Request getRequest(HashMap map, Request request) {
+ //添加公共参数
+ HttpUrl.Builder builder = request.url().newBuilder();
+ for (String key : map.keySet()) {
+ builder.addQueryParameter(key, String.valueOf(map.get(key)));
+ }
+ return request.newBuilder().url(builder.build()).build();
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/yutou/nas/okhttp/HttpBody.java b/src/main/java/com/yutou/nas/okhttp/HttpBody.java
new file mode 100644
index 0000000..fcae393
--- /dev/null
+++ b/src/main/java/com/yutou/nas/okhttp/HttpBody.java
@@ -0,0 +1,14 @@
+package com.yutou.nas.okhttp;
+
+import lombok.Data;
+
+@Data
+public class HttpBody {
+ private String msg;
+ private String status;
+ private int code;
+ private int retcode;
+ private T data;
+ private String src;
+
+}
\ No newline at end of file
diff --git a/src/main/java/com/yutou/nas/okhttp/HttpCallback.java b/src/main/java/com/yutou/nas/okhttp/HttpCallback.java
new file mode 100644
index 0000000..5a156cf
--- /dev/null
+++ b/src/main/java/com/yutou/nas/okhttp/HttpCallback.java
@@ -0,0 +1,33 @@
+package com.yutou.nas.okhttp;
+
+import okhttp3.Headers;
+import retrofit2.Call;
+import retrofit2.Callback;
+import retrofit2.Response;
+
+public abstract class HttpCallback implements Callback> {
+
+ public abstract void onResponse(Headers headers,int code, String status, T response, String rawResponse);
+
+ public abstract void onFailure(Throwable throwable);
+
+ @Override
+ public void onResponse(Call> call, Response> response) {
+ if (response.body() != null) {
+ onResponse(
+ response.headers(),
+ response.body().getRetcode(),
+ response.body().getStatus(),
+ response.body().getData(),
+ response.body().getSrc()
+ );
+ } else {
+ onFailure(new NullPointerException("response body is null"));
+ }
+ }
+
+ @Override
+ public void onFailure(Call> call, Throwable throwable) {
+ onFailure(throwable);
+ }
+}
diff --git a/src/main/java/com/yutou/nas/okhttp/HttpLoggingInterceptor.java b/src/main/java/com/yutou/nas/okhttp/HttpLoggingInterceptor.java
new file mode 100644
index 0000000..610f50b
--- /dev/null
+++ b/src/main/java/com/yutou/nas/okhttp/HttpLoggingInterceptor.java
@@ -0,0 +1,209 @@
+package com.yutou.nas.okhttp;
+
+
+import com.yutou.nas.utils.Log;
+import okhttp3.*;
+import okhttp3.internal.http.HttpHeaders;
+import okio.Buffer;
+
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.util.concurrent.TimeUnit;
+import java.util.logging.Logger;
+
+public class HttpLoggingInterceptor implements Interceptor {
+ private static final String TAG = "HttpLogging";
+
+ private static final Charset UTF8 = Charset.forName("UTF-8");
+
+ private volatile Level printLevel = Level.NONE;
+ private java.util.logging.Level colorLevel;
+ private Logger logger;
+
+ private static boolean prLog;
+
+ public static void setLog(boolean log) {
+ prLog = log;
+ }
+
+ public enum Level {
+ NONE, //不打印log
+ BASIC, //只打印 请求首行 和 响应首行
+ HEADERS, //打印请求和响应的所有 Header
+ BODY //所有数据全部打印
+ }
+
+ public HttpLoggingInterceptor(String tag) {
+ logger = Logger.getLogger(tag);
+ colorLevel = java.util.logging.Level.INFO;
+ }
+
+ public void setPrintLevel(Level level) {
+ if (printLevel == null)
+ throw new NullPointerException("printLevel == null. Use Level.NONE instead.");
+ printLevel = level;
+ }
+
+ public void setColorLevel(java.util.logging.Level level) {
+ colorLevel = level;
+ }
+
+ private void log(String message) {
+ //logger.log(colorLevel, message);
+ if (prLog) {
+ Log.i(TAG, message);
+ }
+ //Log.e(TAG,message);
+ }
+
+ @Override
+ public Response intercept(Chain chain) throws IOException {
+ Request request = chain.request();
+ if (request.body() != null && request.body().contentLength() == 0) {
+ request = chain.call().request();
+ }
+ if (printLevel == Level.NONE) {
+ return chain.proceed(request);
+ }
+
+ //请求日志拦截
+ logForRequest(request, chain.connection());
+
+ //执行请求,计算请求时间
+ long startNs = System.nanoTime();
+ Response response;
+ try {
+ response = chain.proceed(request);
+ } catch (Exception e) {
+ log("<-- HTTP FAILED: " + e);
+ throw e;
+ }
+ long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);
+
+ //响应日志拦截
+ return logForResponse(response, tookMs);
+ }
+
+ private void logForRequest(Request request, Connection connection) throws IOException {
+ boolean logBody = (printLevel == Level.BODY);
+ boolean logHeaders = (printLevel == Level.BODY || printLevel == Level.HEADERS);
+ RequestBody requestBody = request.body();
+ boolean hasRequestBody = requestBody != null;
+ Protocol protocol = connection != null ? connection.protocol() : Protocol.HTTP_1_1;
+
+ try {
+ String requestStartMessage = "--> " + request.method() + ' ' + request.url() + ' ' + protocol;
+ log(requestStartMessage);
+
+ if (logHeaders) {
+ if (hasRequestBody) {
+ // Request body headers are only present when installed as a network interceptor. Force
+ // them to be included (when available) so there values are known.
+ if (requestBody.contentType() != null) {
+ log("\tContent-Type: " + requestBody.contentType());
+ }
+ if (requestBody.contentLength() != -1) {
+ log("\tContent-Length: " + requestBody.contentLength());
+ }
+ }
+ Headers headers = request.headers();
+ for (int i = 0, count = headers.size(); i < count; i++) {
+ String name = headers.name(i);
+ // Skip headers from the request body as they are explicitly logged above.
+ if (!"Content-Type".equalsIgnoreCase(name) && !"Content-Length".equalsIgnoreCase(name)) {
+ log("\t" + name + ": " + headers.value(i));
+ }
+ }
+
+ log(" ");
+ if (logBody && hasRequestBody) {
+ if (isPlaintext(requestBody.contentType())) {
+ bodyToString(request);
+ } else {
+ log("\tbody: maybe [binary body], omitted!");
+ }
+ }
+ }
+ } catch (Exception e) {
+ logger.log(java.util.logging.Level.WARNING, e.getMessage(), e);
+ } finally {
+ log("--> END " + request.method());
+ }
+ }
+
+ private Response logForResponse(Response response, long tookMs) {
+ Response.Builder builder = response.newBuilder();
+ Response clone = builder.build();
+ ResponseBody responseBody = clone.body();
+ boolean logBody = (printLevel == Level.BODY);
+ boolean logHeaders = (printLevel == Level.BODY || printLevel == Level.HEADERS);
+
+ try {
+ log("<-- " + clone.code() + ' ' + clone.message() + ' ' + clone.request().url() + " (" + tookMs + "ms)");
+ if (logHeaders) {
+ Headers headers = clone.headers();
+ for (int i = 0, count = headers.size(); i < count; i++) {
+ log("\t" + headers.name(i) + ": " + headers.value(i));
+ }
+ log(" ");
+ if (logBody && HttpHeaders.hasBody(clone)) {
+ if (responseBody == null) return response;
+
+ if (isPlaintext(responseBody.contentType())) {
+ byte[] bytes = responseBody.byteStream().readAllBytes();
+ MediaType contentType = responseBody.contentType();
+ String body = new String(bytes, getCharset(contentType));
+ log("\tbody:" + body);
+ responseBody = ResponseBody.create(responseBody.contentType(), bytes);
+ return response.newBuilder().body(responseBody).build();
+ } else {
+ log("\tbody: maybe [binary body], omitted!");
+ }
+ }
+ }
+ } catch (Exception e) {
+ logger.log(java.util.logging.Level.WARNING, e.getMessage(), e);
+ } finally {
+ log("<-- END HTTP");
+ }
+ return response;
+ }
+
+ private static Charset getCharset(MediaType contentType) {
+ Charset charset = contentType != null ? contentType.charset(UTF8) : UTF8;
+ if (charset == null) charset = UTF8;
+ return charset;
+ }
+
+ /**
+ * Returns true if the body in question probably contains human readable text. Uses a small sample
+ * of code points to detect unicode control characters commonly used in binary file signatures.
+ */
+ private static boolean isPlaintext(MediaType mediaType) {
+ if (mediaType == null) return false;
+ if (mediaType.type() != null && mediaType.type().equals("text")) {
+ return true;
+ }
+ String subtype = mediaType.subtype();
+ if (subtype != null) {
+ subtype = subtype.toLowerCase();
+ if (subtype.contains("x-www-form-urlencoded") || subtype.contains("json") || subtype.contains("xml") || subtype.contains("html")) //
+ return true;
+ }
+ return false;
+ }
+
+ private void bodyToString(Request request) {
+ try {
+ Request copy = request.newBuilder().build();
+ RequestBody body = copy.body();
+ if (body == null) return;
+ Buffer buffer = new Buffer();
+ body.writeTo(buffer);
+ Charset charset = getCharset(body.contentType());
+ log("\tbody:" + buffer.readString(charset));
+ } catch (Exception e) {
+ logger.log(java.util.logging.Level.WARNING, e.getMessage(), e);
+ }
+ }
+}
diff --git a/src/main/java/com/yutou/nas/okhttp/IRequestParam.java b/src/main/java/com/yutou/nas/okhttp/IRequestParam.java
new file mode 100644
index 0000000..81eff3f
--- /dev/null
+++ b/src/main/java/com/yutou/nas/okhttp/IRequestParam.java
@@ -0,0 +1,9 @@
+package com.yutou.nas.okhttp;
+
+import okhttp3.Request;
+
+import java.util.HashMap;
+
+public interface IRequestParam {
+ Request getRequest(HashMap map, Request request);
+}
\ No newline at end of file
diff --git a/src/main/java/com/yutou/nas/okhttp/ParamsContext.java b/src/main/java/com/yutou/nas/okhttp/ParamsContext.java
new file mode 100644
index 0000000..a256b64
--- /dev/null
+++ b/src/main/java/com/yutou/nas/okhttp/ParamsContext.java
@@ -0,0 +1,31 @@
+package com.yutou.nas.okhttp;
+
+import okhttp3.Request;
+
+import java.util.HashMap;
+
+public class ParamsContext {
+ private IRequestParam iRequestParam;
+ private Request request;
+ private HashMap map;
+
+ public ParamsContext(HashMap map,Request request) {
+ if(map==null){
+ map=new HashMap<>();
+ }
+ this.map=map;
+ this.request = request;
+ }
+
+ public Request getInRequest() {
+ switch (request.method()) {
+ case "GET":
+ iRequestParam = new GetRequestParams();
+ break;
+ case "POST":
+ iRequestParam = new PostRequestParams();
+ break;
+ }
+ return iRequestParam.getRequest(map,request);
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/yutou/nas/okhttp/PostRequestParams.java b/src/main/java/com/yutou/nas/okhttp/PostRequestParams.java
new file mode 100644
index 0000000..e1f3330
--- /dev/null
+++ b/src/main/java/com/yutou/nas/okhttp/PostRequestParams.java
@@ -0,0 +1,69 @@
+package com.yutou.nas.okhttp;
+
+
+import com.alibaba.fastjson2.JSONObject;
+import okhttp3.FormBody;
+import okhttp3.MediaType;
+import okhttp3.Request;
+import okhttp3.RequestBody;
+
+import java.net.URLEncoder;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+public class PostRequestParams implements IRequestParam {
+ @Override
+ public Request getRequest(HashMap map, Request request) {
+ if (request.body() instanceof FormBody) {
+ FormBody.Builder bodyBuilder = new FormBody.Builder();
+ FormBody formBody = (FormBody) request.body();
+
+ for (int i = 0; i < formBody.size(); i++) {
+ bodyBuilder.addEncoded(formBody.encodedName(i), formBody.encodedValue(i));
+ }
+ for (String key : map.keySet()) {
+ bodyBuilder.addEncoded(key, String.valueOf(map.get(key)));
+ }
+ formBody = bodyBuilder.build();
+ request = request.newBuilder().post(formBody).build();
+ } else if (request.body() != null) {
+ RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),toUrlParams(map));
+ request = request.newBuilder().post(request.body())
+ .post(requestBody).build();
+ }
+ return request;
+ }
+
+ public static String toUrlParams(JSONObject json) {
+ StringBuilder string = new StringBuilder();
+ Set keys = json.keySet();
+ for (String key : keys) {
+ try {
+ string.append("&").append(key).append("=").append(URLEncoder.encode(json.getString(key), "UTF-8"));
+ } catch (Exception e) {
+ e.printStackTrace();
+ try {
+ string.append("&").append(URLEncoder.encode(key,"UTF-8")).append("=");
+ // string += "&" + key + "=";
+ } catch (Exception e1) {
+ string.append("&").append(key).append("=");
+ }
+ }
+ }
+
+ string = new StringBuilder(string.substring(1, string.length()).replaceAll(" ", ""));
+ return string.toString();
+ }
+
+ public static String toUrlParams(Map map) {
+ if(map.isEmpty()){
+ return "";
+ }
+ StringBuilder builder = new StringBuilder();
+ for (String key : map.keySet()) {
+ builder.append(key).append("=").append(map.get(key)).append("&");
+ }
+ return builder.substring(0, builder.length() - 1);
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/yutou/nas/okhttp/api/BaseApi.java b/src/main/java/com/yutou/nas/okhttp/api/BaseApi.java
new file mode 100644
index 0000000..4c7c975
--- /dev/null
+++ b/src/main/java/com/yutou/nas/okhttp/api/BaseApi.java
@@ -0,0 +1,101 @@
+package com.yutou.nas.okhttp.api;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.internal.bind.DateTypeAdapter;
+import com.yutou.nas.okhttp.HttpLoggingInterceptor;
+import com.yutou.nas.okhttp.ParamsContext;
+import com.yutou.nas.okhttp.converter.JsonCallAdapter;
+import com.yutou.nas.okhttp.converter.JsonConverterFactory;
+import okhttp3.Interceptor;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.Response;
+import retrofit2.CallAdapter;
+import retrofit2.Converter;
+import retrofit2.Retrofit;
+
+import java.io.IOException;
+import java.util.Date;
+import java.util.HashMap;
+
+public class BaseApi {
+ private String URL;
+ private HashMap params;
+
+ public BaseApi(String URL) {
+ this.URL = URL;
+ }
+
+ public BaseApi setURL(String URL) {
+ this.URL = URL;
+ return this;
+ }
+
+ public BaseApi setParams(HashMap params) {
+ this.params = params;
+ return this;
+ }
+
+ /**
+ * 创建一个接口方法
+ *
+ * @param okHttpClient okhttp客户端
+ * @param converterFactory 处理工厂类
+ * @param callAdapterFactory 请求适配器工厂
+ * @param baseUrl 基础地质
+ * @param service 接口
+ * @param 接口泛型
+ * @return 接口
+ */
+ public T create(OkHttpClient okHttpClient, Converter.Factory converterFactory, CallAdapter.Factory callAdapterFactory, String baseUrl, Class service) {
+ Retrofit.Builder builder = new Retrofit.Builder()
+ //基础url
+ .baseUrl(baseUrl)
+ //客户端OKHttp
+ .client(okHttpClient);
+ //添加转换工厂
+ if (null != converterFactory) {
+ builder.addConverterFactory(converterFactory);
+ }
+ //添加请求工厂
+ if (null != callAdapterFactory) {
+ builder.addCallAdapterFactory(callAdapterFactory);
+ }
+ //创建retrofit对象
+ Retrofit retrofit = builder.build();
+
+ //返回创建的api
+ return retrofit.create(service);
+ }
+
+ public T createApi(Class apiClass) {
+ Gson gson = new GsonBuilder()
+ .registerTypeAdapter(Date.class, new DateTypeAdapter())
+ .create();
+ HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("http");
+ loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);
+ OkHttpClient.Builder builder = new OkHttpClient()
+ .newBuilder()
+
+ .addInterceptor(initQuery())
+ .addInterceptor(loggingInterceptor);
+ return create(builder.build(),
+ JsonConverterFactory.create(gson),
+ JsonCallAdapter.create(),
+ URL,
+ apiClass);
+ }
+ public Interceptor initQuery() {
+ Interceptor addQueryParameterInterceptor = new Interceptor() {
+ @Override
+ public Response intercept(Chain chain) throws IOException {
+ Request request = chain.request();
+ //配置公共参数
+ request = new ParamsContext(params,request).getInRequest();
+ return chain.proceed(request);
+ }
+ };
+ return addQueryParameterInterceptor;
+ }
+}
diff --git a/src/main/java/com/yutou/nas/okhttp/api/ToolsNetApi.java b/src/main/java/com/yutou/nas/okhttp/api/ToolsNetApi.java
new file mode 100644
index 0000000..6a90852
--- /dev/null
+++ b/src/main/java/com/yutou/nas/okhttp/api/ToolsNetApi.java
@@ -0,0 +1,14 @@
+package com.yutou.nas.okhttp.api;
+
+import com.yutou.nas.okhttp.BaseBean;
+import com.yutou.nas.okhttp.HttpBody;
+import retrofit2.Call;
+import retrofit2.http.Field;
+import retrofit2.http.FormUrlEncoded;
+import retrofit2.http.POST;
+
+public interface ToolsNetApi {
+ @FormUrlEncoded
+ @POST("/api/v2/torrents/add")
+ Call> postDownloadBt(@Field("urls")String urls, @Field("savePath")String savePath);
+}
diff --git a/src/main/java/com/yutou/nas/okhttp/api/ToolsNetManager.java b/src/main/java/com/yutou/nas/okhttp/api/ToolsNetManager.java
new file mode 100644
index 0000000..dcc464d
--- /dev/null
+++ b/src/main/java/com/yutou/nas/okhttp/api/ToolsNetManager.java
@@ -0,0 +1,17 @@
+package com.yutou.nas.okhttp.api;
+
+import com.yutou.nas.utils.ConfigTools;
+
+public class ToolsNetManager extends BaseApi {
+ public ToolsNetManager(String URL) {
+ super(URL);
+ }
+
+ public static ToolsNetManager createQbit() {
+ return new ToolsNetManager(ConfigTools.load(ConfigTools.CONFIG, "Qbittorrent.Url", String.class));
+ }
+
+ public ToolsNetApi getToolsNetApi() {
+ return createApi(ToolsNetApi.class);
+ }
+}
diff --git a/src/main/java/com/yutou/nas/okhttp/converter/JsonCallAdapter.java b/src/main/java/com/yutou/nas/okhttp/converter/JsonCallAdapter.java
new file mode 100644
index 0000000..1748fb7
--- /dev/null
+++ b/src/main/java/com/yutou/nas/okhttp/converter/JsonCallAdapter.java
@@ -0,0 +1,19 @@
+package com.yutou.nas.okhttp.converter;
+
+import org.jetbrains.annotations.Nullable;
+import retrofit2.CallAdapter;
+import retrofit2.Retrofit;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+
+public class JsonCallAdapter extends CallAdapter.Factory{
+ public static JsonCallAdapter create(){
+ return new JsonCallAdapter();
+ }
+ @Nullable
+ @Override
+ public CallAdapter, ?> get(Type type, Annotation[] annotations, Retrofit retrofit) {
+ return null;
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/yutou/nas/okhttp/converter/JsonConverterFactory.java b/src/main/java/com/yutou/nas/okhttp/converter/JsonConverterFactory.java
new file mode 100644
index 0000000..d88cf4a
--- /dev/null
+++ b/src/main/java/com/yutou/nas/okhttp/converter/JsonConverterFactory.java
@@ -0,0 +1,40 @@
+package com.yutou.nas.okhttp.converter;
+
+import com.google.gson.Gson;
+import com.google.gson.TypeAdapter;
+import com.google.gson.reflect.TypeToken;
+import okhttp3.RequestBody;
+import okhttp3.ResponseBody;
+import org.jetbrains.annotations.Nullable;
+import retrofit2.Converter;
+import retrofit2.Retrofit;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+
+public class JsonConverterFactory extends Converter.Factory {
+ Gson gson;
+ public static JsonConverterFactory create(Gson gson) {
+ return new JsonConverterFactory(gson);
+ }
+
+ private JsonConverterFactory(Gson gson) {
+ this.gson = gson;
+ }
+
+ @Nullable
+ @Override
+ public Converter, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
+ // return super.requestBodyConverter(type, parameterAnnotations, methodAnnotations, retrofit);
+ TypeAdapter> adapter = gson.getAdapter(TypeToken.get(type));
+ return new JsonRequestBodyConverter<>(gson,adapter);
+ }
+
+ @Nullable
+ @Override
+ public Converter responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
+ // return super.responseBodyConverter(type, annotations, retrofit);
+ TypeAdapter> adapter = gson.getAdapter(TypeToken.get(type));
+ return new JsonResponseBodyConverter<>(gson,adapter,type);
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/yutou/nas/okhttp/converter/JsonRequestBodyConverter.java b/src/main/java/com/yutou/nas/okhttp/converter/JsonRequestBodyConverter.java
new file mode 100644
index 0000000..610f547
--- /dev/null
+++ b/src/main/java/com/yutou/nas/okhttp/converter/JsonRequestBodyConverter.java
@@ -0,0 +1,36 @@
+package com.yutou.nas.okhttp.converter;
+
+import com.google.gson.Gson;
+import com.google.gson.TypeAdapter;
+import com.google.gson.stream.JsonWriter;
+import okhttp3.MediaType;
+import okhttp3.RequestBody;
+import okio.Buffer;
+import org.jetbrains.annotations.Nullable;
+import retrofit2.Converter;
+
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+import java.nio.charset.StandardCharsets;
+
+public class JsonRequestBodyConverter implements Converter {
+ Gson gson;
+ TypeAdapter adapter;
+ public JsonRequestBodyConverter(Gson gson, TypeAdapter adapter) {
+ this.gson=gson;
+ this.adapter=adapter;
+ }
+
+ @Nullable
+ @Override
+ public RequestBody convert(T value) throws IOException {
+ Buffer buffer = new Buffer();
+ Writer writer = new OutputStreamWriter(buffer.outputStream(), StandardCharsets.UTF_8);
+ JsonWriter jsonWriter = gson.newJsonWriter(writer);
+ adapter.write(jsonWriter, value);
+ jsonWriter.close();
+ byte[] bytes = buffer.readByteArray();
+ return RequestBody.create(MediaType.parse("application/json; charset=UTF-8"),bytes );
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/yutou/nas/okhttp/converter/JsonResponseBodyConverter.java b/src/main/java/com/yutou/nas/okhttp/converter/JsonResponseBodyConverter.java
new file mode 100644
index 0000000..16ee310
--- /dev/null
+++ b/src/main/java/com/yutou/nas/okhttp/converter/JsonResponseBodyConverter.java
@@ -0,0 +1,42 @@
+package com.yutou.nas.okhttp.converter;
+
+import com.alibaba.fastjson2.JSONObject;
+import com.google.gson.Gson;
+import com.google.gson.TypeAdapter;
+import com.yutou.nas.okhttp.HttpBody;
+import okhttp3.ResponseBody;
+import org.jetbrains.annotations.Nullable;
+import retrofit2.Converter;
+
+import java.io.IOException;
+import java.lang.reflect.Type;
+
+public class JsonResponseBodyConverter implements Converter {
+ Gson gson;
+ TypeAdapter> adapter;
+ Type type;
+
+ public JsonResponseBodyConverter(Gson gson, TypeAdapter> adapter, Type type) {
+ this.gson = gson;
+ this.adapter = adapter;
+ this.type = type;
+ }
+
+ @Nullable
+ @Override
+ public T convert(ResponseBody responseBody) throws IOException {
+ String string = new String(responseBody.bytes());
+ responseBody.close();
+ HttpBody body;
+ try {
+ body = JSONObject.parseObject(string, type);
+ body.setSrc(string);
+ return (T) body;
+ } catch (Exception e) {
+ body = new HttpBody();
+ body.setSrc(string);
+ }
+ return (T) body;
+
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/yutou/nas/utils/BTDownloadManager.java b/src/main/java/com/yutou/nas/utils/BTDownloadManager.java
index 0b1c3b9..06b3e4f 100644
--- a/src/main/java/com/yutou/nas/utils/BTDownloadManager.java
+++ b/src/main/java/com/yutou/nas/utils/BTDownloadManager.java
@@ -9,11 +9,18 @@ import com.acgist.snail.protocol.magnet.MagnetProtocol;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
+import com.yutou.nas.okhttp.BaseBean;
+import com.yutou.nas.okhttp.HttpBody;
+import com.yutou.nas.okhttp.api.ToolsNetApi;
+import com.yutou.nas.okhttp.api.ToolsNetManager;
import org.slf4j.LoggerFactory;
import org.slf4j.event.Level;
+import retrofit2.Response;
import java.io.File;
import java.io.IOException;
+import java.net.URLEncoder;
+import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
@@ -91,17 +98,9 @@ public class BTDownloadManager {
, "-c"
, exec});
process.waitFor(120, TimeUnit.SECONDS);*/
- JSONObject json = new JSONObject();
- json.put("urls", url);
- json.put("savepath", "anim/" + title);
- String data="urls="+url+"&savepath=anim/"+title;
- String post = HttpTools.http_post_form(
- ConfigTools.load(ConfigTools.CONFIG, "Qbittorrent.Url", String.class) + "/api/v2/torrents/add",
- data.getBytes(StandardCharsets.UTF_8),
- 0,
- null
- );
+ Response> response = ToolsNetManager.createQbit().getToolsNetApi().postDownloadBt(url, "anim/" + title).execute();
+ String post = response.body().getSrc();
Log.i("BT下载", post);
return post.toLowerCase().contains("ok");
} catch (Exception e) {
diff --git a/src/main/java/com/yutou/nas/utils/HttpTools.java b/src/main/java/com/yutou/nas/utils/HttpTools.java
index 703d496..540b160 100644
--- a/src/main/java/com/yutou/nas/utils/HttpTools.java
+++ b/src/main/java/com/yutou/nas/utils/HttpTools.java
@@ -127,6 +127,7 @@ public class HttpTools {
if (body == null) {
body = "".getBytes();
}
+ Log.i("Http","url = "+url+" body = "+new String(body));
connection.setDoOutput(true);
connection.setDoInput(true);
connection.addRequestProperty("User-Agent", getUa());
@@ -150,13 +151,14 @@ public class HttpTools {
str.append(tmp);
}
String finalStr = str.toString();
-
+ Log.i("Http","code = "+connection.getResponseCode()+" url = "+url+" body = "+new String(body));
connection.disconnect();
reader.close();
return finalStr;
} catch (Exception e) {
+ e.printStackTrace();
if (index < HttpRequestIndex) {
- return http_post(url, body, index + 1, headers);
+ return http_post_form(url, body, index + 1, headers);
} else {
e.printStackTrace();
return null;