├── .idea
├── dictionaries
│ └── wulang.xml
├── encodings.xml
├── libraries
│ ├── async_http_client_2_0_24.xml
│ └── javax_mail.xml
├── misc.xml
├── modules.xml
├── uiDesigner.xml
└── vcs.xml
├── ApkAuto.iml
├── MyEmail.eml
├── README.md
├── default.properties
├── libs
├── async-http-client-2.0.24.jar
├── fastjson-1.2.8.jar
├── javax.mail.jar
├── okhttp-3.6.0.jar
└── okio-1.11.0.jar
└── src
└── codbking
├── APkUtils.java
├── App.java
├── Cmd.java
├── PropertiesUtils.java
├── bean
├── APPInfo.java
├── FirGetUrlData.java
└── User.java
├── impl
├── FirUpdate.java
└── UpdateI.java
└── net
└── HttpHelper.java
/.idea/dictionaries/wulang.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/libraries/async_http_client_2_0_24.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/libraries/javax_mail.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
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 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/uiDesigner.xml:
--------------------------------------------------------------------------------
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 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/ApkAuto.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/MyEmail.eml:
--------------------------------------------------------------------------------
1 | Date: Fri, 10 Feb 2017 10:18:40 +0800 (CST)
2 | From: =?UTF-8?B?5ZC05rWq?= <549042026@qq.com>
3 | To: =?UTF-8?B?5ZC05rWq?= <549042026@qq.com>
4 | Message-ID: <769287236.0.1486693120756@wulang-PC>
5 | Subject: =?UTF-8?B?6YKu5Lu25rWL6K+V?=
6 | MIME-Version: 1.0
7 | Content-Type: text/html;charset=UTF-8
8 | Content-Transfer-Encoding: base64
9 |
10 | 6Ieq5Yqo5YyW5omT5YyF
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | #简介
2 | apk自动打包上传困扰了我很久,很想有一个脚本或程序能够一键自动打包上传,这样可以节省很多时间,还不容易出打包的错误,拿这点时间,去抽跟烟,扯扯蛋,该多好。。。
3 |
4 |
5 | #思路
6 | 以下为上传到fir服务器为例:
7 |
8 | - 配置gradle的apk输出路径
9 | ```
10 | buildTypes {
11 | release {
12 | minifyEnabled false
13 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
14 | applicationVariants.all { variant ->
15 | variant.outputs.each { output ->
16 | //开始输出,自定义输出路径
17 | output.outputFile =
18 | new File("f:/app/apkauto"+".apk")
19 | }
20 | }
21 | }
22 | }
23 | ```
24 |
25 | - 通过Java运行cmd命令到项目目录,运行grald命令,gradlew assembleRelease命令为正式环境打包,你可以手动配置打包环境命令
26 | ```
27 | public static String genAPK(String projectPath) throws IOException {
28 | String params1=projectPath.substring(0,1);
29 | String params2=projectPath;
30 |
31 | String cmd=params1+": "+"&cd "+params2+" &gradlew assembleRelease";
32 | return Cmd.exec(cmd);
33 | }
34 | ```
35 |
36 | - 通过 aapt解析apk的信息,解析命令为aapt dump badging apk路径
37 | ```
38 | public static APPInfo parasAPk(String apkPath) throws IOException {
39 | String reulst=Cmd.exec(PARASE_APK+apkPath);
40 | return APPInfo.getAPPInfo(reulst);
41 | }
42 | ```
43 |
44 | - 配置上传文件信息
45 | ```
46 | #apk生成后绝对地址,这个路径需要在gralde里面设置
47 | apkPath=f:/app/apkauto.apk
48 | #app项目的本地路径
49 | appProjectPath=E:/work/APkAuto/
50 | #更新日志
51 | updateLog=更新日志
52 | ```
53 |
54 | - 上传到服务器(目前以上传到fir为例,具体请参考[fir上传接口](https://fir.im/docs/publish))
55 |
56 | a.获取apk上传信息->b.上传icon->c.上传apk
57 |
58 | #流程
59 |
60 |
61 | 
62 |
63 | 启动程序(App.java)
64 | ```java
65 | public static void main(String[] args) {
66 |
67 | String apkPath = PropertiesUtils.getValue("apkPath");
68 | String projectPath=PropertiesUtils.getValue("appProjectPath");
69 | String updateLog=PropertiesUtils.getValue("updateLog");
70 |
71 | try {
72 | //1. 生成apk
73 | APkUtils.genAPK(projectPath);
74 |
75 | //2. 解析apk,获取apk的信息
76 | APPInfo info2 =APkUtils.parasAPk(apkPath);
77 | System.out.println(info2.toString());
78 |
79 | //3. 上传apk
80 | UpdateI updateI=new FirUpdate();
81 | try {
82 | updateI.updateAPk(info2,apkPath,updateLog);
83 | } catch (Exception e) {
84 | e.printStackTrace();
85 | System.out.println(e.getMessage());
86 | }
87 |
88 | } catch (Exception e) {
89 | // TODO Auto-generated catch block
90 | e.printStackTrace();
91 | System.out.println("上传失败");
92 | }
93 | }
94 | ```
95 |
--------------------------------------------------------------------------------
/default.properties:
--------------------------------------------------------------------------------
1 | #apk生成后绝对地址,这个路径需要在gralde里面设置
2 | apkPath=f:/app/apkauto.apk
3 | #app项目的本地路径
4 | appProjectPath=E:/work/APkAuto/
5 | #更新日志
6 | updateLog=可以动态配置咯
--------------------------------------------------------------------------------
/libs/async-http-client-2.0.24.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codbking/ApkAuto/94969467030efc15bf2e6f288989dbf5e32b599e/libs/async-http-client-2.0.24.jar
--------------------------------------------------------------------------------
/libs/fastjson-1.2.8.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codbking/ApkAuto/94969467030efc15bf2e6f288989dbf5e32b599e/libs/fastjson-1.2.8.jar
--------------------------------------------------------------------------------
/libs/javax.mail.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codbking/ApkAuto/94969467030efc15bf2e6f288989dbf5e32b599e/libs/javax.mail.jar
--------------------------------------------------------------------------------
/libs/okhttp-3.6.0.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codbking/ApkAuto/94969467030efc15bf2e6f288989dbf5e32b599e/libs/okhttp-3.6.0.jar
--------------------------------------------------------------------------------
/libs/okio-1.11.0.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/codbking/ApkAuto/94969467030efc15bf2e6f288989dbf5e32b599e/libs/okio-1.11.0.jar
--------------------------------------------------------------------------------
/src/codbking/APkUtils.java:
--------------------------------------------------------------------------------
1 | package codbking;
2 |
3 | import codbking.bean.APPInfo;
4 |
5 | import java.io.IOException;
6 |
7 | /**
8 | * Created by codbking on 2017/2/9.
9 | */
10 | public class APkUtils {
11 |
12 | private static final String PARASE_APK = "aapt dump badging ";
13 |
14 |
15 | public static APPInfo parasAPk(String apkPath) throws IOException {
16 | String reulst=Cmd.exec(PARASE_APK+apkPath);
17 | return APPInfo.getAPPInfo(reulst);
18 | }
19 |
20 |
21 | public static String genAPK(String projectPath) throws IOException {
22 | //
23 | String params1=projectPath.substring(0,1);
24 | String params2=projectPath;
25 |
26 | String cmd=params1+": "+"&cd "+params2+" &gradlew assembleRelease";
27 |
28 | String cdE= Cmd.exec(cmd);
29 | System.out.println(cdE);
30 | return "";
31 | }
32 |
33 |
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/src/codbking/App.java:
--------------------------------------------------------------------------------
1 | package codbking;
2 |
3 | import codbking.bean.APPInfo;
4 | import codbking.impl.FirUpdate;
5 | import codbking.impl.UpdateI;
6 |
7 | /**
8 | * Created by codbking on 2017/2/9.
9 | */
10 | public class App {
11 |
12 |
13 | public static void main(String[] args) {
14 |
15 | String apkPath = PropertiesUtils.getValue("apkPath");
16 | String projectPath=PropertiesUtils.getValue("appProjectPath");
17 | String updateLog=PropertiesUtils.getValue("updateLog");
18 |
19 | try {
20 | //1. 生成apk
21 | APkUtils.genAPK(projectPath);
22 |
23 | //2. 解析apk,获取apk的信息
24 | APPInfo info2 =APkUtils.parasAPk(apkPath);
25 | System.out.println(info2.toString());
26 |
27 | //3. 上传apk
28 | UpdateI updateI=new FirUpdate();
29 | try {
30 | updateI.updateAPk(info2,apkPath,updateLog);
31 | } catch (Exception e) {
32 | e.printStackTrace();
33 | System.out.println(e.getMessage());
34 | }
35 |
36 | } catch (Exception e) {
37 | // TODO Auto-generated catch block
38 | e.printStackTrace();
39 | System.out.println("上传失败");
40 | }
41 | }
42 |
43 |
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/src/codbking/Cmd.java:
--------------------------------------------------------------------------------
1 | package codbking;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.File;
5 | import java.io.IOException;
6 | import java.io.InputStreamReader;
7 |
8 | /**
9 | * Created by codbking on 2017/2/10.
10 | */
11 | public class Cmd {
12 |
13 |
14 | public static String exec(String cmd) throws IOException {
15 | return exec(cmd,true);
16 | }
17 |
18 |
19 | public static String exec(String cmd,boolean isClose) throws IOException {
20 |
21 | if(isClose){
22 | cmd="cmd /c "+cmd;
23 | }else {
24 | cmd="cmd /k"+cmd;
25 | }
26 |
27 | Process process = Runtime.getRuntime().exec(cmd);
28 | BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
29 | String b = null;
30 | StringBuffer stringBuffer = new StringBuffer();
31 | while ((b = in.readLine()) != null) {
32 | stringBuffer.append(b);
33 | }
34 | String result = new String(stringBuffer.toString().getBytes(), "utf-8");
35 | return result;
36 | }
37 |
38 |
39 | public static String exec(String cmd, String[] var2,File file) throws IOException {
40 | cmd="cmd /c "+cmd;
41 | Process process = Runtime.getRuntime().exec(cmd,var2,file);
42 |
43 |
44 | BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
45 | String b = null;
46 | StringBuffer stringBuffer = new StringBuffer();
47 | while ((b = in.readLine()) != null) {
48 | stringBuffer.append(b);
49 | }
50 | String result = new String(stringBuffer.toString().getBytes(), "utf-8");
51 | return result;
52 | }
53 |
54 |
55 | public static String exec(String cmd, String[] var2) throws IOException {
56 |
57 |
58 | Process process = Runtime.getRuntime().exec(cmd,var2);
59 |
60 |
61 | BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
62 | String b = null;
63 | StringBuffer stringBuffer = new StringBuffer();
64 | while ((b = in.readLine()) != null) {
65 | stringBuffer.append(b);
66 | }
67 | String result = new String(stringBuffer.toString().getBytes(), "utf-8");
68 | return result;
69 | }
70 |
71 |
72 | }
73 |
--------------------------------------------------------------------------------
/src/codbking/PropertiesUtils.java:
--------------------------------------------------------------------------------
1 | package codbking;
2 |
3 | import java.io.FileInputStream;
4 | import java.io.InputStreamReader;
5 | import java.util.Properties;
6 |
7 | /**
8 | * Created by codbking on 2017/2/10.
9 | */
10 | public class PropertiesUtils {
11 |
12 |
13 | private static Properties getProperties(){
14 | Properties pro = new Properties();
15 | FileInputStream in = null;
16 | try {
17 | in = new FileInputStream("./././default.properties");
18 | pro.load(new InputStreamReader(in, "UTF-8"));
19 | in.close();
20 | return pro;
21 | } catch (Exception e) {
22 | e.printStackTrace();
23 |
24 | }
25 | return null;
26 | }
27 |
28 |
29 | public static String getValue(String key){
30 | String vlaue=getProperties().getProperty(key);
31 | System.out.println(key+"="+vlaue);
32 | return vlaue;
33 | }
34 |
35 |
36 | public static void main(String[] args) {
37 | System.out.println(Thread.currentThread().getContextClassLoader().getResource("").getPath());
38 | System.out.println(getValue("appProjectPath"));
39 | }
40 |
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/src/codbking/bean/APPInfo.java:
--------------------------------------------------------------------------------
1 | package codbking.bean;
2 |
3 | import java.lang.reflect.Field;
4 |
5 | public class APPInfo {
6 | // name
7 | public String packAge;
8 | public String versionCode;
9 | public String versionName;
10 | // application-label
11 | public String name;
12 |
13 | public static APPInfo getAPPInfo(String info) {
14 | APPInfo appInfo = new APPInfo();
15 | String[] arr = info.split("'");
16 | appInfo.packAge = arr[1];
17 | appInfo.versionCode = arr[3];
18 | appInfo.versionName = arr[5];
19 |
20 | String sp = "application-label:";
21 | int index = info.indexOf(sp);
22 | if (index != -1) {
23 | int index1 = info.indexOf("'", index);
24 | int index2 = info.indexOf("'", index1+1);
25 | String name = info.substring(index1+1, index2);
26 | appInfo.name = name;
27 | }
28 |
29 | return appInfo;
30 | }
31 |
32 | @Override
33 | public String toString() {
34 |
35 | Field[] field = getClass().getFields();
36 | String string = "";
37 |
38 | try {
39 | for (Field f : field) {
40 | string += "[" + f.getName() + "=" + f.get(this).toString() + "]";
41 | }
42 | } catch (Exception e) {
43 | // TODO: handle exception
44 | }
45 |
46 | return string;
47 |
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/src/codbking/bean/FirGetUrlData.java:
--------------------------------------------------------------------------------
1 | package codbking.bean;
2 |
3 | public class FirGetUrlData {
4 |
5 | public String id;
6 | public String type;
7 | public Cert cert;
8 |
9 | public static class Cert {
10 | public Bean icon;
11 | public Bean binary;
12 | public Mqc mqc;
13 | public String support;
14 | public String prefix;
15 | }
16 |
17 | public static class Bean {
18 | public String key;
19 | public String token;
20 | public String upload_url;
21 | }
22 |
23 | public static class Mqc{
24 | public int total;
25 | public int used;
26 | public String is_mqc_availabled;
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/src/codbking/bean/User.java:
--------------------------------------------------------------------------------
1 | package codbking.bean;
2 |
3 | import javax.mail.Message;
4 | import javax.mail.internet.MimeMessage;
5 |
6 | /**
7 | * Created by codbking on 2017/2/10.
8 | */
9 | public class User {
10 |
11 | private String emal;
12 | private String nickName;
13 | private Message.RecipientType type= MimeMessage.RecipientType.TO;
14 |
15 | public String getEmal() {
16 | return emal;
17 | }
18 |
19 | public void setEmal(String emal) {
20 | this.emal = emal;
21 | }
22 |
23 | public String getNickName() {
24 | return nickName;
25 | }
26 |
27 | public void setNickName(String nickName) {
28 | this.nickName = nickName;
29 | }
30 |
31 | public Message.RecipientType getType() {
32 | return type;
33 | }
34 |
35 | public void setType(Message.RecipientType type) {
36 | this.type = type;
37 | }
38 |
39 | public User( Message.RecipientType type,String emal, String nickName) {
40 | this.emal = emal;
41 | this.nickName = nickName;
42 | this.type = type;
43 | }
44 |
45 | public User(String emal, String nickName) {
46 | this.emal = emal;
47 | this.nickName = nickName;
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/codbking/impl/FirUpdate.java:
--------------------------------------------------------------------------------
1 | package codbking.impl;
2 |
3 | import codbking.bean.APPInfo;
4 | import codbking.net.HttpHelper;
5 | import com.alibaba.fastjson.JSON;
6 | import com.alibaba.fastjson.JSONObject;
7 | import codbking.bean.FirGetUrlData;
8 |
9 | import java.io.File;
10 | import java.util.HashMap;
11 |
12 | /**
13 | * Created by codbking on 2017/2/9.
14 | */
15 | public class FirUpdate implements UpdateI {
16 |
17 | //fir获取apk上传信息
18 | private static final String firUrl = "http://api.fir.im/apps";
19 | //fir的token值
20 | private static String token = "you fir token";
21 |
22 | @Override
23 | public void updateAPk(APPInfo info, String apkPath, String updateLog) throws Exception {
24 |
25 |
26 | HashMap map = new HashMap<>();
27 | map.put("type", "android");
28 | map.put("bundle_id", info.packAge);
29 | map.put("api_token", token);
30 |
31 | //获取上传信息
32 | String reuslt = HttpHelper.post(firUrl, map, null);
33 | FirGetUrlData data = JSON.parseObject(reuslt, FirGetUrlData.class);
34 |
35 | System.out.println("获取上传信息:"+JSON.toJSON(data));
36 |
37 | if (data == null) {
38 | throw new Exception("请求出错");
39 | }
40 |
41 |
42 | updateIcon(data, apkPath);
43 | updateApk(data, info, apkPath, updateLog);
44 |
45 | }
46 |
47 |
48 | //上传icon
49 | private void updateIcon(FirGetUrlData data, String apkPath) throws Exception {
50 | HashMap params = new HashMap<>();
51 | params.put("key", data.cert.icon.key);
52 | params.put("token", data.cert.icon.token);
53 | params.put("file", new File(apkPath));
54 |
55 | String result = HttpHelper.update(data.cert.icon.upload_url, params);
56 |
57 | JSONObject json = JSON.parseObject(result);
58 | boolean isSuccess = json.getBoolean("is_completed");
59 | if (isSuccess) {
60 | System.out.println("apk icon上传成功");
61 | } else {
62 | throw new Exception("apk icon上传失败");
63 | }
64 |
65 | }
66 |
67 | //上传apk
68 | private void updateApk(FirGetUrlData data, APPInfo info, String apkPath, String updateLog) throws Exception {
69 |
70 | HashMap upateAPkParams = new HashMap<>();
71 | upateAPkParams.put("key", data.cert.binary.key);
72 | upateAPkParams.put("token", data.cert.binary.token);
73 | upateAPkParams.put("file", new File(apkPath));
74 |
75 | upateAPkParams.put("x:name", info.packAge);
76 | upateAPkParams.put("x:version", info.versionName);
77 | upateAPkParams.put("x:build", info.versionCode);
78 | upateAPkParams.put("x:changelog", updateLog);
79 | String result = HttpHelper.update(data.cert.binary.upload_url, upateAPkParams);
80 |
81 | JSONObject json = JSON.parseObject(result);
82 | boolean isSuccess = json.getBoolean("is_completed");
83 | if (isSuccess) {
84 | System.out.println("apk上传成功");
85 | } else {
86 | throw new Exception("apk上传失败");
87 | }
88 |
89 | }
90 |
91 |
92 | }
93 |
--------------------------------------------------------------------------------
/src/codbking/impl/UpdateI.java:
--------------------------------------------------------------------------------
1 | package codbking.impl;
2 |
3 | import codbking.bean.APPInfo;
4 |
5 | /**
6 | * Created by wulang on 2017/2/9.
7 | */
8 | public interface UpdateI {
9 | /**
10 | *
11 | * @param info apk信息
12 | * @param apkPath apk本地路径
13 | * @param updateLog 上传日志
14 | */
15 | void updateAPk(APPInfo info, String apkPath, String updateLog) throws Exception;
16 |
17 | }
18 |
--------------------------------------------------------------------------------
/src/codbking/net/HttpHelper.java:
--------------------------------------------------------------------------------
1 | package codbking.net;
2 |
3 | import com.alibaba.fastjson.JSON;
4 | import okhttp3.*;
5 |
6 | import java.io.File;
7 | import java.io.IOException;
8 | import java.lang.reflect.Field;
9 | import java.util.HashMap;
10 | import java.util.concurrent.TimeUnit;
11 |
12 | public class HttpHelper {
13 |
14 | private static final OkHttpClient client;
15 | public static final MediaType FILE_TYPE = MediaType.parse("application/octet-stream; charset=utf-8");
16 | public static final MediaType JSON_TYPE = MediaType.parse("application/json; charset=utf-8");
17 |
18 | static {
19 | client = new OkHttpClient();
20 | }
21 |
22 | public static String post(String url, HashMap hashMap, HashMap header)
23 | throws IOException {
24 |
25 | RequestBody body = RequestBody.create(JSON_TYPE, JSON.toJSONString(hashMap));
26 |
27 | Request.Builder request = new Request.Builder().url(url).post(body);
28 |
29 | if (header != null && !header.isEmpty()) {
30 | for (String key : header.keySet()) {
31 | request.addHeader(key, header.get(key));
32 | }
33 | }
34 |
35 | String result = new String(client.newCall(request.build()).execute().body().bytes());
36 |
37 | return result;
38 | }
39 |
40 | public static String update(String url, HashMap params) throws IOException {
41 |
42 | MultipartBody.Builder builder = new MultipartBody.Builder();
43 | builder.setType(MultipartBody.FORM);
44 |
45 | for (String key : params.keySet()) {
46 | Object object = params.get(key);
47 | if (object instanceof File) {
48 | File file = (File) object;
49 | builder.addFormDataPart(key, file.getName(), RequestBody.create(FILE_TYPE, file));
50 | } else {
51 | builder.addFormDataPart(key, object.toString());
52 | }
53 | }
54 |
55 | RequestBody body = builder.build();
56 | Request request = new Request.Builder().url(url).post(body).build();
57 | client.newBuilder().writeTimeout(5000, TimeUnit.SECONDS).build().newCall(request);
58 |
59 | String result = new String(client.newCall(request).execute().body().bytes());
60 |
61 | return result;
62 | }
63 |
64 | public static String get(String url, Object params) throws IOException {
65 |
66 | Request request = new Request.Builder().url(url + getParams(params)).build();
67 |
68 | Call call = client.newCall(request);
69 | ResponseBody body = call.execute().body();
70 |
71 | return body.toString();
72 | }
73 |
74 | public static String get(String url, HashMap params) throws IOException {
75 |
76 | Request request = new Request.Builder().url(url + getParams(params)).build();
77 |
78 | Call call = client.newCall(request);
79 | ResponseBody body = call.execute().body();
80 |
81 | String result = new String(body.bytes());
82 | return result;
83 | }
84 |
85 | public static String getParams(HashMap maps) {
86 |
87 | if (maps == null || maps.isEmpty()) {
88 | return "";
89 | }
90 |
91 | String params = "";
92 | for (String key : maps.keySet()) {
93 | params += "&" + key + "=" + maps.get(key);
94 | }
95 |
96 | if (!"".equals(params)) {
97 | params = params.substring(1);
98 | params = "?" + params;
99 | }
100 |
101 | return params;
102 | }
103 |
104 | public static String getParams(Object object) {
105 | String params = "";
106 |
107 | if (object == null) {
108 | return params;
109 | }
110 |
111 | try {
112 | Field[] fields = object.getClass().getDeclaredFields();
113 |
114 | for (Field field : fields) {
115 | field.setAccessible(true);
116 | params += "&" + field.getName() + "=" + field.get(object);
117 | }
118 | } catch (Exception e) {
119 | }
120 |
121 | if (!"".equals(params)) {
122 | params = params.substring(1);
123 | params = "?" + params;
124 | }
125 |
126 | return params;
127 |
128 | }
129 |
130 |
131 |
132 |
133 | }
134 |
--------------------------------------------------------------------------------