Browse Source

更改为正确的maven地址

yanyi 2 years ago
parent
commit
54c623b54d

+ 1 - 1
README.md

@@ -11,7 +11,7 @@ OkHttp请求封装
 ~~~
 repositories {
         maven {
-            url "http://maven.keleyanyi.com/repository/benyanyi/"
+            url "https://www.benyanyi.com/repository/benyanyi/"
         }
     }
 ~~~

+ 2 - 2
app/build.gradle

@@ -39,8 +39,8 @@ dependencies {
     androidTestImplementation 'androidx.test.ext:junit:1.1.2'
     androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
 //    implementation project(':okhttplib')
-    implementation 'com.yanyi.benyanyi:Logger:1.0.5'
-    implementation 'com.yanyi.benyanyi:permissionlib:1.0.9'
+    implementation 'com.yanyi.benyanyi:Logger:1.1.3'
+    implementation 'com.yanyi.benyanyi:PermissionHelper:1.1.8'
     implementation 'io.reactivex.rxjava2:rxjava:2.2.10'
     implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
     implementation 'org.greenrobot:eventbus:3.1.1'

+ 87 - 0
app/src/main/java/com/mylove/okhttp/MD5Utils.java

@@ -0,0 +1,87 @@
+package com.mylove.okhttp;
+
+import com.benyanyi.loglib.Jlog;
+
+import java.nio.charset.Charset;
+import java.security.MessageDigest;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author mylov
+ * @date 2023/3/22 23:33
+ * @email ben@yanyi.red
+ * @overview
+ */
+public class MD5Utils {
+
+    public static String getSignToken(Map<String, String> map) {
+        String result = "";
+        try {
+            List<Map.Entry<String, String>> infoIds = new ArrayList<Map.Entry<String, String>>(map.entrySet());
+            // 对所有传入参数按照字段名的 ASCII 码从小到大排序(字典序)
+            Collections.sort(infoIds, new Comparator<Map.Entry<String, String>>() {
+                public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
+                    return (o1.getKey()).toString().compareTo(o2.getKey());
+                }
+            });
+            // 构造签名键值对的格式
+            StringBuilder sb = new StringBuilder();
+            for (Map.Entry<String, String> item : infoIds) {
+                if (item.getKey() != null || item.getKey() != "") {
+                    String key = item.getKey();
+                    String val = item.getValue();
+                    if (!(val == "" || val == null)) {
+                        sb.append(key + "=" + val + "&");
+                    }
+                }
+            }
+            sb.deleteCharAt(sb.length()-1);
+            result = sb.toString();
+            Jlog.d(result);
+            //进行MD5加密
+            result = MD5Encode(result);
+        } catch (Exception e) {
+            return null;
+        }
+        Jlog.d(result);
+        return result;
+    }
+
+    public static String MD5Encode(String origin) {
+        String resultString = "";
+        try {
+            resultString = new String(origin.getBytes(Charset.forName("UTF-8")));
+            MessageDigest md5 = MessageDigest.getInstance("MD5");
+            resultString = byteArrayToHexString(md5.digest(resultString.getBytes()));
+        } catch (Exception e) {
+            e.printStackTrace();
+            resultString = origin;
+        }
+        return resultString;
+    }
+
+    private static String byteArrayToHexString(byte[] b) {
+        StringBuilder resultSb = new StringBuilder();
+        for (byte value : b) {
+            resultSb.append(byteToHexString(value));
+        }
+        return resultSb.toString();
+    }
+
+    private static String byteToHexString(byte b) {
+        int n = b;
+        if (n < 0) {
+            n += 256;
+        }
+        int d1 = n / 16;
+        int d2 = n % 16;
+        return hexDigits[d1] + hexDigits[d2];
+    }
+
+    private static String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
+
+}

+ 99 - 40
app/src/main/java/com/mylove/okhttp/MainActivity.java

@@ -3,6 +3,8 @@ package com.mylove.okhttp;
 import android.Manifest;
 import android.content.Intent;
 import android.os.Bundle;
+import android.os.Environment;
+import android.util.Base64;
 import android.view.View;
 
 import androidx.annotation.Nullable;
@@ -13,11 +15,14 @@ import com.benyanyi.okhttp.OkHttpUtil;
 import com.benyanyi.okhttp.download.DownloadInfo;
 import com.benyanyi.okhttp.listener.OnDownLoadObserver;
 import com.benyanyi.okhttp.listener.OnOkHttpListener;
-import com.benyanyi.permissionlib.PermissionCallBack;
 import com.benyanyi.permissionlib.PermissionHelper;
-import com.benyanyi.permissionlib.PermissionType;
-import com.benyanyi.permissionlib.msg.FailureMsg;
+import com.benyanyi.permissionlib.callback.PermissionCallBack;
+import com.benyanyi.permissionlib.type.PermissionType;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -38,41 +43,35 @@ public class MainActivity extends AppCompatActivity {
         findViewById(R.id.but).setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
-                PermissionHelper.getInstance(MainActivity.this).hasPermission(0x11, new PermissionCallBack() {
-                    @Override
-                    public void onPermissionSuccess(int permissionCode) {
-                        Intent intent = new Intent(MainActivity.this, DownloadActivity.class);
-                        startActivity(intent);
-                    }
-
-                    @Override
-                    public void onPermissionFailure(FailureMsg failureMsg) {
-
-                    }
-
-                    @Override
-                    public void onPermissionComplete(int permissionCode) {
-
-                    }
-                }, Manifest.permission.WRITE_EXTERNAL_STORAGE);
+                PermissionHelper.with(MainActivity.this)
+                        .setPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE)
+                        .request(new PermissionCallBack() {
+                            @Override
+                            public void onSuccess() {
+//                                Intent intent = new Intent(MainActivity.this, DownloadActivity.class);
+//                                startActivity(intent);
+                                upload();
+                            }
+                        });
             }
         });
