From 4dad758a17f2ed207810d8a06e9958e3666f1d46 Mon Sep 17 00:00:00 2001 From: onlyxx <178620086@qq.com> Date: Thu, 7 Mar 2024 10:55:46 +0800 Subject: [PATCH] add httpkit upload response --- .../design/work/ConnectionComboBoxPanel.java | 6 ++++- .../com/fanruan/api/net/http/HttpKit.java | 23 ++++++++++--------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/fanruan/api/design/work/ConnectionComboBoxPanel.java b/src/main/java/com/fanruan/api/design/work/ConnectionComboBoxPanel.java index 4846c13..0f8e853 100644 --- a/src/main/java/com/fanruan/api/design/work/ConnectionComboBoxPanel.java +++ b/src/main/java/com/fanruan/api/design/work/ConnectionComboBoxPanel.java @@ -130,7 +130,11 @@ public class ConnectionComboBoxPanel extends ItemEditableComboBoxPanel { ConfigurationKit.modify(ConnectionConfig.class, new Runner() { @Override public void run() { - connectionListPane.update(connectionConfig); + try { + connectionListPane.update(connectionConfig); + } catch (Exception e) { + throw new RuntimeException(e); + } } }, new WorkerCallBack() { @Override diff --git a/src/main/java/com/fanruan/api/net/http/HttpKit.java b/src/main/java/com/fanruan/api/net/http/HttpKit.java index 57ae033..034fe11 100644 --- a/src/main/java/com/fanruan/api/net/http/HttpKit.java +++ b/src/main/java/com/fanruan/api/net/http/HttpKit.java @@ -44,6 +44,7 @@ import java.net.URISyntaxException; import java.net.URLEncoder; import java.net.UnknownHostException; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.*; import static com.fanruan.api.net.http.rs.HttpRequestType.POST; @@ -193,12 +194,12 @@ public class HttpKit { /** * 发起POST JSON请求并获取返回的文本 - * + * 以前版本只支持jsonObject 修改为object就可以支持多种json格式 * @param url 响应请求的的服务器地址 * @param params POST请求的参数 * @return 服务器返回的文本内容 */ - public static String postJSON(String url, JSONObject params, Map headers) throws IOException { + public static String postJSON(String url, Object params, Map headers) throws IOException { StringEntity jsonEntity = new StringEntity(params.toString(), "UTF-8"); jsonEntity.setContentEncoding("UTF-8"); if (headers != null) { @@ -484,8 +485,8 @@ public class HttpKit { * @param file 要上传的文件,默认的文件编码为utf-8 * @throws IOException 上传中出现错误则抛出此异常 */ - public static void upload(String url, File file) throws IOException { - upload(url, file, Charset.forName("utf-8")); + public static String upload(String url, File file) throws IOException { + return upload(url, file, StandardCharsets.UTF_8); } /** @@ -496,8 +497,8 @@ public class HttpKit { * @param charset 文件的编码 * @throws IOException 上传中出现错误则抛出此异常 */ - public static void upload(String url, File file, Charset charset) throws IOException { - upload(url, new FileEntity(file), charset); + public static String upload(String url, File file, Charset charset) throws IOException { + return upload(url, new FileEntity(file), charset); } /** @@ -520,8 +521,8 @@ public class HttpKit { * @param charset 文件的编码 * @throws IOException 上传中出现错误则抛出此异常 */ - public static void upload(String url, FileEntity fileEntity, Charset charset) throws IOException { - upload(url, fileEntity, charset, Collections.emptyMap(), POST); + public static String upload(String url, FileEntity fileEntity, Charset charset) throws IOException { + return upload(url, fileEntity, charset, Collections.emptyMap(), POST); } /** @@ -551,8 +552,8 @@ public class HttpKit { * @param httpRequestType 请求类型 * @throws IOException 上传中出现错误则抛出此异常 */ - public static void upload(String url, HttpEntity reqEntity, Charset charset, Map headers, HttpRequestType httpRequestType) throws IOException { - executeAndParse(HttpRequest + public static String upload(String url, HttpEntity reqEntity, Charset charset, Map headers, HttpRequestType httpRequestType) throws IOException { + return executeAndParse(HttpRequest .custom() .url(url) .headers(headers) @@ -560,7 +561,7 @@ public class HttpKit { .httpEntity(reqEntity) .encoding(charset.toString()) .build(), - UploadResponseHandle.DEFAULT); + new TextResponseHandle(charset.toString())); } /**