update gpt

This commit is contained in:
yutou 2023-09-20 22:21:09 +08:00
parent c48db62d00
commit 126f25af81
2 changed files with 26 additions and 21 deletions

View File

@ -60,8 +60,12 @@ public class BaiduGPTManager {
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put("messages", messages); json.put("messages", messages);
System.out.println("json = " + json); System.out.println("json = " + json);
String post = HttpTools.post(url + "?access_token=" + getToken()
, json.toJSONString().getBytes(StandardCharsets.UTF_8)); Map<String, String> map = new HashMap<>();
map.put("Content-Type", "application/json");
map.put("Content-Length", String.valueOf(json.toJSONString().getBytes(StandardCharsets.UTF_8).length));
String post = HttpTools.http_post(url + "?access_token=" + getToken()
, json.toJSONString().getBytes(StandardCharsets.UTF_8),0,map);
System.out.println("post = " + post); System.out.println("post = " + post);
if (StringUtils.isEmpty(post)) { if (StringUtils.isEmpty(post)) {
clear(); clear();

View File

@ -17,15 +17,15 @@ public class HttpTools {
private static final int HttpRequestIndex = 3; private static final int HttpRequestIndex = 3;
public static String get(String url) { public static String get(String url) {
return http_get(url, null,false); return http_get(url, null, false);
} }
public static String post(final String url, final byte[] body) { public static String post(final String url, final byte[] body) {
return http_post(url, body, 0, null); return http_post(url, body, 0, null);
} }
public static File syncDownload(final String url, final String saveName,boolean isProxy) { public static File syncDownload(final String url, final String saveName, boolean isProxy) {
return new HttpTools().http_syncDownload(url, saveName,isProxy); return new HttpTools().http_syncDownload(url, saveName, isProxy);
} }
public static String https_get(String url, Map<String, String> header) { public static String https_get(String url, Map<String, String> header) {
@ -40,7 +40,7 @@ public class HttpTools {
return null; return null;
} }
public static String http_get(String url, Map<String, String> header,boolean isProxy) { public static String http_get(String url, Map<String, String> header, boolean isProxy) {
try { try {
HttpURLConnection connection; HttpURLConnection connection;
Proxy proxy = null; Proxy proxy = null;
@ -48,9 +48,9 @@ public class HttpTools {
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890)); proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890));
} }
if(proxy==null) { if (proxy == null) {
connection = (HttpURLConnection) new URL(url).openConnection(); connection = (HttpURLConnection) new URL(url).openConnection();
}else{ } else {
connection = (HttpURLConnection) new URL(url).openConnection(proxy); connection = (HttpURLConnection) new URL(url).openConnection(proxy);
} }
return urlConnection(header, connection); return urlConnection(header, connection);
@ -95,8 +95,8 @@ public class HttpTools {
connection.setDoOutput(true); connection.setDoOutput(true);
connection.setDoInput(true); connection.setDoInput(true);
connection.addRequestProperty("User-Agent", getExtUa()); connection.addRequestProperty("User-Agent", getExtUa());
// connection.setConnectTimeout(5 * 1000); // connection.setConnectTimeout(5 * 1000);
// connection.setReadTimeout(10 * 1000); // connection.setReadTimeout(10 * 1000);
//connection.addRequestProperty("Connection", "keep-alive"); //connection.addRequestProperty("Connection", "keep-alive");
//connection.addRequestProperty("User-Agent", getExtUa()); //connection.addRequestProperty("User-Agent", getExtUa());
//connection.addRequestProperty("content-type", "application/json"); //connection.addRequestProperty("content-type", "application/json");
@ -119,10 +119,10 @@ public class HttpTools {
reader.close(); reader.close();
return finalStr; return finalStr;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
if (index < HttpRequestIndex) { if (index < HttpRequestIndex) {
return http_post(url, body, index + 1, headers); return http_post(url, body, index + 1, headers);
} else { } else {
e.printStackTrace();
return null; return null;
} }
} }
@ -153,8 +153,9 @@ public class HttpTools {
string = new StringBuilder(string.substring(1, string.length()).replaceAll(" ", "")); string = new StringBuilder(string.substring(1, string.length()).replaceAll(" ", ""));
return string.toString(); return string.toString();
} }
public static String toUrlParams(Map<String,String> map) {
StringBuilder builder=new StringBuilder(); public static String toUrlParams(Map<String, String> map) {
StringBuilder builder = new StringBuilder();
for (String key : map.keySet()) { for (String key : map.keySet()) {
builder.append(key).append("=").append(map.get(key)).append("&"); builder.append(key).append("=").append(map.get(key)).append("&");
} }
@ -174,7 +175,7 @@ public class HttpTools {
} }
public static void main(String[] args) { public static void main(String[] args) {
File file = syncDownload("https://lain.bgm.tv/pic/cover/l/6c/2a/302128_qQIjG.jpg", "12345.jpg",false); File file = syncDownload("https://lain.bgm.tv/pic/cover/l/6c/2a/302128_qQIjG.jpg", "12345.jpg", false);
System.out.println("file.length() = " + file.length()); System.out.println("file.length() = " + file.length());
} }
@ -246,8 +247,8 @@ public class HttpTools {
if (downloadInterface != null) { if (downloadInterface != null) {
if (oldJar.exists()) { if (oldJar.exists()) {
downloadInterface.onDownload(oldJar); downloadInterface.onDownload(oldJar);
}else{ } else {
downloadInterface.onError(new FileNotFoundException("文件下载失败, 网络大小 = "+fileSize+" 本地大小 = "+oldJar.length())); downloadInterface.onError(new FileNotFoundException("文件下载失败, 网络大小 = " + fileSize + " 本地大小 = " + oldJar.length()));
} }
} }
@ -265,7 +266,7 @@ public class HttpTools {
}).start(); }).start();
} }
public synchronized File http_syncDownload(final String url, final String saveName,boolean isProxy) { public synchronized File http_syncDownload(final String url, final String saveName, boolean isProxy) {
if (StringUtils.isEmpty(url)) { if (StringUtils.isEmpty(url)) {
return null; return null;
} }
@ -282,10 +283,10 @@ public class HttpTools {
Log.i("DOWNLOAD", "下载文件:" + url + " 保存文件:" + saveName); Log.i("DOWNLOAD", "下载文件:" + url + " 保存文件:" + saveName);
HttpURLConnection connection; HttpURLConnection connection;
if(proxy==null) { if (proxy == null) {
connection = (HttpURLConnection) new URL(url).openConnection(); connection = (HttpURLConnection) new URL(url).openConnection();
}else{ } else {
connection = (HttpURLConnection) new URL(url).openConnection(proxy); connection = (HttpURLConnection) new URL(url).openConnection(proxy);
} }
connection.addRequestProperty("User-Agent", getExtUa()); connection.addRequestProperty("User-Agent", getExtUa());
// Log.i(TAG,"获取到网络请求:"+connection.getResponseCode()); // Log.i(TAG,"获取到网络请求:"+connection.getResponseCode());