|
|
@@ -26,7 +26,7 @@ public class api {
|
|
|
// private static String host = "http://192.168.12.139:10001/transfer/mobile"; //本机网关
|
|
|
// private static String host = "http://192.168.12.139:10004/mobile"; // 本机中转
|
|
|
|
|
|
- private static String host = "https://gatewayservicev2dev.fujica.com.cn/transfer/mobile"; //UAT环境
|
|
|
+ private static String host = "https://gatewayservicev2dev.fujica.com.cn/transfer/mobile"; //UAT环境
|
|
|
// private static String host = "https://gatewayservicev2test.fujica.com.cn/transfer/mobile"; //PRE环境
|
|
|
|
|
|
// 前端nginx连接
|
|
|
@@ -113,7 +113,7 @@ public class api {
|
|
|
// 如果需要特定类型,调用方需要自己转换
|
|
|
result.setData((T) jsonObj.get("data"));
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return result;
|
|
|
} catch (Exception e) {
|
|
|
Log.error("JSON解析失败: " + e.getMessage());
|
|
|
@@ -130,37 +130,36 @@ public class api {
|
|
|
if (failToast == null) {
|
|
|
failToast = true;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 显示Loading:默认显示,只有当hideLoading!=null && hideLoading==false时才不显示
|
|
|
final boolean shouldShowLoading = !(hideLoading != null && hideLoading);
|
|
|
if (shouldShowLoading) {
|
|
|
LoadingDialog.show(context);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
url = mergeUrl(url, method, data);
|
|
|
Log.info("请求" + url);
|
|
|
String token = cache.getToken(context);
|
|
|
final String _url = url;
|
|
|
final Boolean _failToast = failToast;
|
|
|
- final boolean _shouldShowLoading = shouldShowLoading;
|
|
|
return CompletableFuture.supplyAsync(() -> {
|
|
|
HttpURLConnection connection = null;
|
|
|
try {
|
|
|
URL requestUrl = new URL(_url);
|
|
|
connection = (HttpURLConnection) requestUrl.openConnection();
|
|
|
-
|
|
|
+
|
|
|
// 设置请求方法
|
|
|
connection.setRequestMethod(method);
|
|
|
-
|
|
|
+
|
|
|
// 设置请求头
|
|
|
connection.setRequestProperty("Accept", "application/json");
|
|
|
connection.setRequestProperty("Content-Type", "application/json");
|
|
|
connection.setRequestProperty("token", token);
|
|
|
-
|
|
|
+
|
|
|
// 设置超时
|
|
|
connection.setConnectTimeout(10000);
|
|
|
connection.setReadTimeout(60000);
|
|
|
-
|
|
|
+
|
|
|
// 设置请求体(非GET请求)
|
|
|
if (!"GET".equals(method) && data != null) {
|
|
|
connection.setDoOutput(true);
|
|
|
@@ -170,14 +169,14 @@ public class api {
|
|
|
os.flush();
|
|
|
os.close();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 执行请求
|
|
|
int responseCode = connection.getResponseCode();
|
|
|
-
|
|
|
+
|
|
|
try {
|
|
|
R<T> result;
|
|
|
if (responseCode == 401) {
|
|
|
- Toast.error(context,"登录过期,请重试");
|
|
|
+ Toast.error(context, "登录过期,请重试");
|
|
|
cache.logout(context);
|
|
|
EventBus.getInstance().emit("onAuthChanged");
|
|
|
result = new R<>("登录过期,请重试");
|
|
|
@@ -197,66 +196,49 @@ public class api {
|
|
|
}
|
|
|
reader.close();
|
|
|
inputStream.close();
|
|
|
-
|
|
|
+
|
|
|
String resultStr = response.toString();
|
|
|
Log.error("响应:" + resultStr);
|
|
|
result = parseResponse(resultStr);
|
|
|
-
|
|
|
+
|
|
|
if (result != null && !result.isSuccess() && _failToast) {
|
|
|
String errorMsg = result.getMsg() != null ? result.getMsg() : "操作失败" + result.getCode();
|
|
|
- Toast.error(context,errorMsg);
|
|
|
+ Toast.error(context, errorMsg);
|
|
|
}
|
|
|
Log.info("响应:" + resultStr);
|
|
|
if (result == null) {
|
|
|
result = new R<>("解析响应失败");
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- // 隐藏Loading
|
|
|
- if (_shouldShowLoading) {
|
|
|
- LoadingDialog.hide();
|
|
|
- }
|
|
|
-
|
|
|
+
|
|
|
return result;
|
|
|
} catch (Exception e) {
|
|
|
String msg = "操作失败" + e.getMessage();
|
|
|
- Toast.error(context,msg);
|
|
|
+ Toast.error(context, msg);
|
|
|
Log.error("响应解析错误: " + e.getMessage());
|
|
|
-
|
|
|
- // 隐藏Loading
|
|
|
- if (_shouldShowLoading) {
|
|
|
- LoadingDialog.hide();
|
|
|
- }
|
|
|
-
|
|
|
+
|
|
|
return new R<>(msg);
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
- String msg = (e.getMessage() != null && (e.getMessage().contains("2300006") || e.getMessage().contains("2300007")))
|
|
|
+ String msg = (e.getMessage() != null && (e.getMessage().contains("2300006") || e.getMessage().contains("2300007")))
|
|
|
? "网络连接失败" : e.getMessage() + ":" + e.getClass().getSimpleName();
|
|
|
- Toast.error(context,msg);
|
|
|
+ Toast.error(context, msg);
|
|
|
Log.error("请求错误: " + e.getMessage());
|
|
|
-
|
|
|
- // 隐藏Loading
|
|
|
- if (_shouldShowLoading) {
|
|
|
- LoadingDialog.hide();
|
|
|
- }
|
|
|
-
|
|
|
+
|
|
|
return new R<>(msg);
|
|
|
} catch (Exception e) {
|
|
|
String msg = "操作失败" + e.getMessage();
|
|
|
- Toast.error(context,msg);
|
|
|
+ Toast.error(context, msg);
|
|
|
Log.error("请求异常: " + e.getMessage());
|
|
|
-
|
|
|
- // 隐藏Loading
|
|
|
- if (_shouldShowLoading) {
|
|
|
- LoadingDialog.hide();
|
|
|
- }
|
|
|
-
|
|
|
+
|
|
|
return new R<>(msg);
|
|
|
} finally {
|
|
|
if (connection != null) {
|
|
|
connection.disconnect();
|
|
|
}
|
|
|
+ if (shouldShowLoading) {
|
|
|
+// LoadingDialog.hide();
|
|
|
+ }
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
@@ -286,7 +268,7 @@ public class api {
|
|
|
* GET请求(带选项)
|
|
|
*/
|
|
|
public static <T> CompletableFuture<R<T>> get1(Context context, ApiOption option) {
|
|
|
- return http(context,option.getUrl(), "GET", option.getData(), option.getToast(), option.getHideLoading());
|
|
|
+ return http(context, option.getUrl(), "GET", option.getData(), option.getToast(), option.getHideLoading());
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -314,17 +296,17 @@ public class api {
|
|
|
try {
|
|
|
URL requestUrl = new URL(finalUrl);
|
|
|
connection = (HttpURLConnection) requestUrl.openConnection();
|
|
|
-
|
|
|
+
|
|
|
// 设置请求方法
|
|
|
connection.setRequestMethod("POST");
|
|
|
-
|
|
|
+
|
|
|
// 设置请求头
|
|
|
String boundary = "----WebKitFormBoundary" + System.currentTimeMillis();
|
|
|
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
|
|
|
connection.setRequestProperty("Connection", "keep-alive");
|
|
|
connection.setRequestProperty("token", token);
|
|
|
connection.setDoOutput(true);
|
|
|
-
|
|
|
+
|
|
|
// 构建multipart/form-data请求体
|
|
|
OutputStream os = connection.getOutputStream();
|
|
|
java.io.File file = new java.io.File(imageUri);
|
|
|
@@ -333,7 +315,7 @@ public class api {
|
|
|
os.write(("--" + boundary + "\r\n").getBytes("UTF-8"));
|
|
|
os.write(("Content-Disposition: form-data; name=\"file\"; filename=\"file.jpg\"\r\n").getBytes("UTF-8"));
|
|
|
os.write(("Content-Type: image/jpg\r\n\r\n").getBytes("UTF-8"));
|
|
|
-
|
|
|
+
|
|
|
// 读取并写入文件内容
|
|
|
java.io.FileInputStream fis = new java.io.FileInputStream(file);
|
|
|
byte[] buffer = new byte[8192];
|
|
|
@@ -342,21 +324,21 @@ public class api {
|
|
|
os.write(buffer, 0, bytesRead);
|
|
|
}
|
|
|
fis.close();
|
|
|
-
|
|
|
+
|
|
|
// 结束multipart
|
|
|
os.write(("\r\n--" + boundary + "--\r\n").getBytes("UTF-8"));
|
|
|
}
|
|
|
os.flush();
|
|
|
os.close();
|
|
|
-
|
|
|
+
|
|
|
// 执行请求
|
|
|
int responseCode = connection.getResponseCode();
|
|
|
-
|
|
|
+
|
|
|
try {
|
|
|
R<T> result;
|
|
|
if (responseCode != 200) {
|
|
|
String msg = "操作失败:" + responseCode;
|
|
|
- Toast.error(context,msg);
|
|
|
+ Toast.error(context, msg);
|
|
|
Log.info("响应:" + responseCode);
|
|
|
result = new R<>(msg);
|
|
|
} else {
|
|
|
@@ -370,51 +352,50 @@ public class api {
|
|
|
}
|
|
|
reader.close();
|
|
|
inputStream.close();
|
|
|
-
|
|
|
+
|
|
|
String resultStr = response.toString();
|
|
|
result = parseResponse(resultStr);
|
|
|
-
|
|
|
+
|
|
|
if (result != null && !result.isSuccess()) {
|
|
|
String errorMsg = result.getMsg() != null ? result.getMsg() : "操作失败" + result.getCode();
|
|
|
- Toast.error(context,errorMsg);
|
|
|
+ Toast.error(context, errorMsg);
|
|
|
}
|
|
|
Log.info("响应:" + resultStr);
|
|
|
if (result == null) {
|
|
|
result = new R<>("解析响应失败");
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 隐藏Loading
|
|
|
LoadingDialog.hide();
|
|
|
-
|
|
|
+
|
|
|
return result;
|
|
|
} catch (Exception e) {
|
|
|
String msg = "操作失败" + e.getMessage();
|
|
|
- Toast.error(context,msg);
|
|
|
+ Toast.error(context, msg);
|
|
|
Log.error("响应解析错误: " + e.getMessage());
|
|
|
-
|
|
|
+
|
|
|
// 隐藏Loading
|
|
|
LoadingDialog.hide();
|
|
|
-
|
|
|
+
|
|
|
return new R<>(msg);
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
String msg = e.getMessage() + ":" + e.getClass().getSimpleName();
|
|
|
- Toast.error(context,msg);
|
|
|
+ Toast.error(context, msg);
|
|
|
Log.error("上传错误: " + e.getMessage());
|
|
|
-
|
|
|
+
|
|
|
// 隐藏Loading
|
|
|
LoadingDialog.hide();
|
|
|
-
|
|
|
+
|
|
|
return new R<>(msg);
|
|
|
} catch (Exception e) {
|
|
|
String msg = "操作失败" + e.getMessage();
|
|
|
- Toast.error(context,msg);
|
|
|
+ Toast.error(context, msg);
|
|
|
Log.error("上传异常: " + e.getMessage());
|
|
|
-
|
|
|
+
|
|
|
// 隐藏Loading
|
|
|
- LoadingDialog.hide();
|
|
|
-
|
|
|
+
|
|
|
return new R<>(msg);
|
|
|
} finally {
|
|
|
if (connection != null) {
|