# IAPP实例讲解

本实例讲解IAPP实例讲解

# 简单描述

iapp内置请求无法携带请求头,开发者需要封装一个http.mjava工具类用于请求,开发者可以自行携带后台所需参数,请求头等 以及请求JSON报文,本实例使用java写法,如果不懂请百度查java写法,封装好的工具类开发者只需要关注请求接口以及参数

# 完整请求实例

如下是事件内方法,如单击事件... ,触发事件开启线程,使用内置函数call 调用http.mjava内的GET方法,参数1表示返回参数,参数2表示请求URL,由于get请求内容包含在url内,所以没有第五个请求参数 ,当然POST请求除了第四个请求参数以外还需要第五个参数str,开发者可自行封装工具类,用于调用 调用后开发者解析数据展示即可,


        // 获取图片验证码
    t(){
        // 发起请求
      call(result,"mjava","http.get","/getCodeImg")
        // 获取返回值
      json(result,text)
        // 获取返回code
      json(text, "get", "code", code)
        //获取data数据
      json(text, "get", "data", data)
        // 获取message
      json(text, "get", "message", message)
        // 判断状态码
      f(code==500){
           ufnsui()
           {
             //报错提示
             tw(message)
           }  
      }else f(code==403){
           ufnsui()
           {
             //报错提示
             tw(message)
           }
      }else f(code==200){
           ufnsui(){
                json(data,"get","uuid",uuid)
                json(data,"get","src",src)
                sss uuid=uuid
                //调用保存验证码方法
                call(r,"mjava","saveBase64.saveBase64Img",src,"/storage/emulated/0/a.png")
                us(14,"src","/storage/emulated/0/a.png")
            }
        }
    }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

post调用实例

