Skip to content

Commit

Permalink
修复BUG
Browse files Browse the repository at this point in the history
  • Loading branch information
ZDK committed Sep 30, 2020
1 parent 0e756f5 commit e97b61e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 29 deletions.
4 changes: 2 additions & 2 deletions ProjectFrame/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 111
versionName "1.1.1"
versionCode 112
versionName "1.1.2"

viewBinding {
enabled = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dzs.projectframe.utils;


import android.annotation.SuppressLint;
import android.text.TextUtils;

import com.dzs.projectframe.bean.NetEntity;
Expand Down Expand Up @@ -92,7 +93,7 @@ public String getContentType() {
* @param urlString url
* @return HttpURLConnection
*/
private static HttpURLConnection getHttpUrlConnect(String urlString, NetEntity netEntity) throws IOException {
private static HttpURLConnection getHttpUrlConnect(String urlString, NetEntity<?> netEntity) throws IOException {
HttpURLConnection connection;
URL url = new URL(urlString);
connection = (HttpURLConnection) url.openConnection();
Expand All @@ -108,7 +109,7 @@ private static HttpURLConnection getHttpUrlConnect(String urlString, NetEntity n
* @param urlString url
* @return HttpURLConnection
*/
private static HttpsURLConnection getHttpsUrlConnect(String urlString, NetEntity netEntity) throws Exception {
private static HttpsURLConnection getHttpsUrlConnect(String urlString, NetEntity<?> netEntity) throws Exception {
HttpsURLConnection connection;
URL url = new URL(urlString);
if (netEntity.getSslStream() == null) initSSLAll();
Expand All @@ -125,16 +126,15 @@ private static HttpsURLConnection getHttpsUrlConnect(String urlString, NetEntity
*
* @param connection URLConnection
*/
private static void setConnect(URLConnection connection, NetEntity netEntity) {
private static void setConnect(URLConnection connection, NetEntity<?> netEntity) {
connection.setDoInput(true);
connection.setDoOutput(netEntity.getRequestMethod() != HttpRequestMethod.GET);
connection.setUseCaches(false);
connection.setConnectTimeout(netEntity.getTIMEOUT_CONNECTION());
connection.setReadTimeout(netEntity.getTIMEOUT_READ());
if (netEntity.getRequestMethod() != HttpRequestMethod.GET){
if (netEntity.getRequestMethod() != HttpRequestMethod.GET) {
connection.setRequestProperty("Content-Type", netEntity.getRequestType().contentType);
}
@SuppressWarnings("unchecked")
Map<String, Object> head = netEntity.getRequestHead();
if (head != null && !head.isEmpty()) {
for (Map.Entry<String, Object> entry : head.entrySet()) {
Expand All @@ -150,11 +150,11 @@ private static void setConnect(URLConnection connection, NetEntity netEntity) {
* @param isForce 为true时候无论是否过期都会返回数据
* @return NetEntity
*/
public static NetEntity getCatch(NetEntity netEntity, boolean isForce) {
public static NetEntity<?> getCatch(NetEntity<?> netEntity, boolean isForce) {
if (netEntity == null || TextUtils.isEmpty(netEntity.getCacheKey())) {
return null;
}
NetEntity cacheNetEntity = DiskLruCacheHelpUtils.getInstance().getCatch(netEntity.getCacheKey());
NetEntity<?> cacheNetEntity = DiskLruCacheHelpUtils.getInstance().getCatch(netEntity.getCacheKey());
return cacheNetEntity == null ? null : isForce ? cacheNetEntity : (cacheNetEntity.isExpired() ? null : cacheNetEntity);
}

Expand All @@ -164,10 +164,10 @@ public static NetEntity getCatch(NetEntity netEntity, boolean isForce) {
*
* @return NetEntity
*/
public static NetEntity getData(NetEntity netEntity) {
public static NetEntity<?> getData(NetEntity<?> netEntity) {
//网络未连接时候,读取缓存文件
if (!SystemUtils.checkNetConttent(ProjectContext.appContext)) {
NetEntity tempCache = getCatch(netEntity, true);
NetEntity<?> tempCache = getCatch(netEntity, true);
netEntity = tempCache != null ? tempCache : new NetEntity();
netEntity.setCacheData(tempCache != null);
netEntity.setNetResultType(NetResultType.NET_NOT_CONNECT);
Expand All @@ -177,7 +177,6 @@ public static NetEntity getData(NetEntity netEntity) {
netEntity.setNetResultType(NetResultType.NET_CONNECT_FAIL);
String url = netEntity.getUrl();
HttpRequestMethod method = netEntity.getRequestMethod();
@SuppressWarnings("unchecked")
Map<String, Object> params = netEntity.getRequestParameter();
int accessNum = 0;
InputStream is = null;
Expand All @@ -198,7 +197,7 @@ public static NetEntity getData(NetEntity netEntity) {
if (method != HttpRequestMethod.GET) {
dataOutputStream = new DataOutputStream(isHttps ? httpsURLConnection.getOutputStream() : connection.getOutputStream());
if (netEntity.getRequestType() == RequestType.FORM) {
addFormField(params.entrySet(), dataOutputStream);
addFormField(params, dataOutputStream);
} else if (netEntity.getRequestType() == RequestType.JSON) {
addJsonField(params, dataOutputStream);
} else if (netEntity.getRequestType() == RequestType.UPLOAD) {
Expand Down Expand Up @@ -250,7 +249,7 @@ public static NetEntity getData(NetEntity netEntity) {
}
} while (accessNum < netEntity.getACCESS_NUM());
if (netEntity.getNetResultType() == NetResultType.NET_CONNECT_FAIL) {
NetEntity tempCache = getCatch(netEntity, true);
NetEntity<?> tempCache = getCatch(netEntity, true);
netEntity = tempCache != null ? tempCache : new NetEntity();
netEntity.setCacheData(tempCache != null);
netEntity.setNetResultType(NetResultType.NET_CONNECT_FAIL);
Expand Down Expand Up @@ -287,16 +286,16 @@ private static void addFilesContent(UploadFile[] files, DataOutputStream output)
* @param params 参数列表
* @param output 输出流
*/
private static void addFormField(Set<Map.Entry<String, Object>> params, DataOutputStream output) throws IOException {
if (params != null) {
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, Object> param : params) {
private static void addFormField(Map<String, Object> params, DataOutputStream output) throws IOException {
StringBuilder sb = new StringBuilder();
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, Object> param : params.entrySet()) {
sb.append(param.getKey()).append("=").append(param.getValue() == null ? "" : URLEncoder.encode(param.getValue().toString(), StandardCharsets.UTF_8.name()));
sb.append("&");
}
sb.deleteCharAt(sb.length() - 1);
output.write(new String(sb.toString().getBytes(), StandardCharsets.UTF_8).getBytes());
}
output.write(new String(sb.toString().getBytes(), StandardCharsets.UTF_8).getBytes());
}


Expand Down Expand Up @@ -341,16 +340,16 @@ private static void initSSL(HttpsURLConnection httpsURLConnection, InputStream i
context.init(null, tmf.getTrustManagers(), null);
httpsURLConnection.setSSLSocketFactory(context.getSocketFactory());
}

@SuppressLint("TrustAllX509TrustManager")
private static void initSSLAll() throws Exception {
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, new TrustManager[]{new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) {
}

@Override
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {

}

Expand All @@ -361,6 +360,7 @@ public X509Certificate[] getAcceptedIssuers() {
}}, null);
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
@SuppressLint("BadHostnameVerifier")
@Override
public boolean verify(String arg0, SSLSession arg1) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class JsonUtils {
*/
public static String mapToJsonStr(Map<?, ?> params) throws Exception {
JSONObject jsonObject = mapToJsonOb(params);
return jsonObject == null ? "" : jsonObject.toString();
return jsonObject.toString();
}

/**
Expand All @@ -40,15 +40,14 @@ public static String mapToJsonStr(Map<?, ?> params) throws Exception {
*/
public static JSONObject mapToJsonOb(Map<?, ?> params) throws Exception {
JSONObject jsonObject = new JSONObject();
for (Object o : params.entrySet()) {
Map.Entry entry = (Map.Entry) o;
for (Map.Entry<?,?> entry : params.entrySet()) {
if (entry.getValue() instanceof Map) {
jsonObject.put((String) entry.getKey(), mapToJsonOb((Map) entry.getValue()));
jsonObject.put((String) entry.getKey(), mapToJsonOb((Map<?,?>) entry.getValue()));
} else if (entry.getValue() instanceof List) {
JSONArray jsonArray = new JSONArray();
List list = (List) entry.getValue();
List<?> list = (List<?>) entry.getValue();
for (int i = 0; i < list.size(); i++) {
if (((List) entry.getValue()).get(i) instanceof Map) {
if (((List<?>) entry.getValue()).get(i) instanceof Map) {
jsonArray.put(i, mapToJsonOb((Map<?, ?>) list.get(i)));
} else {
jsonArray.put(i, list.get(i));
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ Step 2. Add the dependency

```
dependencies {
implementation 'com.github.DZSDevelop:APF:V1.1.1'
implementation 'com.github.DZSDevelop:APF:V1.1.2'
}
```

0 comments on commit e97b61e

Please sign in to comment.