-        OkHttpUtil.getInstance(this).url("http://apitest.yanyi.online/app/update").getText(null).async(new OnOkHttpListener<Object>() {
-            @Override
-            public void onCompleted() {
-
-            }
-
-            @Override
-            public void onSuccess(Object message) {
-                Jlog.d(message);
-            }
-
-            @Override
-            public void onFailure(Throwable t) {
+//        OkHttpUtil.getInstance(this).url("http://apitest.yanyi.online/app/update").getText(null).async(new OnOkHttpListener<Object>() {
+//            @Override
+//            public void onCompleted() {
+//
+//            }
+//
+//            @Override
+//            public void onSuccess(Object message) {
+//                Jlog.d(message);
+//            }
+//
+//            @Override
+//            public void onFailure(Throwable t) {
+//
+//            }
+//        });
 
-            }
-        });
 //        init();
 //        PermissionHelper.getInstance(this).hasPermission(0x11, new PermissionCallBack() {
 //            @Override
@@ -118,22 +117,82 @@ public class MainActivity extends AppCompatActivity {
 
     public void onClick(View view) {
         String[] strings = new String[]{PermissionType.STORAGE};
-        PermissionHelper.getInstance(this).hasPermission(1, new PermissionCallBack() {
+        PermissionHelper.with(this).setPermissions(strings).request(new PermissionCallBack() {
             @Override
-            public void onPermissionSuccess(int permissionCode) {
+            public void onSuccess() {
                 startActivity(new Intent(MainActivity.this, DownloadActivity.class));
             }
+        });
+    }
+
+    private void upload() {
+        String path = Environment.getExternalStorageDirectory() + "/DCIM/camera/IMG_20190620_082204.jpg";
+        String path1 = Environment.getExternalStorageDirectory() + "/tencent/micromsg/weixin/mmexport1528644702092.jpg";
+        File file = new File(path);
+        File file1 = new File(path1);
+        Map<Object, Object> oMap = new HashMap<>();
+        oMap.put("sn", "2023022100001_孟*杰_0034");
+        oMap.put("device_no", "123456");
+        oMap.put("appid", "37bU34uZPTUjzGZTsZ8eSh");
+        oMap.put("timestamp", System.currentTimeMillis() / 1000 + "");
+        oMap.put("sign", sign(oMap));
+        oMap.put("origin_image", fileToBase64(file));
+        oMap.put("photo_image", fileToBase64(file1));
+        for (Map.Entry<Object, Object> entry : oMap.entrySet()) {
+            if (entry.getValue().toString().length() <= 10) {
+                Jlog.d(entry.getKey() + "=" + entry.getValue());
 
+            } else {
+                Jlog.d(entry.getKey() + "=" + (entry.getValue().toString().substring(0, 20)));
+            }
+        }
+        OkHttpUtil.getInstance(this).url("https://opend.icaiji.com.cn/wxapp/v6/photo").postText(null, oMap).async(new OnOkHttpListener<Object>() {
             @Override
-            public void onPermissionFailure(FailureMsg failureMsg) {
+            public void onCompleted() {
 
             }
 
             @Override
-            public void onPermissionComplete(int permissionCode) {
+            public void onSuccess(Object message) {
+                Jlog.d(message);
+            }
+
+            @Override
+            public void onFailure(Throwable t) {
+
+            }
+        });
+    }
+
+    private String sign(Map<Object, Object> oMap) {
+        Map<String, String> map = new HashMap<>();
+        for (Map.Entry<Object, Object> entry : oMap.entrySet()) {
+            map.put(entry.getKey().toString(), entry.getValue().toString());
+        }
+        map.put("secret", "3sx9NNhVGEEEouhoenA5aJ");
+        return MD5Utils.getSignToken(map);
+    }
 
+    private String fileToBase64(File file) {
+        String base64 = null;
+        InputStream in = null;
+        try {
+            in = new FileInputStream(file);
+            byte[] bytes = new byte[in.available()];
+            int length = in.read(bytes);
+            base64 = Base64.encodeToString(bytes, 0, length, Base64.DEFAULT);
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            try {
+                if (in != null) {
+                    in.close();
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
             }
-        }, strings);
+            return base64;
+        }
     }
 
     private void init() {

+ 2 - 2
build.gradle

@@ -5,7 +5,7 @@ buildscript {
     repositories {
         google()
         jcenter()
-        maven { url 'http://maven.keleyanyi.com/repository/benyanyi/' }
+        maven { url 'https://www.benyanyi.com/repository/benyanyi/' }
     }
     dependencies {
         classpath 'com.android.tools.build:gradle:4.1.3'
@@ -17,7 +17,7 @@ allprojects {
     repositories {
         google()
         jcenter()
-        maven { url 'http://maven.keleyanyi.com/repository/benyanyi/' }
+        maven { url 'https://www.benyanyi.com/repository/benyanyi/' }
         //解决中文乱码问题
         tasks.withType(Javadoc) { //兼容中文
             options.addStringOption('Xdoclint:none', '-quiet')