// 根据id获取属性值
ug(49,"text",number)
ug(56,"text",email)
t()
    {
    s text = "{"test":1, "name":"1243", "age":16}"
    json(text, param)
    call(result,"mjava","http.post","/sendEmailMessage",param)
    // 获取返回值
    json(result,text)
    // 获取返回code
    json(text, "get", "code", code)
    //获取data数据
    json(text, "get", "data", data)
    // 获取message
    json(text, "get", "message", message)
    // 判断状态码
    f(code==500){
         ufnsui(){
           //报错提示
           tw(message)
         }  
    }else f(code==403){
         ufnsui(){
           //报错提示
           tw(message)
         }
    }else f(code==200){
       ufnsui(){
         tw("发送成功")
       }
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

创建一个http.mjava 粘贴如下代码,askKey需要自行更换

import org.json.JSONObject;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;

//这是post请求方式
public post(String url,JSONobject param){
// 获取token
  String token=i.sss("token");
     // 创建输出流
       OutputStream out = null;
       //声明返回值 
       String result="";
        try {
          // 验签,需要更改为自己的字符串,如果在APP设置不需要验签则不需要设置
          String singStr="3.1415926potatoApi";
          //获取当前时间戳用于验签
          long times=System.currentTimeMillis();
          //创建url对象
          URL url=new URL("https://api.potatocloud.cn/api"+url);
         // 创建连接http对象
            HttpURLConnection  connection = (HttpURLConnection) url.openConnection();
            //设置超时时间
            connection.setConnectTimeout(3000);
            //设置读取超时时间
            connection.setReadTimeout(3000);
            //设置请求方式 POST
            connection.setRequestMethod("POST");
            connection.setDoInput(true);
            connection.setDoOutput(false);
            //设置请求头,这里放的是token
            connection.setRequestProperty("apiUserToken",token);
            // 设置签,调用md5进行加密
            connection.setRequestProperty("sign",this.md5(singStr+times));
            // 设置请求时间
            connection.setRequestProperty("time",times+"");
            //设置apitoken
            connection.setRequestProperty("askKey","eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhcHBJZCI6Njk4MTU1MTQ2OTk3NzQ3NzEyLCJnZXRNYW5hZ2VtZW50SWQiOjY5ODExOTE5MzkxODc4NzU4NCwiVElNRSI6MTY1NTU2NDc2ODM3NX0.LaEFrhA1_i_hiWT-Xa0R9yo9dVi6K5xDQ7hb0WZlY_U");
            // 设置请求格式为json
            connection.setRequestProperty("Content-Type", "application/json");
            // 设置链接对外输出流
            out =connection.getOutputStream();
            //开始写入数据
            out.write(param.toString().getBytes());
            // 将数据全部推到流里面
			out.flush();
            // 关闭流,这是必须操作
			out.close();
            // 发起请求
            connection.connect();
            // 获取http返回码
            int responseCode = connection.getResponseCode();
            if (responseCode != HttpURLConnection.HTTP_OK) {
               //请求失败,这里可以使用tw
               syso("请求失败");
            }
            // 获取返回流
            result = getStringByStream(connection.getInputStream());
           // 如果返回空提示失败
             if (result == null) {
             syso("失败");
            }else{
              //请求成功
             syso("成功"+result);
            }
         
            //如果抓到异常需要关流
         } catch (Exception e)  {
           if(out!=null){
            out.close(); 
           }
           //如果报错则请求失败
           syso("请求失败");
         }
 //返回结果    
 return result;
 }

 // 这里是获取流方法,调用就可以,不用详细解释
  private String getStringByStream(InputStream inputStream){
        Reader reader;
        try {
            reader=new InputStreamReader(inputStream,"UTF-8");
            char[] rawBuffer=new char[512];
            StringBuffer buffer=new StringBuffer();
            int length;
            while ((length=reader.read(rawBuffer))!=-1){
                buffer.append(rawBuffer,0,length);
            }
            return buffer.toString();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    
    
    
    // md5算法,不需要解释,调用就行
          public  String md5(String str) {
             MessageDigest md5 = null;
             try {
                    md5 = MessageDigest.getInstance("MD5");
                } catch (Exception e) {
                    e.printStackTrace();
                    return "";
                }

            char[] charArray = str.toCharArray();
            byte[] byteArray = new byte[charArray.length];

            for (int i = 0; i < charArray.length; i++) {
                    byteArray[i] = (byte) charArray[i];
                }
            byte[] md5Bytes = md5.digest(byteArray);

            StringBuffer hexValue = new StringBuffer();
            for (int i = 0; i < md5Bytes.length; i++) {
                    int val = ((int) md5Bytes[i]) & 0xff;
             if (val < 16) {
                     hexValue.append("0");
                   }
               hexValue.append(Integer.toHexString(val));
           }
       return hexValue.toString();
            }
               
    // 这里是get方式请求
    public get(String data){ 
    //获取全局token
    String token=i.sss("token");
       // 获取输入流
        InputStream inputStream = null;
        // 请求地址 
        String url="https://api.potatocloud.cn/api";
        //生命返回结果
        String result="";
        try { 
          // 创建url对象 同post一样
           URL url = new URL(url+data);
           // 验签,同post一样,需要在APP设置开启验签并设置验签字符串
           String singStr="3.1415926potatoApi";
           // 获取当前系统时间
           long times=System.currentTimeMillis();
            if(url != null){ 
                try {
                  // 创建链接对象
                    httpURLConnection = (HttpURLConnection) url.openConnection(); 
                    //设置超时时间 
                    httpURLConnection.setConnectTimeout(3000);
                    // 请求头放置token也就是令牌,如果接口不需要token不传就行
                    httpURLConnection.setRequestProperty("apiUserToken",token);
                    // 请求头设置签
                    httpURLConnection.setRequestProperty("sign",this.md5(singStr+times));
                    // 设置时间戳
                    httpURLConnection.setRequestProperty("time", times+"");
                   // apitoken 
                    httpURLConnection.setRequestProperty("askKey","eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhcHBJZCI6Njk4MTU1MTQ2OTk3NzQ3NzEyLCJnZXRNYW5hZ2VtZW50SWQiOjY5ODExOTE5MzkxODc4NzU4NCwiVElNRSI6MTY1NTU2NDc2ODM3NX0.LaEFrhA1_i_hiWT-Xa0R9yo9dVi6K5xDQ7hb0WZlY_U");
            
                    //设置请求方式 GET
                    httpURLConnection.setRequestMethod("GET");
                    //获取http返回码200为正常
                    int responsecode = httpURLConnection.getResponseCode();
                    //判断返回状态码
                    if(responsecode == HttpURLConnection.HTTP_OK){ 
                        inputStream = httpURLConnection.getInputStream(); 
                    }else{
                      syso("请求失败");
                    }
                } catch (IOException e) { 
                    // 出现异常请求失败
                    if(inputStream!=null){
                      inputStream.close();
                    }
                    syso("请求失败");
                } 
            } 
        } catch (MalformedURLException e) { 
            //出现异常请求失败
            syso("请求失败");
        }
        // 获取返回
        result = getStringByStream(httpURLConnection.getInputStream());
        syso(result);
        return result; 
    }
    

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# Iapp加解密

以下为基本引入

import android.os.Build;
import android.util.Base64;
import androidx.annotation.RequiresApi;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
1
2
3
4
5
6
7

AEC加密方法

    public String AESEncode(String content,String keys) {
        try {
                //创建密码器
                Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                //初始化为加密密码器
                cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(keys));
                byte[] encryptByte = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
                // 将加密以后的数据进行 Base64 编码
                return base64Encode(encryptByte);
         } catch (Exception e) {
                e.printStackTrace();
         }
            return null;
    }

    /**
     * 使用密码获取 AES 秘钥
     */
    public SecretKeySpec getSecretKey(String secretKey) {
        secretKey = toMakeKey(secretKey, 32, "0");
        return new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), "AEC");
    }
    
    private  String toMakeKey(String secretKey, int length, String text) {
        // 获取密钥长度
        int strLen = secretKey.length();
        // 判断长度是否小于应有的长度
        if (strLen < length) {
            // 补全位数
            StringBuilder builder = new StringBuilder();
            // 将key添加至build
            builder.append(secretKey);
            // 遍历添加默认文本
            for (int i = 0; i < length - strLen; i++) {
                builder.append(text);
            }
            // 赋值
            secretKey = builder.toString();
        }
        return secretKey;
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

AEC解密方法

    public  String AESDecode(String content,String keys) {
        try {
                byte[] data = base64Decode(content);
                Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                //设置为解密模式
                cipher.init(Cipher.DECRYPT_MODE, getSecretKey(keys));
                //执行解密操作
                byte[] result = cipher.doFinal(data);
                return new String(result, StandardCharsets.UTF_8);
            } catch (Exception e) {
                e.printStackTrace();
        }
        return null;
    }

    /**
     * 使用密码获取 AES 秘钥
     */
    public SecretKeySpec getSecretKey(String secretKey) {
        secretKey = toMakeKey(secretKey, 32, "0");
        return new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), "AEC");
    }
    
    private  String toMakeKey(String secretKey, int length, String text) {
        // 获取密钥长度
        int strLen = secretKey.length();
        // 判断长度是否小于应有的长度
        if (strLen < length) {
            // 补全位数
            StringBuilder builder = new StringBuilder();
            // 将key添加至build
            builder.append(secretKey);
            // 遍历添加默认文本
            for (int i = 0; i < length - strLen; i++) {
                builder.append(text);
            }
            // 赋值
            secretKey = builder.toString();
        }
        return secretKey;
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

Base64加密方法

    /**
     * 将 字节数组 转换成 Base64 编码
     */
    public  String base64Encode(byte[] data) {
        return Base64.encodeToString(data, Base64.NO_WRAP);
    }
1
2
3
4
5
6

Base64解密方法

    /**
     * 将 Base64 字符串 解码成 字节数组
     */
    public  byte[] base64Decode(String data) {
        return Base64.decode(data, Base64.NO_WRAP);
    }
1
2
3
4
5
6

Md5 加密方法

    // md5算法,
   public  String md5(String str) {
   MessageDigest md5 = null;
   try {
       md5 = MessageDigest.getInstance("MD5");
   } catch (Exception e) {
       e.printStackTrace();
       return "";
   }
  char[] charArray = str.toCharArray();
  byte[] byteArray = new byte[charArray.length];
  for (int i = 0; i < charArray.length; i++) {
          byteArray[i] = (byte) charArray[i];
  }
  byte[] md5Bytes = md5.digest(byteArray);
  StringBuffer hexValue = new StringBuffer();
  for (int i = 0; i < md5Bytes.length; i++) {
    int val = ((int) md5Bytes[i]) & 0xff;
    if (val < 16) {
       hexValue.append("0");
     }
    hexValue.append(Integer.toHexString(val));
 }
    return hexValue.toString();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
lastUpdate: 7/25/2024, 4:20:09 PM