Showing
65 changed files
with
3173 additions
and
0 deletions
._.idea
0 → 100644
No preview for this file type
._settings.gradle
0 → 100644
No preview for this file type
.gitignore
0 → 100644
.idea/._.DS_Store
0 → 100644
No preview for this file type
.idea/.name
0 → 100644
1 | +OkhttpUtils |
.idea/codeStyles/Project.xml
0 → 100644
1 | +<component name="ProjectCodeStyleConfiguration"> | ||
2 | + <code_scheme name="Project" version="173"> | ||
3 | + <codeStyleSettings language="XML"> | ||
4 | + <arrangement> | ||
5 | + <rules> | ||
6 | + <section> | ||
7 | + <rule> | ||
8 | + <match> | ||
9 | + <AND> | ||
10 | + <NAME>xmlns:android</NAME> | ||
11 | + <XML_ATTRIBUTE /> | ||
12 | + <XML_NAMESPACE>^$</XML_NAMESPACE> | ||
13 | + </AND> | ||
14 | + </match> | ||
15 | + </rule> | ||
16 | + </section> | ||
17 | + <section> | ||
18 | + <rule> | ||
19 | + <match> | ||
20 | + <AND> | ||
21 | + <NAME>xmlns:.*</NAME> | ||
22 | + <XML_ATTRIBUTE /> | ||
23 | + <XML_NAMESPACE>^$</XML_NAMESPACE> | ||
24 | + </AND> | ||
25 | + </match> | ||
26 | + <order>BY_NAME</order> | ||
27 | + </rule> | ||
28 | + </section> | ||
29 | + <section> | ||
30 | + <rule> | ||
31 | + <match> | ||
32 | + <AND> | ||
33 | + <NAME>.*:id</NAME> | ||
34 | + <XML_ATTRIBUTE /> | ||
35 | + <XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE> | ||
36 | + </AND> | ||
37 | + </match> | ||
38 | + </rule> | ||
39 | + </section> | ||
40 | + <section> | ||
41 | + <rule> | ||
42 | + <match> | ||
43 | + <AND> | ||
44 | + <NAME>.*:name</NAME> | ||
45 | + <XML_ATTRIBUTE /> | ||
46 | + <XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE> | ||
47 | + </AND> | ||
48 | + </match> | ||
49 | + </rule> | ||
50 | + </section> | ||
51 | + <section> | ||
52 | + <rule> | ||
53 | + <match> | ||
54 | + <AND> | ||
55 | + <NAME>name</NAME> | ||
56 | + <XML_ATTRIBUTE /> | ||
57 | + <XML_NAMESPACE>^$</XML_NAMESPACE> | ||
58 | + </AND> | ||
59 | + </match> | ||
60 | + </rule> | ||
61 | + </section> | ||
62 | + <section> | ||
63 | + <rule> | ||
64 | + <match> | ||
65 | + <AND> | ||
66 | + <NAME>style</NAME> | ||
67 | + <XML_ATTRIBUTE /> | ||
68 | + <XML_NAMESPACE>^$</XML_NAMESPACE> | ||
69 | + </AND> | ||
70 | + </match> | ||
71 | + </rule> | ||
72 | + </section> | ||
73 | + <section> | ||
74 | + <rule> | ||
75 | + <match> | ||
76 | + <AND> | ||
77 | + <NAME>.*</NAME> | ||
78 | + <XML_ATTRIBUTE /> | ||
79 | + <XML_NAMESPACE>^$</XML_NAMESPACE> | ||
80 | + </AND> | ||
81 | + </match> | ||
82 | + <order>BY_NAME</order> | ||
83 | + </rule> | ||
84 | + </section> | ||
85 | + <section> | ||
86 | + <rule> | ||
87 | + <match> | ||
88 | + <AND> | ||
89 | + <NAME>.*</NAME> | ||
90 | + <XML_ATTRIBUTE /> | ||
91 | + <XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE> | ||
92 | + </AND> | ||
93 | + </match> | ||
94 | + <order>ANDROID_ATTRIBUTE_ORDER</order> | ||
95 | + </rule> | ||
96 | + </section> | ||
97 | + <section> | ||
98 | + <rule> | ||
99 | + <match> | ||
100 | + <AND> | ||
101 | + <NAME>.*</NAME> | ||
102 | + <XML_ATTRIBUTE /> | ||
103 | + <XML_NAMESPACE>.*</XML_NAMESPACE> | ||
104 | + </AND> | ||
105 | + </match> | ||
106 | + <order>BY_NAME</order> | ||
107 | + </rule> | ||
108 | + </section> | ||
109 | + </rules> | ||
110 | + </arrangement> | ||
111 | + </codeStyleSettings> | ||
112 | + </code_scheme> | ||
113 | +</component> |
.idea/compiler.xml
0 → 100644
.idea/encodings.xml
0 → 100644
.idea/gradle.xml
0 → 100644
1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
2 | +<project version="4"> | ||
3 | + <component name="GradleMigrationSettings" migrationVersion="1" /> | ||
4 | + <component name="GradleSettings"> | ||
5 | + <option name="linkedExternalProjectsSettings"> | ||
6 | + <GradleProjectSettings> | ||
7 | + <option name="testRunner" value="PLATFORM" /> | ||
8 | + <option name="distributionType" value="DEFAULT_WRAPPED" /> | ||
9 | + <option name="externalProjectPath" value="$PROJECT_DIR$" /> | ||
10 | + <option name="modules"> | ||
11 | + <set> | ||
12 | + <option value="$PROJECT_DIR$" /> | ||
13 | + <option value="$PROJECT_DIR$/app" /> | ||
14 | + </set> | ||
15 | + </option> | ||
16 | + <option name="resolveModulePerSourceSet" value="false" /> | ||
17 | + <option name="useQualifiedModuleNames" value="true" /> | ||
18 | + </GradleProjectSettings> | ||
19 | + </option> | ||
20 | + </component> | ||
21 | +</project> |
.idea/jarRepositories.xml
0 → 100644
1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
2 | +<project version="4"> | ||
3 | + <component name="RemoteRepositoriesConfiguration"> | ||
4 | + <remote-repository> | ||
5 | + <option name="id" value="central" /> | ||
6 | + <option name="name" value="Maven Central repository" /> | ||
7 | + <option name="url" value="https://repo1.maven.org/maven2" /> | ||
8 | + </remote-repository> | ||
9 | + <remote-repository> | ||
10 | + <option name="id" value="jboss.community" /> | ||
11 | + <option name="name" value="JBoss Community repository" /> | ||
12 | + <option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" /> | ||
13 | + </remote-repository> | ||
14 | + <remote-repository> | ||
15 | + <option name="id" value="BintrayJCenter" /> | ||
16 | + <option name="name" value="BintrayJCenter" /> | ||
17 | + <option name="url" value="https://jcenter.bintray.com/" /> | ||
18 | + </remote-repository> | ||
19 | + <remote-repository> | ||
20 | + <option name="id" value="Google" /> | ||
21 | + <option name="name" value="Google" /> | ||
22 | + <option name="url" value="https://dl.google.com/dl/android/maven2/" /> | ||
23 | + </remote-repository> | ||
24 | + </component> | ||
25 | +</project> |
.idea/misc.xml
0 → 100644
1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
2 | +<project version="4"> | ||
3 | + <component name="CMakeSettings"> | ||
4 | + <configurations> | ||
5 | + <configuration PROFILE_NAME="Debug" CONFIG_NAME="Debug" /> | ||
6 | + </configurations> | ||
7 | + </component> | ||
8 | + <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> | ||
9 | + <output url="file://$PROJECT_DIR$/build/classes" /> | ||
10 | + </component> | ||
11 | + <component name="ProjectType"> | ||
12 | + <option name="id" value="Android" /> | ||
13 | + </component> | ||
14 | +</project> |
.idea/runConfigurations.xml
0 → 100644
1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
2 | +<project version="4"> | ||
3 | + <component name="RunConfigurationProducerService"> | ||
4 | + <option name="ignoredProducers"> | ||
5 | + <set> | ||
6 | + <option value="org.jetbrains.plugins.gradle.execution.test.runner.AllInPackageGradleConfigurationProducer" /> | ||
7 | + <option value="org.jetbrains.plugins.gradle.execution.test.runner.TestClassGradleConfigurationProducer" /> | ||
8 | + <option value="org.jetbrains.plugins.gradle.execution.test.runner.TestMethodGradleConfigurationProducer" /> | ||
9 | + </set> | ||
10 | + </option> | ||
11 | + </component> | ||
12 | +</project> |
.idea/vcs.xml
0 → 100644
README.md
0 → 100644
app/._.DS_Store
0 → 100644
No preview for this file type
app/._maven-release-aar.gradle
0 → 100644
No preview for this file type
app/.gitignore
0 → 100644
1 | +/build |
app/build.gradle
0 → 100644
1 | +import java.text.SimpleDateFormat | ||
2 | + | ||
3 | +//apply plugin: 'com.android.application' | ||
4 | +apply plugin: 'com.android.library' | ||
5 | + | ||
6 | +static def buildTime() { | ||
7 | + def df = new SimpleDateFormat("MMddHHmm") | ||
8 | + return df.format(new Date()) | ||
9 | +} | ||
10 | + | ||
11 | +def baseVersion = "1.0.0" | ||
12 | + | ||
13 | +// versionCode按时间自增 | ||
14 | +static def increasedVersionCode(){ | ||
15 | + return (int)(System.currentTimeMillis()/1000/60) | ||
16 | +} | ||
17 | + | ||
18 | +android { | ||
19 | + compileSdkVersion 28 | ||
20 | + buildToolsVersion "29.0.2" | ||
21 | + | ||
22 | + defaultConfig { | ||
23 | + minSdkVersion 17 | ||
24 | + targetSdkVersion 28 | ||
25 | + versionCode increasedVersionCode() | ||
26 | + versionName "${baseVersion}.${buildTime()}" | ||
27 | + | ||
28 | + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | ||
29 | + } | ||
30 | + | ||
31 | + buildTypes { | ||
32 | + release { | ||
33 | + minifyEnabled false | ||
34 | + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
35 | + } | ||
36 | + } | ||
37 | + | ||
38 | +} | ||
39 | + | ||
40 | +dependencies { | ||
41 | + implementation fileTree(dir: 'libs', include: ['*.jar']) | ||
42 | + | ||
43 | + implementation 'com.android.support:appcompat-v7:28.0.0' | ||
44 | + implementation 'com.android.support.constraint:constraint-layout:1.1.3' | ||
45 | + androidTestImplementation 'com.android.support.test:runner:1.0.2' | ||
46 | + androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' | ||
47 | + implementation 'com.squareup.okhttp3:okhttp:3.12.1' | ||
48 | +} | ||
49 | + | ||
50 | +apply from: 'maven-release-aar.gradle' |
app/maven-release-aar.gradle
0 → 100644
1 | +// 1.maven-插件 | ||
2 | +apply plugin: 'maven' | ||
3 | + | ||
4 | +// 2.maven-信息 | ||
5 | +//ext {// ext is a gradle closure allowing the declaration of global properties | ||
6 | +// PUBLISH_GROUP_ID = 'com.hhmedic.android.sdk' | ||
7 | +// PUBLISH_ARTIFACT_ID = 'nim' | ||
8 | +// PUBLISH_VERSION = android.defaultConfig.versionName | ||
9 | +//} | ||
10 | + | ||
11 | +def PUBLISH_URL = 'http://develop.hh-medic.com/repository/maven-releases/' | ||
12 | + | ||
13 | +def PUBLISH_GROUP_ID = 'com.hhmedic.android.sdk' | ||
14 | + | ||
15 | +def PUBLISH_ARTIFACT_ID = "okhttputils" | ||
16 | + | ||
17 | +def PUBLISH_VERSION = android.defaultConfig.versionName | ||
18 | + | ||
19 | + | ||
20 | +// 3.maven-输出路径 | ||
21 | +uploadArchives { | ||
22 | +// repositories.mavenDeployer { | ||
23 | +// //这里就是最后输出地址,在自己电脑上新建个文件夹,把文件夹路径粘贴在此 | ||
24 | +// //注意”file://“ + 路径,有三个斜杠,别漏了 | ||
25 | +// repository(url: "file:///Workspace/Workshop/HHSDKAndroidOutput") | ||
26 | +// | ||
27 | +// pom.project { | ||
28 | +// groupId project.PUBLISH_GROUP_ID | ||
29 | +// artifactId project.PUBLISH_ARTIFACT_ID | ||
30 | +// version project.PUBLISH_VERSION | ||
31 | +// } | ||
32 | +// } | ||
33 | + | ||
34 | + | ||
35 | + repositories{ | ||
36 | + mavenDeployer{ | ||
37 | + repository(url: PUBLISH_URL) { | ||
38 | + authentication(userName: "hh-user", password: "hi9rqvdR") | ||
39 | + } | ||
40 | + | ||
41 | + pom.groupId = PUBLISH_GROUP_ID | ||
42 | + pom.artifactId = PUBLISH_ARTIFACT_ID | ||
43 | + pom.version = PUBLISH_VERSION | ||
44 | + | ||
45 | + pom.project { | ||
46 | + licenses { | ||
47 | + license { | ||
48 | + name 'The Apache Software License, Version 2.0' | ||
49 | + url 'http://www.apache.org/licenses/LICENSE-2.0.txt' | ||
50 | + } | ||
51 | + } | ||
52 | + } | ||
53 | + } | ||
54 | + } | ||
55 | +} | ||
56 | + | ||
57 | +//以下代码会生成jar包源文件,如果是不开源码,请不要输入这段 | ||
58 | +//aar包内包含注释 | ||
59 | +//task androidSourcesJar(type: Jar) { | ||
60 | +// classifier = 'sources' | ||
61 | +// from android.sourceSets.main.java.sourceFiles | ||
62 | +//} | ||
63 | +// | ||
64 | +//artifacts { | ||
65 | +// archives androidSourcesJar | ||
66 | +//} |
app/proguard-rules.pro
0 → 100644
1 | +# Add project specific ProGuard rules here. | ||
2 | +# You can control the set of applied configuration files using the | ||
3 | +# proguardFiles setting in build.gradle. | ||
4 | +# | ||
5 | +# For more details, see | ||
6 | +# http://developer.android.com/guide/developing/tools/proguard.html | ||
7 | + | ||
8 | +# If your project uses WebView with JS, uncomment the following | ||
9 | +# and specify the fully qualified class name to the JavaScript interface | ||
10 | +# class: | ||
11 | +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
12 | +# public *; | ||
13 | +#} | ||
14 | + | ||
15 | +# Uncomment this to preserve the line number information for | ||
16 | +# debugging stack traces. | ||
17 | +#-keepattributes SourceFile,LineNumberTable | ||
18 | + | ||
19 | +# If you keep the line number information, uncomment this to | ||
20 | +# hide the original source file name. | ||
21 | +#-renamesourcefileattribute SourceFile |
app/src/._.DS_Store
0 → 100644
No preview for this file type
app/src/main/AndroidManifest.xml
0 → 100644
No preview for this file type
1 | +package com.hhmedic.android.sdk.okhttputils.okhttp; | ||
2 | + | ||
3 | + | ||
4 | +import com.hhmedic.android.sdk.okhttputils.okhttp.builder.GetBuilder; | ||
5 | +import com.hhmedic.android.sdk.okhttputils.okhttp.builder.HeadBuilder; | ||
6 | +import com.hhmedic.android.sdk.okhttputils.okhttp.builder.OtherRequestBuilder; | ||
7 | +import com.hhmedic.android.sdk.okhttputils.okhttp.builder.PostFileBuilder; | ||
8 | +import com.hhmedic.android.sdk.okhttputils.okhttp.builder.PostFormBuilder; | ||
9 | +import com.hhmedic.android.sdk.okhttputils.okhttp.builder.PostStringBuilder; | ||
10 | +import com.hhmedic.android.sdk.okhttputils.okhttp.callback.Callback; | ||
11 | +import com.hhmedic.android.sdk.okhttputils.okhttp.request.RequestCall; | ||
12 | +import com.hhmedic.android.sdk.okhttputils.okhttp.utils.Platform; | ||
13 | + | ||
14 | +import java.io.IOException; | ||
15 | +import java.util.concurrent.Executor; | ||
16 | + | ||
17 | +import okhttp3.Call; | ||
18 | +import okhttp3.OkHttpClient; | ||
19 | +import okhttp3.Response; | ||
20 | + | ||
21 | +/** | ||
22 | + * Created by zhy on 15/8/17. | ||
23 | + */ | ||
24 | +public class OkHttpUtils | ||
25 | +{ | ||
26 | + public static final long DEFAULT_MILLISECONDS = 10_000L; | ||
27 | + private volatile static OkHttpUtils mInstance; | ||
28 | + private OkHttpClient mOkHttpClient; | ||
29 | + private Platform mPlatform; | ||
30 | + | ||
31 | + public OkHttpUtils(OkHttpClient okHttpClient) | ||
32 | + { | ||
33 | + if (okHttpClient == null) | ||
34 | + { | ||
35 | + mOkHttpClient = new OkHttpClient(); | ||
36 | + } else | ||
37 | + { | ||
38 | + mOkHttpClient = okHttpClient; | ||
39 | + } | ||
40 | + | ||
41 | + mPlatform = Platform.get(); | ||
42 | + } | ||
43 | + | ||
44 | + | ||
45 | + public static OkHttpUtils initClient(OkHttpClient okHttpClient) | ||
46 | + { | ||
47 | + if (mInstance == null) | ||
48 | + { | ||
49 | + synchronized (OkHttpUtils.class) | ||
50 | + { | ||
51 | + if (mInstance == null) | ||
52 | + { | ||
53 | + mInstance = new OkHttpUtils(okHttpClient); | ||
54 | + } | ||
55 | + } | ||
56 | + } | ||
57 | + return mInstance; | ||
58 | + } | ||
59 | + | ||
60 | + public static OkHttpUtils getInstance() | ||
61 | + { | ||
62 | + return initClient(null); | ||
63 | + } | ||
64 | + | ||
65 | + | ||
66 | + public Executor getDelivery() | ||
67 | + { | ||
68 | + return mPlatform.defaultCallbackExecutor(); | ||
69 | + } | ||
70 | + | ||
71 | + public OkHttpClient getOkHttpClient() | ||
72 | + { | ||
73 | + return mOkHttpClient; | ||
74 | + } | ||
75 | + | ||
76 | + public static GetBuilder get() | ||
77 | + { | ||
78 | + return new GetBuilder(); | ||
79 | + } | ||
80 | + | ||
81 | + public static PostStringBuilder postString() | ||
82 | + { | ||
83 | + return new PostStringBuilder(); | ||
84 | + } | ||
85 | + | ||
86 | + public static PostFileBuilder postFile() | ||
87 | + { | ||
88 | + return new PostFileBuilder(); | ||
89 | + } | ||
90 | + | ||
91 | + public static PostFormBuilder post() | ||
92 | + { | ||
93 | + return new PostFormBuilder(); | ||
94 | + } | ||
95 | + | ||
96 | + public static OtherRequestBuilder put() | ||
97 | + { | ||
98 | + return new OtherRequestBuilder(METHOD.PUT); | ||
99 | + } | ||
100 | + | ||
101 | + public static HeadBuilder head() | ||
102 | + { | ||
103 | + return new HeadBuilder(); | ||
104 | + } | ||
105 | + | ||
106 | + public static OtherRequestBuilder delete() | ||
107 | + { | ||
108 | + return new OtherRequestBuilder(METHOD.DELETE); | ||
109 | + } | ||
110 | + | ||
111 | + public static OtherRequestBuilder patch() | ||
112 | + { | ||
113 | + return new OtherRequestBuilder(METHOD.PATCH); | ||
114 | + } | ||
115 | + | ||
116 | + public void execute(final RequestCall requestCall, Callback callback) | ||
117 | + { | ||
118 | + if (callback == null) | ||
119 | + callback = Callback.CALLBACK_DEFAULT; | ||
120 | + final Callback finalCallback = callback; | ||
121 | + final int id = requestCall.getOkHttpRequest().getId(); | ||
122 | + | ||
123 | + requestCall.getCall().enqueue(new okhttp3.Callback() | ||
124 | + { | ||
125 | + @Override | ||
126 | + public void onFailure(Call call, final IOException e) | ||
127 | + { | ||
128 | + sendFailResultCallback(call, e, finalCallback, id); | ||
129 | + } | ||
130 | + | ||
131 | + @Override | ||
132 | + public void onResponse(final Call call, final Response response) | ||
133 | + { | ||
134 | + try | ||
135 | + { | ||
136 | + if (call.isCanceled()) | ||
137 | + { | ||
138 | + sendFailResultCallback(call, new IOException("Canceled!"), finalCallback, id); | ||
139 | + return; | ||
140 | + } | ||
141 | + | ||
142 | + if (!finalCallback.validateReponse(response, id)) | ||
143 | + { | ||
144 | + sendFailResultCallback(call, new IOException("request failed , reponse's code is : " + response.code()), finalCallback, id); | ||
145 | + return; | ||
146 | + } | ||
147 | + | ||
148 | + Object o = finalCallback.parseNetworkResponse(response, id); | ||
149 | + sendSuccessResultCallback(o, finalCallback, id); | ||
150 | + } catch (Exception e) | ||
151 | + { | ||
152 | + sendFailResultCallback(call, e, finalCallback, id); | ||
153 | + } finally | ||
154 | + { | ||
155 | + if (response.body() != null) | ||
156 | + response.body().close(); | ||
157 | + } | ||
158 | + | ||
159 | + } | ||
160 | + }); | ||
161 | + } | ||
162 | + | ||
163 | + | ||
164 | + public void sendFailResultCallback(final Call call, final Exception e, final Callback callback, final int id) | ||
165 | + { | ||
166 | + if (callback == null) return; | ||
167 | + | ||
168 | + mPlatform.execute(new Runnable() | ||
169 | + { | ||
170 | + @Override | ||
171 | + public void run() | ||
172 | + { | ||
173 | + callback.onError(call, e, id); | ||
174 | + callback.onAfter(id); | ||
175 | + } | ||
176 | + }); | ||
177 | + } | ||
178 | + | ||
179 | + public void sendSuccessResultCallback(final Object object, final Callback callback, final int id) | ||
180 | + { | ||
181 | + if (callback == null) return; | ||
182 | + mPlatform.execute(new Runnable() | ||
183 | + { | ||
184 | + @Override | ||
185 | + public void run() | ||
186 | + { | ||
187 | + callback.onResponse(object, id); | ||
188 | + callback.onAfter(id); | ||
189 | + } | ||
190 | + }); | ||
191 | + } | ||
192 | + | ||
193 | + public void cancelTag(Object tag) | ||
194 | + { | ||
195 | + for (Call call : mOkHttpClient.dispatcher().queuedCalls()) | ||
196 | + { | ||
197 | + if (tag.equals(call.request().tag())) | ||
198 | + { | ||
199 | + call.cancel(); | ||
200 | + } | ||
201 | + } | ||
202 | + for (Call call : mOkHttpClient.dispatcher().runningCalls()) | ||
203 | + { | ||
204 | + if (tag.equals(call.request().tag())) | ||
205 | + { | ||
206 | + call.cancel(); | ||
207 | + } | ||
208 | + } | ||
209 | + } | ||
210 | + | ||
211 | + public static class METHOD | ||
212 | + { | ||
213 | + public static final String HEAD = "HEAD"; | ||
214 | + public static final String DELETE = "DELETE"; | ||
215 | + public static final String PUT = "PUT"; | ||
216 | + public static final String PATCH = "PATCH"; | ||
217 | + } | ||
218 | +} | ||
219 | + |
1 | +package com.hhmedic.android.sdk.okhttputils.okhttp.builder; | ||
2 | + | ||
3 | +import android.net.Uri; | ||
4 | + | ||
5 | +import com.hhmedic.android.sdk.okhttputils.okhttp.request.GetRequest; | ||
6 | +import com.hhmedic.android.sdk.okhttputils.okhttp.request.RequestCall; | ||
7 | + | ||
8 | +import java.util.Iterator; | ||
9 | +import java.util.LinkedHashMap; | ||
10 | +import java.util.Map; | ||
11 | +import java.util.Set; | ||
12 | + | ||
13 | +/** | ||
14 | + * Created by zhy on 15/12/14. | ||
15 | + */ | ||
16 | +public class GetBuilder extends OkHttpRequestBuilder<GetBuilder> implements HasParamsable | ||
17 | +{ | ||
18 | + @Override | ||
19 | + public RequestCall build() | ||
20 | + { | ||
21 | + if (params != null) | ||
22 | + { | ||
23 | + url = appendParams(url, params); | ||
24 | + } | ||
25 | + | ||
26 | + return new GetRequest(url, tag, params, headers,id).build(); | ||
27 | + } | ||
28 | + | ||
29 | + protected String appendParams(String url, Map<String, String> params) | ||
30 | + { | ||
31 | + if (url == null || params == null || params.isEmpty()) | ||
32 | + { | ||
33 | + return url; | ||
34 | + } | ||
35 | + Uri.Builder builder = Uri.parse(url).buildUpon(); | ||
36 | + Set<String> keys = params.keySet(); | ||
37 | + Iterator<String> iterator = keys.iterator(); | ||
38 | + while (iterator.hasNext()) | ||
39 | + { | ||
40 | + String key = iterator.next(); | ||
41 | + builder.appendQueryParameter(key, params.get(key)); | ||
42 | + } | ||
43 | + return builder.build().toString(); | ||
44 | + } | ||
45 | + | ||
46 | + | ||
47 | + @Override | ||
48 | + public GetBuilder params(Map<String, String> params) | ||
49 | + { | ||
50 | + this.params = params; | ||
51 | + return this; | ||
52 | + } | ||
53 | + | ||
54 | + @Override | ||
55 | + public GetBuilder addParams(String key, String val) | ||
56 | + { | ||
57 | + if (this.params == null) | ||
58 | + { | ||
59 | + params = new LinkedHashMap<>(); | ||
60 | + } | ||
61 | + params.put(key, val); | ||
62 | + return this; | ||
63 | + } | ||
64 | + | ||
65 | + | ||
66 | +} |
1 | +package com.hhmedic.android.sdk.okhttputils.okhttp.builder; | ||
2 | + | ||
3 | +import java.util.Map; | ||
4 | + | ||
5 | +/** | ||
6 | + * Created by zhy on 16/3/1. | ||
7 | + */ | ||
8 | +public interface HasParamsable | ||
9 | +{ | ||
10 | + OkHttpRequestBuilder params(Map<String, String> params); | ||
11 | + OkHttpRequestBuilder addParams(String key, String val); | ||
12 | +} |
1 | +package com.hhmedic.android.sdk.okhttputils.okhttp.builder; | ||
2 | + | ||
3 | +import com.hhmedic.android.sdk.okhttputils.okhttp.OkHttpUtils; | ||
4 | +import com.hhmedic.android.sdk.okhttputils.okhttp.request.RequestCall; | ||
5 | +import com.hhmedic.android.sdk.okhttputils.okhttp.request.OtherRequest; | ||
6 | + | ||
7 | +/** | ||
8 | + * Created by zhy on 16/3/2. | ||
9 | + */ | ||
10 | +public class HeadBuilder extends GetBuilder | ||
11 | +{ | ||
12 | + @Override | ||
13 | + public RequestCall build() | ||
14 | + { | ||
15 | + return new OtherRequest(null, null, OkHttpUtils.METHOD.HEAD, url, tag, params, headers,id).build(); | ||
16 | + } | ||
17 | +} |
app/src/main/java/com/hhmedic/android/sdk/okhttputils/okhttp/builder/OkHttpRequestBuilder.java
0 → 100644
1 | +package com.hhmedic.android.sdk.okhttputils.okhttp.builder; | ||
2 | + | ||
3 | +import com.hhmedic.android.sdk.okhttputils.okhttp.request.RequestCall; | ||
4 | + | ||
5 | +import java.util.LinkedHashMap; | ||
6 | +import java.util.Map; | ||
7 | + | ||
8 | +/** | ||
9 | + * Created by zhy on 15/12/14. | ||
10 | + */ | ||
11 | +public abstract class OkHttpRequestBuilder<T extends OkHttpRequestBuilder> | ||
12 | +{ | ||
13 | + protected String url; | ||
14 | + protected Object tag; | ||
15 | + protected Map<String, String> headers; | ||
16 | + protected Map<String, String> params; | ||
17 | + protected int id; | ||
18 | + | ||
19 | + public T id(int id) | ||
20 | + { | ||
21 | + this.id = id; | ||
22 | + return (T) this; | ||
23 | + } | ||
24 | + | ||
25 | + public T url(String url) | ||
26 | + { | ||
27 | + this.url = url; | ||
28 | + return (T) this; | ||
29 | + } | ||
30 | + | ||
31 | + | ||
32 | + public T tag(Object tag) | ||
33 | + { | ||
34 | + this.tag = tag; | ||
35 | + return (T) this; | ||
36 | + } | ||
37 | + | ||
38 | + public T headers(Map<String, String> headers) | ||
39 | + { | ||
40 | + this.headers = headers; | ||
41 | + return (T) this; | ||
42 | + } | ||
43 | + | ||
44 | + public T addHeader(String key, String val) | ||
45 | + { | ||
46 | + if (this.headers == null) | ||
47 | + { | ||
48 | + headers = new LinkedHashMap<>(); | ||
49 | + } | ||
50 | + headers.put(key, val); | ||
51 | + return (T) this; | ||
52 | + } | ||
53 | + | ||
54 | + public abstract RequestCall build(); | ||
55 | +} |
app/src/main/java/com/hhmedic/android/sdk/okhttputils/okhttp/builder/OtherRequestBuilder.java
0 → 100644
1 | +package com.hhmedic.android.sdk.okhttputils.okhttp.builder; | ||
2 | + | ||
3 | +import com.hhmedic.android.sdk.okhttputils.okhttp.request.OtherRequest; | ||
4 | +import com.hhmedic.android.sdk.okhttputils.okhttp.request.RequestCall; | ||
5 | + | ||
6 | +import okhttp3.RequestBody; | ||
7 | + | ||
8 | + | ||
9 | +/** | ||
10 | + * DELETE、PUT、PATCH等其他方法 | ||
11 | + */ | ||
12 | +public class OtherRequestBuilder extends OkHttpRequestBuilder<OtherRequestBuilder> | ||
13 | +{ | ||
14 | + private RequestBody requestBody; | ||
15 | + private String method; | ||
16 | + private String content; | ||
17 | + | ||
18 | + public OtherRequestBuilder(String method) | ||
19 | + { | ||
20 | + this.method = method; | ||
21 | + } | ||
22 | + | ||
23 | + @Override | ||
24 | + public RequestCall build() | ||
25 | + { | ||
26 | + return new OtherRequest(requestBody, content, method, url, tag, params, headers,id).build(); | ||
27 | + } | ||
28 | + | ||
29 | + public OtherRequestBuilder requestBody(RequestBody requestBody) | ||
30 | + { | ||
31 | + this.requestBody = requestBody; | ||
32 | + return this; | ||
33 | + } | ||
34 | + | ||
35 | + public OtherRequestBuilder requestBody(String content) | ||
36 | + { | ||
37 | + this.content = content; | ||
38 | + return this; | ||
39 | + } | ||
40 | + | ||
41 | + | ||
42 | +} |
app/src/main/java/com/hhmedic/android/sdk/okhttputils/okhttp/builder/PostFileBuilder.java
0 → 100644
1 | +package com.hhmedic.android.sdk.okhttputils.okhttp.builder; | ||
2 | + | ||
3 | +import com.hhmedic.android.sdk.okhttputils.okhttp.request.PostFileRequest; | ||
4 | +import com.hhmedic.android.sdk.okhttputils.okhttp.request.RequestCall; | ||
5 | + | ||
6 | +import java.io.File; | ||
7 | + | ||
8 | +import okhttp3.MediaType; | ||
9 | + | ||
10 | +/** | ||
11 | + * Created by zhy on 15/12/14. | ||
12 | + */ | ||
13 | +public class PostFileBuilder extends OkHttpRequestBuilder<PostFileBuilder> | ||
14 | +{ | ||
15 | + private File file; | ||
16 | + private MediaType mediaType; | ||
17 | + | ||
18 | + | ||
19 | + public OkHttpRequestBuilder file(File file) | ||
20 | + { | ||
21 | + this.file = file; | ||
22 | + return this; | ||
23 | + } | ||
24 | + | ||
25 | + public OkHttpRequestBuilder mediaType(MediaType mediaType) | ||
26 | + { | ||
27 | + this.mediaType = mediaType; | ||
28 | + return this; | ||
29 | + } | ||
30 | + | ||
31 | + @Override | ||
32 | + public RequestCall build() | ||
33 | + { | ||
34 | + return new PostFileRequest(url, tag, params, headers, file, mediaType,id).build(); | ||
35 | + } | ||
36 | + | ||
37 | + | ||
38 | +} |
app/src/main/java/com/hhmedic/android/sdk/okhttputils/okhttp/builder/PostFormBuilder.java
0 → 100644
1 | +package com.hhmedic.android.sdk.okhttputils.okhttp.builder; | ||
2 | + | ||
3 | +import com.hhmedic.android.sdk.okhttputils.okhttp.request.PostFormRequest; | ||
4 | +import com.hhmedic.android.sdk.okhttputils.okhttp.request.RequestCall; | ||
5 | + | ||
6 | +import java.io.File; | ||
7 | +import java.util.ArrayList; | ||
8 | +import java.util.LinkedHashMap; | ||
9 | +import java.util.List; | ||
10 | +import java.util.Map; | ||
11 | + | ||
12 | +/** | ||
13 | + * Created by zhy on 15/12/14. | ||
14 | + */ | ||
15 | +public class PostFormBuilder extends OkHttpRequestBuilder<PostFormBuilder> implements HasParamsable | ||
16 | +{ | ||
17 | + private List<FileInput> files = new ArrayList<>(); | ||
18 | + | ||
19 | + @Override | ||
20 | + public RequestCall build() | ||
21 | + { | ||
22 | + return new PostFormRequest(url, tag, params, headers, files,id).build(); | ||
23 | + } | ||
24 | + | ||
25 | + public PostFormBuilder files(String key, Map<String, File> files) | ||
26 | + { | ||
27 | + for (String filename : files.keySet()) | ||
28 | + { | ||
29 | + this.files.add(new FileInput(key, filename, files.get(filename))); | ||
30 | + } | ||
31 | + return this; | ||
32 | + } | ||
33 | + | ||
34 | + public PostFormBuilder addFile(String name, String filename, File file) | ||
35 | + { | ||
36 | + files.add(new FileInput(name, filename, file)); | ||
37 | + return this; | ||
38 | + } | ||
39 | + | ||
40 | + public static class FileInput | ||
41 | + { | ||
42 | + public String key; | ||
43 | + public String filename; | ||
44 | + public File file; | ||
45 | + | ||
46 | + public FileInput(String name, String filename, File file) | ||
47 | + { | ||
48 | + this.key = name; | ||
49 | + this.filename = filename; | ||
50 | + this.file = file; | ||
51 | + } | ||
52 | + | ||
53 | + @Override | ||
54 | + public String toString() | ||
55 | + { | ||
56 | + return "FileInput{" + | ||
57 | + "key='" + key + '\'' + | ||
58 | + ", filename='" + filename + '\'' + | ||
59 | + ", file=" + file + | ||
60 | + '}'; | ||
61 | + } | ||
62 | + } | ||
63 | + | ||
64 | + | ||
65 | + | ||
66 | + @Override | ||
67 | + public PostFormBuilder params(Map<String, String> params) | ||
68 | + { | ||
69 | + this.params = params; | ||
70 | + return this; | ||
71 | + } | ||
72 | + | ||
73 | + @Override | ||
74 | + public PostFormBuilder addParams(String key, String val) | ||
75 | + { | ||
76 | + if (this.params == null) | ||
77 | + { | ||
78 | + params = new LinkedHashMap<>(); | ||
79 | + } | ||
80 | + params.put(key, val); | ||
81 | + return this; | ||
82 | + } | ||
83 | + | ||
84 | + | ||
85 | + | ||
86 | + | ||
87 | +} |
app/src/main/java/com/hhmedic/android/sdk/okhttputils/okhttp/builder/PostStringBuilder.java
0 → 100644
1 | +package com.hhmedic.android.sdk.okhttputils.okhttp.builder; | ||
2 | + | ||
3 | +import com.hhmedic.android.sdk.okhttputils.okhttp.request.PostStringRequest; | ||
4 | +import com.hhmedic.android.sdk.okhttputils.okhttp.request.RequestCall; | ||
5 | + | ||
6 | +import okhttp3.MediaType; | ||
7 | + | ||
8 | +/** | ||
9 | + * Created by zhy on 15/12/14. | ||
10 | + */ | ||
11 | +public class PostStringBuilder extends OkHttpRequestBuilder<PostStringBuilder> | ||
12 | +{ | ||
13 | + private String content; | ||
14 | + private MediaType mediaType; | ||
15 | + | ||
16 | + | ||
17 | + public PostStringBuilder content(String content) | ||
18 | + { | ||
19 | + this.content = content; | ||
20 | + return this; | ||
21 | + } | ||
22 | + | ||
23 | + public PostStringBuilder mediaType(MediaType mediaType) | ||
24 | + { | ||
25 | + this.mediaType = mediaType; | ||
26 | + return this; | ||
27 | + } | ||
28 | + | ||
29 | + @Override | ||
30 | + public RequestCall build() | ||
31 | + { | ||
32 | + return new PostStringRequest(url, tag, params, headers, content, mediaType,id).build(); | ||
33 | + } | ||
34 | + | ||
35 | + | ||
36 | +} |
app/src/main/java/com/hhmedic/android/sdk/okhttputils/okhttp/callback/BitmapCallback.java
0 → 100644
1 | +package com.hhmedic.android.sdk.okhttputils.okhttp.callback; | ||
2 | + | ||
3 | +import android.graphics.Bitmap; | ||
4 | +import android.graphics.BitmapFactory; | ||
5 | + | ||
6 | +import okhttp3.Response; | ||
7 | + | ||
8 | +/** | ||
9 | + * Created by zhy on 15/12/14. | ||
10 | + */ | ||
11 | +public abstract class BitmapCallback extends Callback<Bitmap> | ||
12 | +{ | ||
13 | + @Override | ||
14 | + public Bitmap parseNetworkResponse(Response response , int id) throws Exception | ||
15 | + { | ||
16 | + return BitmapFactory.decodeStream(response.body().byteStream()); | ||
17 | + } | ||
18 | + | ||
19 | +} |
1 | +package com.hhmedic.android.sdk.okhttputils.okhttp.callback; | ||
2 | + | ||
3 | +import okhttp3.Call; | ||
4 | +import okhttp3.Request; | ||
5 | +import okhttp3.Response; | ||
6 | + | ||
7 | +public abstract class Callback<T> | ||
8 | +{ | ||
9 | + /** | ||
10 | + * UI Thread | ||
11 | + * | ||
12 | + * @param request | ||
13 | + */ | ||
14 | + public void onBefore(Request request, int id) | ||
15 | + { | ||
16 | + } | ||
17 | + | ||
18 | + /** | ||
19 | + * UI Thread | ||
20 | + * | ||
21 | + * @param | ||
22 | + */ | ||
23 | + public void onAfter(int id) | ||
24 | + { | ||
25 | + } | ||
26 | + | ||
27 | + /** | ||
28 | + * UI Thread | ||
29 | + * | ||
30 | + * @param progress | ||
31 | + */ | ||
32 | + public void inProgress(float progress, long total , int id) | ||
33 | + { | ||
34 | + | ||
35 | + } | ||
36 | + | ||
37 | + /** | ||
38 | + * if you parse reponse code in parseNetworkResponse, you should make this method return true. | ||
39 | + * | ||
40 | + * @param response | ||
41 | + * @return | ||
42 | + */ | ||
43 | + public boolean validateReponse(Response response, int id) | ||
44 | + { | ||
45 | + return response.isSuccessful(); | ||
46 | + } | ||
47 | + | ||
48 | + /** | ||
49 | + * Thread Pool Thread | ||
50 | + * | ||
51 | + * @param response | ||
52 | + */ | ||
53 | + public abstract T parseNetworkResponse(Response response, int id) throws Exception; | ||
54 | + | ||
55 | + public abstract void onError(Call call, Exception e, int id); | ||
56 | + | ||
57 | + public abstract void onResponse(T response, int id); | ||
58 | + | ||
59 | + | ||
60 | + public static Callback CALLBACK_DEFAULT = new Callback() | ||
61 | + { | ||
62 | + | ||
63 | + @Override | ||
64 | + public Object parseNetworkResponse(Response response, int id) throws Exception | ||
65 | + { | ||
66 | + return null; | ||
67 | + } | ||
68 | + | ||
69 | + @Override | ||
70 | + public void onError(Call call, Exception e, int id) | ||
71 | + { | ||
72 | + | ||
73 | + } | ||
74 | + | ||
75 | + @Override | ||
76 | + public void onResponse(Object response, int id) | ||
77 | + { | ||
78 | + | ||
79 | + } | ||
80 | + }; | ||
81 | + | ||
82 | +} |
1 | +package com.hhmedic.android.sdk.okhttputils.okhttp.callback; | ||
2 | + | ||
3 | +import com.hhmedic.android.sdk.okhttputils.okhttp.OkHttpUtils; | ||
4 | + | ||
5 | +import java.io.File; | ||
6 | +import java.io.FileOutputStream; | ||
7 | +import java.io.IOException; | ||
8 | +import java.io.InputStream; | ||
9 | + | ||
10 | +import okhttp3.Response; | ||
11 | + | ||
12 | +/** | ||
13 | + * Created by zhy on 15/12/15. | ||
14 | + */ | ||
15 | +public abstract class FileCallBack extends Callback<File> | ||
16 | +{ | ||
17 | + /** | ||
18 | + * 目标文件存储的文件夹路径 | ||
19 | + */ | ||
20 | + private String destFileDir; | ||
21 | + /** | ||
22 | + * 目标文件存储的文件名 | ||
23 | + */ | ||
24 | + private String destFileName; | ||
25 | + | ||
26 | + | ||
27 | + public FileCallBack(String destFileDir, String destFileName) | ||
28 | + { | ||
29 | + this.destFileDir = destFileDir; | ||
30 | + this.destFileName = destFileName; | ||
31 | + } | ||
32 | + | ||
33 | + | ||
34 | + @Override | ||
35 | + public File parseNetworkResponse(Response response, int id) throws Exception | ||
36 | + { | ||
37 | + return saveFile(response,id); | ||
38 | + } | ||
39 | + | ||
40 | + | ||
41 | + public File saveFile(Response response,final int id) throws IOException | ||
42 | + { | ||
43 | + InputStream is = null; | ||
44 | + byte[] buf = new byte[2048]; | ||
45 | + int len = 0; | ||
46 | + FileOutputStream fos = null; | ||
47 | + try | ||
48 | + { | ||
49 | + is = response.body().byteStream(); | ||
50 | + final long total = response.body().contentLength(); | ||
51 | + | ||
52 | + long sum = 0; | ||
53 | + | ||
54 | + File dir = new File(destFileDir); | ||
55 | + if (!dir.exists()) | ||
56 | + { | ||
57 | + dir.mkdirs(); | ||
58 | + } | ||
59 | + File file = new File(dir, destFileName); | ||
60 | + fos = new FileOutputStream(file); | ||
61 | + while ((len = is.read(buf)) != -1) | ||
62 | + { | ||
63 | + sum += len; | ||
64 | + fos.write(buf, 0, len); | ||
65 | + final long finalSum = sum; | ||
66 | + OkHttpUtils.getInstance().getDelivery().execute(new Runnable() | ||
67 | + { | ||
68 | + @Override | ||
69 | + public void run() | ||
70 | + { | ||
71 | + | ||
72 | + inProgress(finalSum * 1.0f / total,total,id); | ||
73 | + } | ||
74 | + }); | ||
75 | + } | ||
76 | + fos.flush(); | ||
77 | + | ||
78 | + return file; | ||
79 | + | ||
80 | + } finally | ||
81 | + { | ||
82 | + try | ||
83 | + { | ||
84 | + response.body().close(); | ||
85 | + if (is != null) is.close(); | ||
86 | + } catch (IOException e) | ||
87 | + { | ||
88 | + } | ||
89 | + try | ||
90 | + { | ||
91 | + if (fos != null) fos.close(); | ||
92 | + } catch (IOException e) | ||
93 | + { | ||
94 | + } | ||
95 | + | ||
96 | + } | ||
97 | + } | ||
98 | + | ||
99 | + | ||
100 | +} |
app/src/main/java/com/hhmedic/android/sdk/okhttputils/okhttp/callback/GenericsCallback.java
0 → 100644
1 | +package com.hhmedic.android.sdk.okhttputils.okhttp.callback; | ||
2 | + | ||
3 | +import com.hhmedic.android.sdk.okhttputils.okhttp.callback.Callback; | ||
4 | + | ||
5 | +import java.io.IOException; | ||
6 | +import java.lang.reflect.ParameterizedType; | ||
7 | + | ||
8 | +import okhttp3.Response; | ||
9 | + | ||
10 | +/** | ||
11 | + * Created by JimGong on 2016/6/23. | ||
12 | + */ | ||
13 | + | ||
14 | +public abstract class GenericsCallback<T> extends Callback<T> { | ||
15 | + IGenericsSerializator mGenericsSerializator; | ||
16 | + | ||
17 | + public GenericsCallback(IGenericsSerializator serializator) { | ||
18 | + mGenericsSerializator = serializator; | ||
19 | + } | ||
20 | + | ||
21 | + @Override | ||
22 | + public T parseNetworkResponse(Response response, int id) throws IOException { | ||
23 | + String string = response.body().string(); | ||
24 | + Class<T> entityClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; | ||
25 | + if (entityClass == String.class) { | ||
26 | + return (T) string; | ||
27 | + } | ||
28 | + T bean = mGenericsSerializator.transform(string, entityClass); | ||
29 | + return bean; | ||
30 | + } | ||
31 | + | ||
32 | +} |
app/src/main/java/com/hhmedic/android/sdk/okhttputils/okhttp/callback/StringCallback.java
0 → 100644
1 | +package com.hhmedic.android.sdk.okhttputils.okhttp.callback; | ||
2 | + | ||
3 | +import com.hhmedic.android.sdk.okhttputils.okhttp.callback.Callback; | ||
4 | + | ||
5 | +import java.io.IOException; | ||
6 | + | ||
7 | +import okhttp3.Response; | ||
8 | + | ||
9 | +/** | ||
10 | + * Created by zhy on 15/12/14. | ||
11 | + */ | ||
12 | +public abstract class StringCallback extends Callback<String> | ||
13 | +{ | ||
14 | + @Override | ||
15 | + public String parseNetworkResponse(Response response, int id) throws IOException | ||
16 | + { | ||
17 | + return response.body().string(); | ||
18 | + } | ||
19 | +} |
1 | +package com.hhmedic.android.sdk.okhttputils.okhttp.cookie; | ||
2 | + | ||
3 | +import com.hhmedic.android.sdk.okhttputils.okhttp.cookie.store.CookieStore; | ||
4 | +import com.hhmedic.android.sdk.okhttputils.okhttp.utils.Exceptions; | ||
5 | + | ||
6 | +import java.util.List; | ||
7 | + | ||
8 | +import okhttp3.Cookie; | ||
9 | +import okhttp3.CookieJar; | ||
10 | +import okhttp3.HttpUrl; | ||
11 | + | ||
12 | +/** | ||
13 | + * Created by zhy on 16/3/10. | ||
14 | + */ | ||
15 | +public class CookieJarImpl implements CookieJar | ||
16 | +{ | ||
17 | + private CookieStore cookieStore; | ||
18 | + | ||
19 | + public CookieJarImpl(CookieStore cookieStore) | ||
20 | + { | ||
21 | + if (cookieStore == null) Exceptions.illegalArgument("cookieStore can not be null."); | ||
22 | + this.cookieStore = cookieStore; | ||
23 | + } | ||
24 | + | ||
25 | + @Override | ||
26 | + public synchronized void saveFromResponse(HttpUrl url, List<Cookie> cookies) | ||
27 | + { | ||
28 | + cookieStore.add(url, cookies); | ||
29 | + } | ||
30 | + | ||
31 | + @Override | ||
32 | + public synchronized List<Cookie> loadForRequest(HttpUrl url) | ||
33 | + { | ||
34 | + return cookieStore.get(url); | ||
35 | + } | ||
36 | + | ||
37 | + public CookieStore getCookieStore() | ||
38 | + { | ||
39 | + return cookieStore; | ||
40 | + } | ||
41 | +} |
app/src/main/java/com/hhmedic/android/sdk/okhttputils/okhttp/cookie/store/CookieStore.java
0 → 100644
1 | +package com.hhmedic.android.sdk.okhttputils.okhttp.cookie.store; | ||
2 | + | ||
3 | +import java.util.List; | ||
4 | + | ||
5 | +import okhttp3.Cookie; | ||
6 | +import okhttp3.HttpUrl; | ||
7 | + | ||
8 | +public interface CookieStore | ||
9 | +{ | ||
10 | + | ||
11 | + void add(HttpUrl uri, List<Cookie> cookie); | ||
12 | + | ||
13 | + List<Cookie> get(HttpUrl uri); | ||
14 | + | ||
15 | + List<Cookie> getCookies(); | ||
16 | + | ||
17 | + boolean remove(HttpUrl uri, Cookie cookie); | ||
18 | + | ||
19 | + boolean removeAll(); | ||
20 | + | ||
21 | +} |
app/src/main/java/com/hhmedic/android/sdk/okhttputils/okhttp/cookie/store/MemoryCookieStore.java
0 → 100644
1 | +package com.hhmedic.android.sdk.okhttputils.okhttp.cookie.store; | ||
2 | + | ||
3 | +import java.util.ArrayList; | ||
4 | +import java.util.HashMap; | ||
5 | +import java.util.Iterator; | ||
6 | +import java.util.List; | ||
7 | +import java.util.Set; | ||
8 | + | ||
9 | +import okhttp3.Cookie; | ||
10 | +import okhttp3.HttpUrl; | ||
11 | + | ||
12 | +/** | ||
13 | + * Created by zhy on 16/3/10. | ||
14 | + */ | ||
15 | +public class MemoryCookieStore implements CookieStore | ||
16 | +{ | ||
17 | + private final HashMap<String, List<Cookie>> allCookies = new HashMap<>(); | ||
18 | + | ||
19 | + @Override | ||
20 | + public void add(HttpUrl url, List<Cookie> cookies) | ||
21 | + { | ||
22 | + List<Cookie> oldCookies = allCookies.get(url.host()); | ||
23 | + | ||
24 | + if (oldCookies != null) | ||
25 | + { | ||
26 | + Iterator<Cookie> itNew = cookies.iterator(); | ||
27 | + Iterator<Cookie> itOld = oldCookies.iterator(); | ||
28 | + while (itNew.hasNext()) | ||
29 | + { | ||
30 | + String va = itNew.next().name(); | ||
31 | + while (va != null && itOld.hasNext()) | ||
32 | + { | ||
33 | + String v = itOld.next().name(); | ||
34 | + if (v != null && va.equals(v)) | ||
35 | + { | ||
36 | + itOld.remove(); | ||
37 | + } | ||
38 | + } | ||
39 | + } | ||
40 | + oldCookies.addAll(cookies); | ||
41 | + } else | ||
42 | + { | ||
43 | + allCookies.put(url.host(), cookies); | ||
44 | + } | ||
45 | + | ||
46 | + | ||
47 | + } | ||
48 | + | ||
49 | + @Override | ||
50 | + public List<Cookie> get(HttpUrl uri) | ||
51 | + { | ||
52 | + List<Cookie> cookies = allCookies.get(uri.host()); | ||
53 | + if (cookies == null) | ||
54 | + { | ||
55 | + cookies = new ArrayList<>(); | ||
56 | + allCookies.put(uri.host(), cookies); | ||
57 | + } | ||
58 | + return cookies; | ||
59 | + | ||
60 | + } | ||
61 | + | ||
62 | + @Override | ||
63 | + public boolean removeAll() | ||
64 | + { | ||
65 | + allCookies.clear(); | ||
66 | + return true; | ||
67 | + } | ||
68 | + | ||
69 | + @Override | ||
70 | + public List<Cookie> getCookies() | ||
71 | + { | ||
72 | + List<Cookie> cookies = new ArrayList<>(); | ||
73 | + Set<String> httpUrls = allCookies.keySet(); | ||
74 | + for (String url : httpUrls) | ||
75 | + { | ||
76 | + cookies.addAll(allCookies.get(url)); | ||
77 | + } | ||
78 | + return cookies; | ||
79 | + } | ||
80 | + | ||
81 | + | ||
82 | + @Override | ||
83 | + public boolean remove(HttpUrl uri, Cookie cookie) | ||
84 | + { | ||
85 | + List<Cookie> cookies = allCookies.get(uri.host()); | ||
86 | + if (cookie != null) | ||
87 | + { | ||
88 | + return cookies.remove(cookie); | ||
89 | + } | ||
90 | + return false; | ||
91 | + } | ||
92 | + | ||
93 | + | ||
94 | +} |
app/src/main/java/com/hhmedic/android/sdk/okhttputils/okhttp/cookie/store/PersistentCookieStore.java
0 → 100644
1 | +package com.hhmedic.android.sdk.okhttputils.okhttp.cookie.store; | ||
2 | + | ||
3 | +import android.content.Context; | ||
4 | +import android.content.SharedPreferences; | ||
5 | +import android.text.TextUtils; | ||
6 | +import android.util.Log; | ||
7 | + | ||
8 | +import java.io.ByteArrayInputStream; | ||
9 | +import java.io.ByteArrayOutputStream; | ||
10 | +import java.io.IOException; | ||
11 | +import java.io.ObjectInputStream; | ||
12 | +import java.io.ObjectOutputStream; | ||
13 | +import java.util.ArrayList; | ||
14 | +import java.util.Collection; | ||
15 | +import java.util.HashMap; | ||
16 | +import java.util.List; | ||
17 | +import java.util.Locale; | ||
18 | +import java.util.Map; | ||
19 | +import java.util.concurrent.ConcurrentHashMap; | ||
20 | + | ||
21 | +import okhttp3.Cookie; | ||
22 | +import okhttp3.HttpUrl; | ||
23 | + | ||
24 | +/** | ||
25 | + * <pre> | ||
26 | + * OkHttpClient client = new OkHttpClient.Builder() | ||
27 | + * .cookieJar(new JavaNetCookieJar(new CookieManager( | ||
28 | + * new PersistentCookieStore(getApplicationContext()), | ||
29 | + * CookiePolicy.ACCEPT_ALL)) | ||
30 | + * .build(); | ||
31 | + * | ||
32 | + * </pre> | ||
33 | + * <p/> | ||
34 | + * from http://stackoverflow.com/questions/25461792/persistent-cookie-store-using-okhttp-2-on-android | ||
35 | + * <p/> | ||
36 | + * <br/> | ||
37 | + * A persistent cookie store which implements the Apache HttpClient CookieStore interface. | ||
38 | + * Cookies are stored and will persist on the user's device between application sessions since they | ||
39 | + * are serialized and stored in SharedPreferences. Instances of this class are | ||
40 | + * designed to be used with AsyncHttpClient#setCookieStore, but can also be used with a | ||
41 | + * regular old apache HttpClient/HttpContext if you prefer. | ||
42 | + */ | ||
43 | +public class PersistentCookieStore implements CookieStore | ||
44 | +{ | ||
45 | + | ||
46 | + private static final String LOG_TAG = "PersistentCookieStore"; | ||
47 | + private static final String COOKIE_PREFS = "CookiePrefsFile"; | ||
48 | + private static final String COOKIE_NAME_PREFIX = "cookie_"; | ||
49 | + | ||
50 | + private final HashMap<String, ConcurrentHashMap<String, Cookie>> cookies; | ||
51 | + private final SharedPreferences cookiePrefs; | ||
52 | + | ||
53 | + /** | ||
54 | + * Construct a persistent cookie store. | ||
55 | + * | ||
56 | + * @param context Context to attach cookie store to | ||
57 | + */ | ||
58 | + public PersistentCookieStore(Context context) | ||
59 | + { | ||
60 | + cookiePrefs = context.getSharedPreferences(COOKIE_PREFS, 0); | ||
61 | + cookies = new HashMap<String, ConcurrentHashMap<String, Cookie>>(); | ||
62 | + | ||
63 | + // Load any previously stored cookies into the store | ||
64 | + Map<String, ?> prefsMap = cookiePrefs.getAll(); | ||
65 | + for (Map.Entry<String, ?> entry : prefsMap.entrySet()) | ||
66 | + { | ||
67 | + if (((String) entry.getValue()) != null && !((String) entry.getValue()).startsWith(COOKIE_NAME_PREFIX)) | ||
68 | + { | ||
69 | + String[] cookieNames = TextUtils.split((String) entry.getValue(), ","); | ||
70 | + for (String name : cookieNames) | ||
71 | + { | ||
72 | + String encodedCookie = cookiePrefs.getString(COOKIE_NAME_PREFIX + name, null); | ||
73 | + if (encodedCookie != null) | ||
74 | + { | ||
75 | + Cookie decodedCookie = decodeCookie(encodedCookie); | ||
76 | + if (decodedCookie != null) | ||
77 | + { | ||
78 | + if (!cookies.containsKey(entry.getKey())) | ||
79 | + cookies.put(entry.getKey(), new ConcurrentHashMap<String, Cookie>()); | ||
80 | + cookies.get(entry.getKey()).put(name, decodedCookie); | ||
81 | + } | ||
82 | + } | ||
83 | + } | ||
84 | + | ||
85 | + } | ||
86 | + } | ||
87 | + } | ||
88 | + | ||
89 | + protected void add(HttpUrl uri, Cookie cookie) | ||
90 | + { | ||
91 | + String name = getCookieToken(cookie); | ||
92 | + | ||
93 | + if (cookie.persistent()) | ||
94 | + { | ||
95 | + if (!cookies.containsKey(uri.host())) | ||
96 | + { | ||
97 | + cookies.put(uri.host(), new ConcurrentHashMap<String, Cookie>()); | ||
98 | + } | ||
99 | + cookies.get(uri.host()).put(name, cookie); | ||
100 | + } else | ||
101 | + { | ||
102 | + if (cookies.containsKey(uri.host())) | ||
103 | + { | ||
104 | + cookies.get(uri.host()).remove(name); | ||
105 | + }else | ||
106 | + { | ||
107 | + return ; | ||
108 | + } | ||
109 | + } | ||
110 | + | ||
111 | + // Save cookie into persistent store | ||
112 | + SharedPreferences.Editor prefsWriter = cookiePrefs.edit(); | ||
113 | + prefsWriter.putString(uri.host(), TextUtils.join(",", cookies.get(uri.host()).keySet())); | ||
114 | + prefsWriter.putString(COOKIE_NAME_PREFIX + name, encodeCookie(new SerializableHttpCookie(cookie))); | ||
115 | + prefsWriter.apply(); | ||
116 | + } | ||
117 | + | ||
118 | + protected String getCookieToken(Cookie cookie) | ||
119 | + { | ||
120 | + return cookie.name() + cookie.domain(); | ||
121 | + } | ||
122 | + | ||
123 | + @Override | ||
124 | + public void add(HttpUrl uri, List<Cookie> cookies) | ||
125 | + { | ||
126 | + for (Cookie cookie : cookies) | ||
127 | + { | ||
128 | + add(uri, cookie); | ||
129 | + } | ||
130 | + } | ||
131 | + | ||
132 | + @Override | ||
133 | + public List<Cookie> get(HttpUrl uri) | ||
134 | + { | ||
135 | + ArrayList<Cookie> ret = new ArrayList<Cookie>(); | ||
136 | + if (cookies.containsKey(uri.host())) | ||
137 | + { | ||
138 | + Collection<Cookie> cookies = this.cookies.get(uri.host()).values(); | ||
139 | + for (Cookie cookie : cookies) | ||
140 | + { | ||
141 | + if (isCookieExpired(cookie)) | ||
142 | + { | ||
143 | + remove(uri, cookie); | ||
144 | + } else | ||
145 | + { | ||
146 | + ret.add(cookie); | ||
147 | + } | ||
148 | + } | ||
149 | + } | ||
150 | + | ||
151 | + return ret; | ||
152 | + } | ||
153 | + | ||
154 | + private static boolean isCookieExpired(Cookie cookie) | ||
155 | + { | ||
156 | + return cookie.expiresAt() < System.currentTimeMillis(); | ||
157 | + } | ||
158 | + | ||
159 | + @Override | ||
160 | + public boolean removeAll() | ||
161 | + { | ||
162 | + SharedPreferences.Editor prefsWriter = cookiePrefs.edit(); | ||
163 | + prefsWriter.clear(); | ||
164 | + prefsWriter.apply(); | ||
165 | + cookies.clear(); | ||
166 | + return true; | ||
167 | + } | ||
168 | + | ||
169 | + | ||
170 | + @Override | ||
171 | + public boolean remove(HttpUrl uri, Cookie cookie) | ||
172 | + { | ||
173 | + String name = getCookieToken(cookie); | ||
174 | + | ||
175 | + if (cookies.containsKey(uri.host()) && cookies.get(uri.host()).containsKey(name)) | ||
176 | + { | ||
177 | + cookies.get(uri.host()).remove(name); | ||
178 | + | ||
179 | + SharedPreferences.Editor prefsWriter = cookiePrefs.edit(); | ||
180 | + if (cookiePrefs.contains(COOKIE_NAME_PREFIX + name)) | ||
181 | + { | ||
182 | + prefsWriter.remove(COOKIE_NAME_PREFIX + name); | ||
183 | + } | ||
184 | + prefsWriter.putString(uri.host(), TextUtils.join(",", cookies.get(uri.host()).keySet())); | ||
185 | + prefsWriter.apply(); | ||
186 | + | ||
187 | + return true; | ||
188 | + } else | ||
189 | + { | ||
190 | + return false; | ||
191 | + } | ||
192 | + } | ||
193 | + | ||
194 | + @Override | ||
195 | + public List<Cookie> getCookies() | ||
196 | + { | ||
197 | + ArrayList<Cookie> ret = new ArrayList<Cookie>(); | ||
198 | + for (String key : cookies.keySet()) | ||
199 | + ret.addAll(cookies.get(key).values()); | ||
200 | + | ||
201 | + return ret; | ||
202 | + } | ||
203 | + | ||
204 | + | ||
205 | + protected String encodeCookie(SerializableHttpCookie cookie) | ||
206 | + { | ||
207 | + if (cookie == null) | ||
208 | + return null; | ||
209 | + ByteArrayOutputStream os = new ByteArrayOutputStream(); | ||
210 | + try | ||
211 | + { | ||
212 | + ObjectOutputStream outputStream = new ObjectOutputStream(os); | ||
213 | + outputStream.writeObject(cookie); | ||
214 | + } catch (IOException e) | ||
215 | + { | ||
216 | + Log.d(LOG_TAG, "IOException in encodeCookie", e); | ||
217 | + return null; | ||
218 | + } | ||
219 | + | ||
220 | + return byteArrayToHexString(os.toByteArray()); | ||
221 | + } | ||
222 | + | ||
223 | + protected Cookie decodeCookie(String cookieString) | ||
224 | + { | ||
225 | + byte[] bytes = hexStringToByteArray(cookieString); | ||
226 | + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); | ||
227 | + Cookie cookie = null; | ||
228 | + try | ||
229 | + { | ||
230 | + ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream); | ||
231 | + cookie = ((SerializableHttpCookie) objectInputStream.readObject()).getCookie(); | ||
232 | + } catch (IOException e) | ||
233 | + { | ||
234 | + Log.d(LOG_TAG, "IOException in decodeCookie", e); | ||
235 | + } catch (ClassNotFoundException e) | ||
236 | + { | ||
237 | + Log.d(LOG_TAG, "ClassNotFoundException in decodeCookie", e); | ||
238 | + } | ||
239 | + | ||
240 | + return cookie; | ||
241 | + } | ||
242 | + | ||
243 | + /** | ||
244 | + * Using some super basic byte array <-> hex conversions so we don't have to rely on any | ||
245 | + * large Base64 libraries. Can be overridden if you like! | ||
246 | + * | ||
247 | + * @param bytes byte array to be converted | ||
248 | + * @return string containing hex values | ||
249 | + */ | ||
250 | + protected String byteArrayToHexString(byte[] bytes) | ||
251 | + { | ||
252 | + StringBuilder sb = new StringBuilder(bytes.length * 2); | ||
253 | + for (byte element : bytes) | ||
254 | + { | ||
255 | + int v = element & 0xff; | ||
256 | + if (v < 16) | ||
257 | + { | ||
258 | + sb.append('0'); | ||
259 | + } | ||
260 | + sb.append(Integer.toHexString(v)); | ||
261 | + } | ||
262 | + return sb.toString().toUpperCase(Locale.US); | ||
263 | + } | ||
264 | + | ||
265 | + /** | ||
266 | + * Converts hex values from strings to byte arra | ||
267 | + * | ||
268 | + * @param hexString string of hex-encoded values | ||
269 | + * @return decoded byte array | ||
270 | + */ | ||
271 | + protected byte[] hexStringToByteArray(String hexString) | ||
272 | + { | ||
273 | + int len = hexString.length(); | ||
274 | + byte[] data = new byte[len / 2]; | ||
275 | + for (int i = 0; i < len; i += 2) | ||
276 | + { | ||
277 | + data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character.digit(hexString.charAt(i + 1), 16)); | ||
278 | + } | ||
279 | + return data; | ||
280 | + } | ||
281 | +} |
1 | +package com.hhmedic.android.sdk.okhttputils.okhttp.cookie.store; | ||
2 | + | ||
3 | +import java.io.IOException; | ||
4 | +import java.io.ObjectInputStream; | ||
5 | +import java.io.ObjectOutputStream; | ||
6 | +import java.io.Serializable; | ||
7 | + | ||
8 | +import okhttp3.Cookie; | ||
9 | + | ||
10 | +/** | ||
11 | + * from http://stackoverflow.com/questions/25461792/persistent-cookie-store-using-okhttp-2-on-android | ||
12 | + * and<br/> | ||
13 | + * http://www.geebr.com/post/okHttp3%E4%B9%8BCookies%E7%AE%A1%E7%90%86%E5%8F%8A%E6%8C%81%E4%B9%85%E5%8C%96 | ||
14 | + */ | ||
15 | + | ||
16 | +public class SerializableHttpCookie implements Serializable | ||
17 | +{ | ||
18 | + private static final long serialVersionUID = 6374381323722046732L; | ||
19 | + | ||
20 | + private transient final Cookie cookie; | ||
21 | + private transient Cookie clientCookie; | ||
22 | + | ||
23 | + public SerializableHttpCookie(Cookie cookie) | ||
24 | + { | ||
25 | + this.cookie = cookie; | ||
26 | + } | ||
27 | + | ||
28 | + public Cookie getCookie() | ||
29 | + { | ||
30 | + Cookie bestCookie = cookie; | ||
31 | + if (clientCookie != null) | ||
32 | + { | ||
33 | + bestCookie = clientCookie; | ||
34 | + } | ||
35 | + | ||
36 | + return bestCookie; | ||
37 | + } | ||
38 | + | ||
39 | + private void writeObject(ObjectOutputStream out) throws IOException | ||
40 | + { | ||
41 | + out.writeObject(cookie.name()); | ||
42 | + out.writeObject(cookie.value()); | ||
43 | + out.writeLong(cookie.expiresAt()); | ||
44 | + out.writeObject(cookie.domain()); | ||
45 | + out.writeObject(cookie.path()); | ||
46 | + out.writeBoolean(cookie.secure()); | ||
47 | + out.writeBoolean(cookie.httpOnly()); | ||
48 | + out.writeBoolean(cookie.hostOnly()); | ||
49 | + out.writeBoolean(cookie.persistent()); | ||
50 | + } | ||
51 | + | ||
52 | + private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException | ||
53 | + { | ||
54 | + String name = (String) in.readObject(); | ||
55 | + String value = (String) in.readObject(); | ||
56 | + long expiresAt = in.readLong(); | ||
57 | + String domain = (String) in.readObject(); | ||
58 | + String path = (String) in.readObject(); | ||
59 | + boolean secure = in.readBoolean(); | ||
60 | + boolean httpOnly = in.readBoolean(); | ||
61 | + boolean hostOnly = in.readBoolean(); | ||
62 | + boolean persistent = in.readBoolean(); | ||
63 | + Cookie.Builder builder = new Cookie.Builder(); | ||
64 | + builder = builder.name(name); | ||
65 | + builder = builder.value(value); | ||
66 | + builder = builder.expiresAt(expiresAt); | ||
67 | + builder = hostOnly ? builder.hostOnlyDomain(domain) : builder.domain(domain); | ||
68 | + builder = builder.path(path); | ||
69 | + builder = secure ? builder.secure() : builder; | ||
70 | + builder = httpOnly ? builder.httpOnly() : builder; | ||
71 | + clientCookie = builder.build(); | ||
72 | + | ||
73 | + } | ||
74 | +} |
1 | +package com.hhmedic.android.sdk.okhttputils.okhttp.log; | ||
2 | + | ||
3 | +import android.text.TextUtils; | ||
4 | +import android.util.Log; | ||
5 | + | ||
6 | +import java.io.IOException; | ||
7 | + | ||
8 | +import okhttp3.Headers; | ||
9 | +import okhttp3.Interceptor; | ||
10 | +import okhttp3.MediaType; | ||
11 | +import okhttp3.Request; | ||
12 | +import okhttp3.RequestBody; | ||
13 | +import okhttp3.Response; | ||
14 | +import okhttp3.ResponseBody; | ||
15 | +import okio.Buffer; | ||
16 | + | ||
17 | +/** | ||
18 | + * Created by zhy on 16/3/1. | ||
19 | + */ | ||
20 | +public class LoggerInterceptor implements Interceptor | ||
21 | +{ | ||
22 | + public static final String TAG = "OkHttpUtils"; | ||
23 | + private String tag; | ||
24 | + private boolean showResponse; | ||
25 | + | ||
26 | + public LoggerInterceptor(String tag, boolean showResponse) | ||
27 | + { | ||
28 | + if (TextUtils.isEmpty(tag)) | ||
29 | + { | ||
30 | + tag = TAG; | ||
31 | + } | ||
32 | + this.showResponse = showResponse; | ||
33 | + this.tag = tag; | ||
34 | + } | ||
35 | + | ||
36 | + public LoggerInterceptor(String tag) | ||
37 | + { | ||
38 | + this(tag, false); | ||
39 | + } | ||
40 | + | ||
41 | + @Override | ||
42 | + public Response intercept(Chain chain) throws IOException | ||
43 | + { | ||
44 | + Request request = chain.request(); | ||
45 | + logForRequest(request); | ||
46 | + Response response = chain.proceed(request); | ||
47 | + return logForResponse(response); | ||
48 | + } | ||
49 | + | ||
50 | + private Response logForResponse(Response response) | ||
51 | + { | ||
52 | + try | ||
53 | + { | ||
54 | + //===>response log | ||
55 | + Log.e(tag, "========response'log======="); | ||
56 | + Response.Builder builder = response.newBuilder(); | ||
57 | + Response clone = builder.build(); | ||
58 | + Log.e(tag, "url : " + clone.request().url()); | ||
59 | + Log.e(tag, "code : " + clone.code()); | ||
60 | + Log.e(tag, "protocol : " + clone.protocol()); | ||
61 | + if (!TextUtils.isEmpty(clone.message())) | ||
62 | + Log.e(tag, "message : " + clone.message()); | ||
63 | + | ||
64 | + if (showResponse) | ||
65 | + { | ||
66 | + ResponseBody body = clone.body(); | ||
67 | + if (body != null) | ||
68 | + { | ||
69 | + MediaType mediaType = body.contentType(); | ||
70 | + if (mediaType != null) | ||
71 | + { | ||
72 | + Log.e(tag, "responseBody's contentType : " + mediaType.toString()); | ||
73 | + if (isText(mediaType)) | ||
74 | + { | ||
75 | + String resp = body.string(); | ||
76 | + Log.e(tag, "responseBody's content : " + resp); | ||
77 | + | ||
78 | + body = ResponseBody.create(mediaType, resp); | ||
79 | + return response.newBuilder().body(body).build(); | ||
80 | + } else | ||
81 | + { | ||
82 | + Log.e(tag, "responseBody's content : " + " maybe [file part] , too large too print , ignored!"); | ||
83 | + } | ||
84 | + } | ||
85 | + } | ||
86 | + } | ||
87 | + | ||
88 | + Log.e(tag, "========response'log=======end"); | ||
89 | + } catch (Exception e) | ||
90 | + { | ||
91 | +// e.printStackTrace(); | ||
92 | + } | ||
93 | + | ||
94 | + return response; | ||
95 | + } | ||
96 | + | ||
97 | + private void logForRequest(Request request) | ||
98 | + { | ||
99 | + try | ||
100 | + { | ||
101 | + String url = request.url().toString(); | ||
102 | + Headers headers = request.headers(); | ||
103 | + | ||
104 | + Log.e(tag, "========request'log======="); | ||
105 | + Log.e(tag, "method : " + request.method()); | ||
106 | + Log.e(tag, "url : " + url); | ||
107 | + if (headers != null && headers.size() > 0) | ||
108 | + { | ||
109 | + Log.e(tag, "headers : " + headers.toString()); | ||
110 | + } | ||
111 | + RequestBody requestBody = request.body(); | ||
112 | + if (requestBody != null) | ||
113 | + { | ||
114 | + MediaType mediaType = requestBody.contentType(); | ||
115 | + if (mediaType != null) | ||
116 | + { | ||
117 | + Log.e(tag, "requestBody's contentType : " + mediaType.toString()); | ||
118 | + if (isText(mediaType)) | ||
119 | + { | ||
120 | + Log.e(tag, "requestBody's content : " + bodyToString(request)); | ||
121 | + } else | ||
122 | + { | ||
123 | + Log.e(tag, "requestBody's content : " + " maybe [file part] , too large too print , ignored!"); | ||
124 | + } | ||
125 | + } | ||
126 | + } | ||
127 | + Log.e(tag, "========request'log=======end"); | ||
128 | + } catch (Exception e) | ||
129 | + { | ||
130 | +// e.printStackTrace(); | ||
131 | + } | ||
132 | + } | ||
133 | + | ||
134 | + private boolean isText(MediaType mediaType) | ||
135 | + { | ||
136 | + if (mediaType.type() != null && mediaType.type().equals("text")) | ||
137 | + { | ||
138 | + return true; | ||
139 | + } | ||
140 | + if (mediaType.subtype() != null) | ||
141 | + { | ||
142 | + if (mediaType.subtype().equals("json") || | ||
143 | + mediaType.subtype().equals("xml") || | ||
144 | + mediaType.subtype().equals("html") || | ||
145 | + mediaType.subtype().equals("webviewhtml") | ||
146 | + ) | ||
147 | + return true; | ||
148 | + } | ||
149 | + return false; | ||
150 | + } | ||
151 | + | ||
152 | + private String bodyToString(final Request request) | ||
153 | + { | ||
154 | + try | ||
155 | + { | ||
156 | + final Request copy = request.newBuilder().build(); | ||
157 | + final Buffer buffer = new Buffer(); | ||
158 | + copy.body().writeTo(buffer); | ||
159 | + return buffer.readUtf8(); | ||
160 | + } catch (final IOException e) | ||
161 | + { | ||
162 | + return "something error when show requestBody."; | ||
163 | + } | ||
164 | + } | ||
165 | +} |
app/src/main/java/com/hhmedic/android/sdk/okhttputils/okhttp/request/CountingRequestBody.java
0 → 100644
1 | +package com.hhmedic.android.sdk.okhttputils.okhttp.request; | ||
2 | + | ||
3 | +import okhttp3.MediaType; | ||
4 | +import okhttp3.RequestBody; | ||
5 | + | ||
6 | +import java.io.IOException; | ||
7 | + | ||
8 | +import okio.Buffer; | ||
9 | +import okio.BufferedSink; | ||
10 | +import okio.ForwardingSink; | ||
11 | +import okio.Okio; | ||
12 | +import okio.Sink; | ||
13 | + | ||
14 | +/** | ||
15 | + * Decorates an OkHttp request body to count the number of bytes written when writing it. Can | ||
16 | + * decorate any request body, but is most useful for tracking the upload progress of large | ||
17 | + * multipart requests. | ||
18 | + * | ||
19 | + * @author Leo Nikkilä | ||
20 | + */ | ||
21 | +public class CountingRequestBody extends RequestBody | ||
22 | +{ | ||
23 | + | ||
24 | + protected RequestBody delegate; | ||
25 | + protected Listener listener; | ||
26 | + | ||
27 | + protected CountingSink countingSink; | ||
28 | + | ||
29 | + public CountingRequestBody(RequestBody delegate, Listener listener) | ||
30 | + { | ||
31 | + this.delegate = delegate; | ||
32 | + this.listener = listener; | ||
33 | + } | ||
34 | + | ||
35 | + @Override | ||
36 | + public MediaType contentType() | ||
37 | + { | ||
38 | + return delegate.contentType(); | ||
39 | + } | ||
40 | + | ||
41 | + @Override | ||
42 | + public long contentLength() | ||
43 | + { | ||
44 | + try | ||
45 | + { | ||
46 | + return delegate.contentLength(); | ||
47 | + } catch (IOException e) | ||
48 | + { | ||
49 | + e.printStackTrace(); | ||
50 | + } | ||
51 | + return -1; | ||
52 | + } | ||
53 | + | ||
54 | + @Override | ||
55 | + public void writeTo(BufferedSink sink) throws IOException | ||
56 | + { | ||
57 | + | ||
58 | + countingSink = new CountingSink(sink); | ||
59 | + BufferedSink bufferedSink = Okio.buffer(countingSink); | ||
60 | + | ||
61 | + delegate.writeTo(bufferedSink); | ||
62 | + | ||
63 | + bufferedSink.flush(); | ||
64 | + } | ||
65 | + | ||
66 | + protected final class CountingSink extends ForwardingSink | ||
67 | + { | ||
68 | + | ||
69 | + private long bytesWritten = 0; | ||
70 | + | ||
71 | + public CountingSink(Sink delegate) | ||
72 | + { | ||
73 | + super(delegate); | ||
74 | + } | ||
75 | + | ||
76 | + @Override | ||
77 | + public void write(Buffer source, long byteCount) throws IOException | ||
78 | + { | ||
79 | + super.write(source, byteCount); | ||
80 | + | ||
81 | + bytesWritten += byteCount; | ||
82 | + listener.onRequestProgress(bytesWritten, contentLength()); | ||
83 | + } | ||
84 | + | ||
85 | + } | ||
86 | + | ||
87 | + public static interface Listener | ||
88 | + { | ||
89 | + public void onRequestProgress(long bytesWritten, long contentLength); | ||
90 | + } | ||
91 | + | ||
92 | +} |
1 | +package com.hhmedic.android.sdk.okhttputils.okhttp.request; | ||
2 | + | ||
3 | +import java.util.Map; | ||
4 | + | ||
5 | +import okhttp3.Request; | ||
6 | +import okhttp3.RequestBody; | ||
7 | + | ||
8 | +/** | ||
9 | + * Created by zhy on 15/12/14. | ||
10 | + */ | ||
11 | +public class GetRequest extends OkHttpRequest | ||
12 | +{ | ||
13 | + public GetRequest(String url, Object tag, Map<String, String> params, Map<String, String> headers,int id) | ||
14 | + { | ||
15 | + super(url, tag, params, headers,id); | ||
16 | + } | ||
17 | + | ||
18 | + @Override | ||
19 | + protected RequestBody buildRequestBody() | ||
20 | + { | ||
21 | + return null; | ||
22 | + } | ||
23 | + | ||
24 | + @Override | ||
25 | + protected Request buildRequest(RequestBody requestBody) | ||
26 | + { | ||
27 | + return builder.get().build(); | ||
28 | + } | ||
29 | + | ||
30 | + | ||
31 | +} |
1 | +package com.hhmedic.android.sdk.okhttputils.okhttp.request; | ||
2 | + | ||
3 | +import com.hhmedic.android.sdk.okhttputils.okhttp.callback.Callback; | ||
4 | +import com.hhmedic.android.sdk.okhttputils.okhttp.utils.Exceptions; | ||
5 | + | ||
6 | +import java.util.Map; | ||
7 | + | ||
8 | +import okhttp3.Headers; | ||
9 | +import okhttp3.Request; | ||
10 | +import okhttp3.RequestBody; | ||
11 | + | ||
12 | +/** | ||
13 | + * Created by zhy on 15/11/6. | ||
14 | + */ | ||
15 | +public abstract class OkHttpRequest | ||
16 | +{ | ||
17 | + protected String url; | ||
18 | + protected Object tag; | ||
19 | + protected Map<String, String> params; | ||
20 | + protected Map<String, String> headers; | ||
21 | + protected int id; | ||
22 | + | ||
23 | + protected Request.Builder builder = new Request.Builder(); | ||
24 | + | ||
25 | + protected OkHttpRequest(String url, Object tag, | ||
26 | + Map<String, String> params, Map<String, String> headers,int id) | ||
27 | + { | ||
28 | + this.url = url; | ||
29 | + this.tag = tag; | ||
30 | + this.params = params; | ||
31 | + this.headers = headers; | ||
32 | + this.id = id ; | ||
33 | + | ||
34 | + if (url == null) | ||
35 | + { | ||
36 | + Exceptions.illegalArgument("url can not be null."); | ||
37 | + } | ||
38 | + | ||
39 | + initBuilder(); | ||
40 | + } | ||
41 | + | ||
42 | + | ||
43 | + | ||
44 | + /** | ||
45 | + * 初始化一些基本参数 url , tag , headers | ||
46 | + */ | ||
47 | + private void initBuilder() | ||
48 | + { | ||
49 | + builder.url(url).tag(tag); | ||
50 | + appendHeaders(); | ||
51 | + } | ||
52 | + | ||
53 | + protected abstract RequestBody buildRequestBody(); | ||
54 | + | ||
55 | + protected RequestBody wrapRequestBody(RequestBody requestBody, final Callback callback) | ||
56 | + { | ||
57 | + return requestBody; | ||
58 | + } | ||
59 | + | ||
60 | + protected abstract Request buildRequest(RequestBody requestBody); | ||
61 | + | ||
62 | + public RequestCall build() | ||
63 | + { | ||
64 | + return new RequestCall(this); | ||
65 | + } | ||
66 | + | ||
67 | + | ||
68 | + public Request generateRequest(Callback callback) | ||
69 | + { | ||
70 | + RequestBody requestBody = buildRequestBody(); | ||
71 | + RequestBody wrappedRequestBody = wrapRequestBody(requestBody, callback); | ||
72 | + Request request = buildRequest(wrappedRequestBody); | ||
73 | + return request; | ||
74 | + } | ||
75 | + | ||
76 | + | ||
77 | + protected void appendHeaders() | ||
78 | + { | ||
79 | + Headers.Builder headerBuilder = new Headers.Builder(); | ||
80 | + if (headers == null || headers.isEmpty()) return; | ||
81 | + | ||
82 | + for (String key : headers.keySet()) | ||
83 | + { | ||
84 | + headerBuilder.add(key, headers.get(key)); | ||
85 | + } | ||
86 | + builder.headers(headerBuilder.build()); | ||
87 | + } | ||
88 | + | ||
89 | + public int getId() | ||
90 | + { | ||
91 | + return id ; | ||
92 | + } | ||
93 | + | ||
94 | +} |
1 | +package com.hhmedic.android.sdk.okhttputils.okhttp.request; | ||
2 | + | ||
3 | +import android.text.TextUtils; | ||
4 | + | ||
5 | +import com.hhmedic.android.sdk.okhttputils.okhttp.OkHttpUtils; | ||
6 | +import com.hhmedic.android.sdk.okhttputils.okhttp.utils.Exceptions; | ||
7 | + | ||
8 | +import java.util.Map; | ||
9 | + | ||
10 | +import okhttp3.MediaType; | ||
11 | +import okhttp3.Request; | ||
12 | +import okhttp3.RequestBody; | ||
13 | +import okhttp3.internal.http.HttpMethod; | ||
14 | + | ||
15 | +/** | ||
16 | + * Created by zhy on 16/2/23. | ||
17 | + */ | ||
18 | +public class OtherRequest extends OkHttpRequest | ||
19 | +{ | ||
20 | + private static MediaType MEDIA_TYPE_PLAIN = MediaType.parse("text/plain;charset=utf-8"); | ||
21 | + | ||
22 | + private RequestBody requestBody; | ||
23 | + private String method; | ||
24 | + private String content; | ||
25 | + | ||
26 | + public OtherRequest(RequestBody requestBody, String content, String method, String url, Object tag, Map<String, String> params, Map<String, String> headers,int id) | ||
27 | + { | ||
28 | + super(url, tag, params, headers,id); | ||
29 | + this.requestBody = requestBody; | ||
30 | + this.method = method; | ||
31 | + this.content = content; | ||
32 | + | ||
33 | + } | ||
34 | + | ||
35 | + @Override | ||
36 | + protected RequestBody buildRequestBody() | ||
37 | + { | ||
38 | + if (requestBody == null && TextUtils.isEmpty(content) && HttpMethod.requiresRequestBody(method)) | ||
39 | + { | ||
40 | + Exceptions.illegalArgument("requestBody and content can not be null in method:" + method); | ||
41 | + } | ||
42 | + | ||
43 | + if (requestBody == null && !TextUtils.isEmpty(content)) | ||
44 | + { | ||
45 | + requestBody = RequestBody.create(MEDIA_TYPE_PLAIN, content); | ||
46 | + } | ||
47 | + | ||
48 | + return requestBody; | ||
49 | + } | ||
50 | + | ||
51 | + @Override | ||
52 | + protected Request buildRequest(RequestBody requestBody) | ||
53 | + { | ||
54 | + if (method.equals(OkHttpUtils.METHOD.PUT)) | ||
55 | + { | ||
56 | + builder.put(requestBody); | ||
57 | + } else if (method.equals(OkHttpUtils.METHOD.DELETE)) | ||
58 | + { | ||
59 | + if (requestBody == null) | ||
60 | + builder.delete(); | ||
61 | + else | ||
62 | + builder.delete(requestBody); | ||
63 | + } else if (method.equals(OkHttpUtils.METHOD.HEAD)) | ||
64 | + { | ||
65 | + builder.head(); | ||
66 | + } else if (method.equals(OkHttpUtils.METHOD.PATCH)) | ||
67 | + { | ||
68 | + builder.patch(requestBody); | ||
69 | + } | ||
70 | + | ||
71 | + return builder.build(); | ||
72 | + } | ||
73 | + | ||
74 | +} |
app/src/main/java/com/hhmedic/android/sdk/okhttputils/okhttp/request/PostFileRequest.java
0 → 100644
1 | +package com.hhmedic.android.sdk.okhttputils.okhttp.request; | ||
2 | + | ||
3 | +import com.hhmedic.android.sdk.okhttputils.okhttp.OkHttpUtils; | ||
4 | +import com.hhmedic.android.sdk.okhttputils.okhttp.callback.Callback; | ||
5 | +import com.hhmedic.android.sdk.okhttputils.okhttp.utils.Exceptions; | ||
6 | + | ||
7 | +import java.io.File; | ||
8 | +import java.util.Map; | ||
9 | + | ||
10 | +import okhttp3.MediaType; | ||
11 | +import okhttp3.Request; | ||
12 | +import okhttp3.RequestBody; | ||
13 | + | ||
14 | +/** | ||
15 | + * Created by zhy on 15/12/14. | ||
16 | + */ | ||
17 | +public class PostFileRequest extends OkHttpRequest | ||
18 | +{ | ||
19 | + private static MediaType MEDIA_TYPE_STREAM = MediaType.parse("application/octet-stream"); | ||
20 | + | ||
21 | + private File file; | ||
22 | + private MediaType mediaType; | ||
23 | + | ||
24 | + public PostFileRequest(String url, Object tag, Map<String, String> params, Map<String, String> headers, File file, MediaType mediaType,int id) | ||
25 | + { | ||
26 | + super(url, tag, params, headers,id); | ||
27 | + this.file = file; | ||
28 | + this.mediaType = mediaType; | ||
29 | + | ||
30 | + if (this.file == null) | ||
31 | + { | ||
32 | + Exceptions.illegalArgument("the file can not be null !"); | ||
33 | + } | ||
34 | + if (this.mediaType == null) | ||
35 | + { | ||
36 | + this.mediaType = MEDIA_TYPE_STREAM; | ||
37 | + } | ||
38 | + } | ||
39 | + | ||
40 | + @Override | ||
41 | + protected RequestBody buildRequestBody() | ||
42 | + { | ||
43 | + return RequestBody.create(mediaType, file); | ||
44 | + } | ||
45 | + | ||
46 | + @Override | ||
47 | + protected RequestBody wrapRequestBody(RequestBody requestBody, final Callback callback) | ||
48 | + { | ||
49 | + if (callback == null) return requestBody; | ||
50 | + CountingRequestBody countingRequestBody = new CountingRequestBody(requestBody, new CountingRequestBody.Listener() | ||
51 | + { | ||
52 | + @Override | ||
53 | + public void onRequestProgress(final long bytesWritten, final long contentLength) | ||
54 | + { | ||
55 | + | ||
56 | + OkHttpUtils.getInstance().getDelivery().execute(new Runnable() | ||
57 | + { | ||
58 | + @Override | ||
59 | + public void run() | ||
60 | + { | ||
61 | + callback.inProgress(bytesWritten * 1.0f / contentLength,contentLength,id); | ||
62 | + } | ||
63 | + }); | ||
64 | + | ||
65 | + } | ||
66 | + }); | ||
67 | + return countingRequestBody; | ||
68 | + } | ||
69 | + | ||
70 | + @Override | ||
71 | + protected Request buildRequest(RequestBody requestBody) | ||
72 | + { | ||
73 | + return builder.post(requestBody).build(); | ||
74 | + } | ||
75 | + | ||
76 | + | ||
77 | + | ||
78 | +} |
app/src/main/java/com/hhmedic/android/sdk/okhttputils/okhttp/request/PostFormRequest.java
0 → 100644
1 | +package com.hhmedic.android.sdk.okhttputils.okhttp.request; | ||
2 | + | ||
3 | +import com.hhmedic.android.sdk.okhttputils.okhttp.OkHttpUtils; | ||
4 | +import com.hhmedic.android.sdk.okhttputils.okhttp.builder.PostFormBuilder; | ||
5 | +import com.hhmedic.android.sdk.okhttputils.okhttp.callback.Callback; | ||
6 | + | ||
7 | +import java.io.UnsupportedEncodingException; | ||
8 | +import java.net.FileNameMap; | ||
9 | +import java.net.URLConnection; | ||
10 | +import java.net.URLEncoder; | ||
11 | +import java.util.List; | ||
12 | +import java.util.Map; | ||
13 | + | ||
14 | +import okhttp3.FormBody; | ||
15 | +import okhttp3.Headers; | ||
16 | +import okhttp3.MediaType; | ||
17 | +import okhttp3.MultipartBody; | ||
18 | +import okhttp3.Request; | ||
19 | +import okhttp3.RequestBody; | ||
20 | + | ||
21 | +/** | ||
22 | + * Created by zhy on 15/12/14. | ||
23 | + */ | ||
24 | +public class PostFormRequest extends OkHttpRequest | ||
25 | +{ | ||
26 | + private List<PostFormBuilder.FileInput> files; | ||
27 | + | ||
28 | + public PostFormRequest(String url, Object tag, Map<String, String> params, Map<String, String> headers, List<PostFormBuilder.FileInput> files,int id) | ||
29 | + { | ||
30 | + super(url, tag, params, headers,id); | ||
31 | + this.files = files; | ||
32 | + } | ||
33 | + | ||
34 | + @Override | ||
35 | + protected RequestBody buildRequestBody() | ||
36 | + { | ||
37 | + if (files == null || files.isEmpty()) | ||
38 | + { | ||
39 | + FormBody.Builder builder = new FormBody.Builder(); | ||
40 | + addParams(builder); | ||
41 | + FormBody formBody = builder.build(); | ||
42 | + return formBody; | ||
43 | + } else | ||
44 | + { | ||
45 | + MultipartBody.Builder builder = new MultipartBody.Builder() | ||
46 | + .setType(MultipartBody.FORM); | ||
47 | + addParams(builder); | ||
48 | + | ||
49 | + for (int i = 0; i < files.size(); i++) | ||
50 | + { | ||
51 | + PostFormBuilder.FileInput fileInput = files.get(i); | ||
52 | + RequestBody fileBody = RequestBody.create(MediaType.parse(guessMimeType(fileInput.filename)), fileInput.file); | ||
53 | + builder.addFormDataPart(fileInput.key, fileInput.filename, fileBody); | ||
54 | + } | ||
55 | + return builder.build(); | ||
56 | + } | ||
57 | + } | ||
58 | + | ||
59 | + @Override | ||
60 | + protected RequestBody wrapRequestBody(RequestBody requestBody, final Callback callback) | ||
61 | + { | ||
62 | + if (callback == null) return requestBody; | ||
63 | + CountingRequestBody countingRequestBody = new CountingRequestBody(requestBody, new CountingRequestBody.Listener() | ||
64 | + { | ||
65 | + @Override | ||
66 | + public void onRequestProgress(final long bytesWritten, final long contentLength) | ||
67 | + { | ||
68 | + | ||
69 | + OkHttpUtils.getInstance().getDelivery().execute(new Runnable() | ||
70 | + { | ||
71 | + @Override | ||
72 | + public void run() | ||
73 | + { | ||
74 | + callback.inProgress(bytesWritten * 1.0f / contentLength,contentLength,id); | ||
75 | + } | ||
76 | + }); | ||
77 | + | ||
78 | + } | ||
79 | + }); | ||
80 | + return countingRequestBody; | ||
81 | + } | ||
82 | + | ||
83 | + @Override | ||
84 | + protected Request buildRequest(RequestBody requestBody) | ||
85 | + { | ||
86 | + return builder.post(requestBody).build(); | ||
87 | + } | ||
88 | + | ||
89 | + private String guessMimeType(String path) | ||
90 | + { | ||
91 | + FileNameMap fileNameMap = URLConnection.getFileNameMap(); | ||
92 | + String contentTypeFor = null; | ||
93 | + try | ||
94 | + { | ||
95 | + contentTypeFor = fileNameMap.getContentTypeFor(URLEncoder.encode(path, "UTF-8")); | ||
96 | + } catch (UnsupportedEncodingException e) | ||
97 | + { | ||
98 | + e.printStackTrace(); | ||
99 | + } | ||
100 | + if (contentTypeFor == null) | ||
101 | + { | ||
102 | + contentTypeFor = "application/octet-stream"; | ||
103 | + } | ||
104 | + return contentTypeFor; | ||
105 | + } | ||
106 | + | ||
107 | + private void addParams(MultipartBody.Builder builder) | ||
108 | + { | ||
109 | + if (params != null && !params.isEmpty()) | ||
110 | + { | ||
111 | + for (String key : params.keySet()) | ||
112 | + { | ||
113 | + builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"" + key + "\""), | ||
114 | + RequestBody.create(null, params.get(key))); | ||
115 | + } | ||
116 | + } | ||
117 | + } | ||
118 | + | ||
119 | + private void addParams(FormBody.Builder builder) | ||
120 | + { | ||
121 | + if (params != null) | ||
122 | + { | ||
123 | + for (String key : params.keySet()) | ||
124 | + { | ||
125 | + builder.add(key, params.get(key)); | ||
126 | + } | ||
127 | + } | ||
128 | + } | ||
129 | + | ||
130 | +} |
app/src/main/java/com/hhmedic/android/sdk/okhttputils/okhttp/request/PostStringRequest.java
0 → 100644
1 | +package com.hhmedic.android.sdk.okhttputils.okhttp.request; | ||
2 | + | ||
3 | +import com.hhmedic.android.sdk.okhttputils.okhttp.utils.Exceptions; | ||
4 | + | ||
5 | +import java.util.Map; | ||
6 | + | ||
7 | +import okhttp3.MediaType; | ||
8 | +import okhttp3.Request; | ||
9 | +import okhttp3.RequestBody; | ||
10 | + | ||
11 | +/** | ||
12 | + * Created by zhy on 15/12/14. | ||
13 | + */ | ||
14 | +public class PostStringRequest extends OkHttpRequest | ||
15 | +{ | ||
16 | + private static MediaType MEDIA_TYPE_PLAIN = MediaType.parse("text/plain;charset=utf-8"); | ||
17 | + | ||
18 | + private String content; | ||
19 | + private MediaType mediaType; | ||
20 | + | ||
21 | + | ||
22 | + public PostStringRequest(String url, Object tag, Map<String, String> params, Map<String, String> headers, String content, MediaType mediaType,int id) | ||
23 | + { | ||
24 | + super(url, tag, params, headers,id); | ||
25 | + this.content = content; | ||
26 | + this.mediaType = mediaType; | ||
27 | + | ||
28 | + if (this.content == null) | ||
29 | + { | ||
30 | + Exceptions.illegalArgument("the content can not be null !"); | ||
31 | + } | ||
32 | + if (this.mediaType == null) | ||
33 | + { | ||
34 | + this.mediaType = MEDIA_TYPE_PLAIN; | ||
35 | + } | ||
36 | + | ||
37 | + } | ||
38 | + | ||
39 | + @Override | ||
40 | + protected RequestBody buildRequestBody() | ||
41 | + { | ||
42 | + return RequestBody.create(mediaType, content); | ||
43 | + } | ||
44 | + | ||
45 | + @Override | ||
46 | + protected Request buildRequest( RequestBody requestBody) | ||
47 | + { | ||
48 | + return builder.post(requestBody).build(); | ||
49 | + } | ||
50 | + | ||
51 | + | ||
52 | +} |
1 | +package com.hhmedic.android.sdk.okhttputils.okhttp.request; | ||
2 | + | ||
3 | +import com.hhmedic.android.sdk.okhttputils.okhttp.OkHttpUtils; | ||
4 | +import com.hhmedic.android.sdk.okhttputils.okhttp.callback.Callback; | ||
5 | + | ||
6 | +import java.io.IOException; | ||
7 | +import java.util.concurrent.TimeUnit; | ||
8 | + | ||
9 | +import okhttp3.Call; | ||
10 | +import okhttp3.OkHttpClient; | ||
11 | +import okhttp3.Request; | ||
12 | +import okhttp3.Response; | ||
13 | + | ||
14 | +/** | ||
15 | + * Created by zhy on 15/12/15. | ||
16 | + * 对OkHttpRequest的封装,对外提供更多的接口:cancel(),readTimeOut()... | ||
17 | + */ | ||
18 | +public class RequestCall | ||
19 | +{ | ||
20 | + private OkHttpRequest okHttpRequest; | ||
21 | + private Request request; | ||
22 | + private Call call; | ||
23 | + | ||
24 | + private long readTimeOut; | ||
25 | + private long writeTimeOut; | ||
26 | + private long connTimeOut; | ||
27 | + | ||
28 | + private OkHttpClient clone; | ||
29 | + | ||
30 | + public RequestCall(OkHttpRequest request) | ||
31 | + { | ||
32 | + this.okHttpRequest = request; | ||
33 | + } | ||
34 | + | ||
35 | + public RequestCall readTimeOut(long readTimeOut) | ||
36 | + { | ||
37 | + this.readTimeOut = readTimeOut; | ||
38 | + return this; | ||
39 | + } | ||
40 | + | ||
41 | + public RequestCall writeTimeOut(long writeTimeOut) | ||
42 | + { | ||
43 | + this.writeTimeOut = writeTimeOut; | ||
44 | + return this; | ||
45 | + } | ||
46 | + | ||
47 | + public RequestCall connTimeOut(long connTimeOut) | ||
48 | + { | ||
49 | + this.connTimeOut = connTimeOut; | ||
50 | + return this; | ||
51 | + } | ||
52 | + | ||
53 | + public Call buildCall(Callback callback) | ||
54 | + { | ||
55 | + request = generateRequest(callback); | ||
56 | + | ||
57 | + if (readTimeOut > 0 || writeTimeOut > 0 || connTimeOut > 0) | ||
58 | + { | ||
59 | + readTimeOut = readTimeOut > 0 ? readTimeOut : OkHttpUtils.DEFAULT_MILLISECONDS; | ||
60 | + writeTimeOut = writeTimeOut > 0 ? writeTimeOut : OkHttpUtils.DEFAULT_MILLISECONDS; | ||
61 | + connTimeOut = connTimeOut > 0 ? connTimeOut : OkHttpUtils.DEFAULT_MILLISECONDS; | ||
62 | + | ||
63 | + clone = OkHttpUtils.getInstance().getOkHttpClient().newBuilder() | ||
64 | + .readTimeout(readTimeOut, TimeUnit.MILLISECONDS) | ||
65 | + .writeTimeout(writeTimeOut, TimeUnit.MILLISECONDS) | ||
66 | + .connectTimeout(connTimeOut, TimeUnit.MILLISECONDS) | ||
67 | + .build(); | ||
68 | + | ||
69 | + call = clone.newCall(request); | ||
70 | + } else | ||
71 | + { | ||
72 | + call = OkHttpUtils.getInstance().getOkHttpClient().newCall(request); | ||
73 | + } | ||
74 | + return call; | ||
75 | + } | ||
76 | + | ||
77 | + private Request generateRequest(Callback callback) | ||
78 | + { | ||
79 | + return okHttpRequest.generateRequest(callback); | ||
80 | + } | ||
81 | + | ||
82 | + public void execute(Callback callback) | ||
83 | + { | ||
84 | + buildCall(callback); | ||
85 | + | ||
86 | + if (callback != null) | ||
87 | + { | ||
88 | + callback.onBefore(request, getOkHttpRequest().getId()); | ||
89 | + } | ||
90 | + | ||
91 | + OkHttpUtils.getInstance().execute(this, callback); | ||
92 | + } | ||
93 | + | ||
94 | + public Call getCall() | ||
95 | + { | ||
96 | + return call; | ||
97 | + } | ||
98 | + | ||
99 | + public Request getRequest() | ||
100 | + { | ||
101 | + return request; | ||
102 | + } | ||
103 | + | ||
104 | + public OkHttpRequest getOkHttpRequest() | ||
105 | + { | ||
106 | + return okHttpRequest; | ||
107 | + } | ||
108 | + | ||
109 | + public Response execute() throws IOException | ||
110 | + { | ||
111 | + buildCall(null); | ||
112 | + return call.execute(); | ||
113 | + } | ||
114 | + | ||
115 | + public void cancel() | ||
116 | + { | ||
117 | + if (call != null) | ||
118 | + { | ||
119 | + call.cancel(); | ||
120 | + } | ||
121 | + } | ||
122 | + | ||
123 | + | ||
124 | +} |
1 | +package com.hhmedic.android.sdk.okhttputils.okhttp.utils; | ||
2 | + | ||
3 | +/** | ||
4 | + * Created by zhy on 15/12/14. | ||
5 | + */ | ||
6 | +public class Exceptions | ||
7 | +{ | ||
8 | + public static void illegalArgument(String msg, Object... params) | ||
9 | + { | ||
10 | + throw new IllegalArgumentException(String.format(msg, params)); | ||
11 | + } | ||
12 | + | ||
13 | + | ||
14 | +} |
1 | +package com.hhmedic.android.sdk.okhttputils.okhttp.utils; | ||
2 | + | ||
3 | +import android.graphics.BitmapFactory; | ||
4 | +import android.util.DisplayMetrics; | ||
5 | +import android.view.View; | ||
6 | +import android.view.ViewGroup; | ||
7 | +import android.widget.ImageView; | ||
8 | + | ||
9 | +import java.io.InputStream; | ||
10 | +import java.lang.reflect.Field; | ||
11 | + | ||
12 | +/** | ||
13 | + * Created by zhy on 15/11/6. | ||
14 | + */ | ||
15 | +public class ImageUtils | ||
16 | +{ | ||
17 | + /** | ||
18 | + * 根据InputStream获取图片实际的宽度和高度 | ||
19 | + * | ||
20 | + * @param imageStream | ||
21 | + * @return | ||
22 | + */ | ||
23 | + public static ImageSize getImageSize(InputStream imageStream) | ||
24 | + { | ||
25 | + BitmapFactory.Options options = new BitmapFactory.Options(); | ||
26 | + options.inJustDecodeBounds = true; | ||
27 | + BitmapFactory.decodeStream(imageStream, null, options); | ||
28 | + return new ImageSize(options.outWidth, options.outHeight); | ||
29 | + } | ||
30 | + | ||
31 | + public static class ImageSize | ||
32 | + { | ||
33 | + int width; | ||
34 | + int height; | ||
35 | + | ||
36 | + public ImageSize() | ||
37 | + { | ||
38 | + } | ||
39 | + | ||
40 | + public ImageSize(int width, int height) | ||
41 | + { | ||
42 | + this.width = width; | ||
43 | + this.height = height; | ||
44 | + } | ||
45 | + | ||
46 | + @Override | ||
47 | + public String toString() | ||
48 | + { | ||
49 | + return "ImageSize{" + | ||
50 | + "width=" + width + | ||
51 | + ", height=" + height + | ||
52 | + '}'; | ||
53 | + } | ||
54 | + } | ||
55 | + | ||
56 | + public static int calculateInSampleSize(ImageSize srcSize, ImageSize targetSize) | ||
57 | + { | ||
58 | + // 源图片的宽度 | ||
59 | + int width = srcSize.width; | ||
60 | + int height = srcSize.height; | ||
61 | + int inSampleSize = 1; | ||
62 | + | ||
63 | + int reqWidth = targetSize.width; | ||
64 | + int reqHeight = targetSize.height; | ||
65 | + | ||
66 | + if (width > reqWidth && height > reqHeight) | ||
67 | + { | ||
68 | + // 计算出实际宽度和目标宽度的比率 | ||
69 | + int widthRatio = Math.round((float) width / (float) reqWidth); | ||
70 | + int heightRatio = Math.round((float) height / (float) reqHeight); | ||
71 | + inSampleSize = Math.max(widthRatio, heightRatio); | ||
72 | + } | ||
73 | + return inSampleSize; | ||
74 | + } | ||
75 | + | ||
76 | + /** | ||
77 | + * 根据ImageView获适当的压缩的宽和高 | ||
78 | + * | ||
79 | + * @param view | ||
80 | + * @return | ||
81 | + */ | ||
82 | + public static ImageSize getImageViewSize(View view) | ||
83 | + { | ||
84 | + | ||
85 | + ImageSize imageSize = new ImageSize(); | ||
86 | + | ||
87 | + imageSize.width = getExpectWidth(view); | ||
88 | + imageSize.height = getExpectHeight(view); | ||
89 | + | ||
90 | + return imageSize; | ||
91 | + } | ||
92 | + | ||
93 | + /** | ||
94 | + * 根据view获得期望的高度 | ||
95 | + * | ||
96 | + * @param view | ||
97 | + * @return | ||
98 | + */ | ||
99 | + private static int getExpectHeight(View view) | ||
100 | + { | ||
101 | + | ||
102 | + int height = 0; | ||
103 | + if (view == null) return 0; | ||
104 | + | ||
105 | + final ViewGroup.LayoutParams params = view.getLayoutParams(); | ||
106 | + //如果是WRAP_CONTENT,此时图片还没加载,getWidth根本无效 | ||
107 | + if (params != null && params.height != ViewGroup.LayoutParams.WRAP_CONTENT) | ||
108 | + { | ||
109 | + height = view.getWidth(); // 获得实际的宽度 | ||
110 | + } | ||
111 | + if (height <= 0 && params != null) | ||
112 | + { | ||
113 | + height = params.height; // 获得布局文件中的声明的宽度 | ||
114 | + } | ||
115 | + | ||
116 | + if (height <= 0) | ||
117 | + { | ||
118 | + height = getImageViewFieldValue(view, "mMaxHeight");// 获得设置的最大的宽度 | ||
119 | + } | ||
120 | + | ||
121 | + //如果宽度还是没有获取到,憋大招,使用屏幕的宽度 | ||
122 | + if (height <= 0) | ||
123 | + { | ||
124 | + DisplayMetrics displayMetrics = view.getContext().getResources() | ||
125 | + .getDisplayMetrics(); | ||
126 | + height = displayMetrics.heightPixels; | ||
127 | + } | ||
128 | + | ||
129 | + return height; | ||
130 | + } | ||
131 | + | ||
132 | + /** | ||
133 | + * 根据view获得期望的宽度 | ||
134 | + * | ||
135 | + * @param view | ||
136 | + * @return | ||
137 | + */ | ||
138 | + private static int getExpectWidth(View view) | ||
139 | + { | ||
140 | + int width = 0; | ||
141 | + if (view == null) return 0; | ||
142 | + | ||
143 | + final ViewGroup.LayoutParams params = view.getLayoutParams(); | ||
144 | + //如果是WRAP_CONTENT,此时图片还没加载,getWidth根本无效 | ||
145 | + if (params != null && params.width != ViewGroup.LayoutParams.WRAP_CONTENT) | ||
146 | + { | ||
147 | + width = view.getWidth(); // 获得实际的宽度 | ||
148 | + } | ||
149 | + if (width <= 0 && params != null) | ||
150 | + { | ||
151 | + width = params.width; // 获得布局文件中的声明的宽度 | ||
152 | + } | ||
153 | + | ||
154 | + if (width <= 0) | ||
155 | + | ||
156 | + { | ||
157 | + width = getImageViewFieldValue(view, "mMaxWidth");// 获得设置的最大的宽度 | ||
158 | + } | ||
159 | + //如果宽度还是没有获取到,憋大招,使用屏幕的宽度 | ||
160 | + if (width <= 0) | ||
161 | + | ||
162 | + { | ||
163 | + DisplayMetrics displayMetrics = view.getContext().getResources() | ||
164 | + .getDisplayMetrics(); | ||
165 | + width = displayMetrics.widthPixels; | ||
166 | + } | ||
167 | + | ||
168 | + return width; | ||
169 | + } | ||
170 | + | ||
171 | + /** | ||
172 | + * 通过反射获取imageview的某个属性值 | ||
173 | + * | ||
174 | + * @param object | ||
175 | + * @param fieldName | ||
176 | + * @return | ||
177 | + */ | ||
178 | + private static int getImageViewFieldValue(Object object, String fieldName) | ||
179 | + { | ||
180 | + int value = 0; | ||
181 | + try | ||
182 | + { | ||
183 | + Field field = ImageView.class.getDeclaredField(fieldName); | ||
184 | + field.setAccessible(true); | ||
185 | + int fieldValue = field.getInt(object); | ||
186 | + if (fieldValue > 0 && fieldValue < Integer.MAX_VALUE) | ||
187 | + { | ||
188 | + value = fieldValue; | ||
189 | + } | ||
190 | + } catch (Exception e) | ||
191 | + { | ||
192 | + } | ||
193 | + return value; | ||
194 | + | ||
195 | + } | ||
196 | +} |
1 | +package com.hhmedic.android.sdk.okhttputils.okhttp.utils; | ||
2 | + | ||
3 | +import android.util.Log; | ||
4 | + | ||
5 | +/** | ||
6 | + * Created by zhy on 15/11/6. | ||
7 | + */ | ||
8 | +public class L | ||
9 | +{ | ||
10 | + private static boolean debug = false; | ||
11 | + | ||
12 | + public static void e(String msg) | ||
13 | + { | ||
14 | + if (debug) | ||
15 | + { | ||
16 | + Log.e("OkHttp", msg); | ||
17 | + } | ||
18 | + } | ||
19 | + | ||
20 | +} | ||
21 | + |
1 | +/* | ||
2 | + * Copyright (C) 2013 Square, Inc. | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | +package com.hhmedic.android.sdk.okhttputils.okhttp.utils; | ||
17 | + | ||
18 | +import android.os.Build; | ||
19 | +import android.os.Handler; | ||
20 | +import android.os.Looper; | ||
21 | + | ||
22 | +import java.util.concurrent.Executor; | ||
23 | +import java.util.concurrent.Executors; | ||
24 | + | ||
25 | +public class Platform | ||
26 | +{ | ||
27 | + private static final Platform PLATFORM = findPlatform(); | ||
28 | + | ||
29 | + public static Platform get() | ||
30 | + { | ||
31 | + L.e(PLATFORM.getClass().toString()); | ||
32 | + return PLATFORM; | ||
33 | + } | ||
34 | + | ||
35 | + private static Platform findPlatform() | ||
36 | + { | ||
37 | + try | ||
38 | + { | ||
39 | + Class.forName("android.os.Build"); | ||
40 | + if (Build.VERSION.SDK_INT != 0) | ||
41 | + { | ||
42 | + return new Android(); | ||
43 | + } | ||
44 | + } catch (ClassNotFoundException ignored) | ||
45 | + { | ||
46 | + } | ||
47 | + return new Platform(); | ||
48 | + } | ||
49 | + | ||
50 | + public Executor defaultCallbackExecutor() | ||
51 | + { | ||
52 | + return Executors.newCachedThreadPool(); | ||
53 | + } | ||
54 | + | ||
55 | + public void execute(Runnable runnable) | ||
56 | + { | ||
57 | + defaultCallbackExecutor().execute(runnable); | ||
58 | + } | ||
59 | + | ||
60 | + | ||
61 | + static class Android extends Platform | ||
62 | + { | ||
63 | + @Override | ||
64 | + public Executor defaultCallbackExecutor() | ||
65 | + { | ||
66 | + return new MainThreadExecutor(); | ||
67 | + } | ||
68 | + | ||
69 | + static class MainThreadExecutor implements Executor | ||
70 | + { | ||
71 | + private final Handler handler = new Handler(Looper.getMainLooper()); | ||
72 | + | ||
73 | + @Override | ||
74 | + public void execute(Runnable r) | ||
75 | + { | ||
76 | + handler.post(r); | ||
77 | + } | ||
78 | + } | ||
79 | + } | ||
80 | + | ||
81 | + | ||
82 | +} |
app/src/main/res/._.DS_Store
0 → 100644
No preview for this file type
build.gradle
0 → 100644
1 | +// Top-level build file where you can add configuration options common to all sub-projects/modules. | ||
2 | + | ||
3 | +buildscript { | ||
4 | + | ||
5 | + repositories { | ||
6 | + google() | ||
7 | + jcenter() | ||
8 | + | ||
9 | + } | ||
10 | + dependencies { | ||
11 | + classpath 'com.android.tools.build:gradle:3.6.2' | ||
12 | + | ||
13 | + | ||
14 | + // NOTE: Do not place your application dependencies here; they belong | ||
15 | + // in the individual module build.gradle files | ||
16 | + } | ||
17 | +} | ||
18 | + | ||
19 | +allprojects { | ||
20 | + repositories { | ||
21 | + google() | ||
22 | + jcenter() | ||
23 | + | ||
24 | + } | ||
25 | +} | ||
26 | + | ||
27 | +task clean(type: Delete) { | ||
28 | + delete rootProject.buildDir | ||
29 | +} |
gradle.properties
0 → 100644
1 | +# Project-wide Gradle settings. | ||
2 | +# IDE (e.g. Android Studio) users: | ||
3 | +# Gradle settings configured through the IDE *will override* | ||
4 | +# any settings specified in this file. | ||
5 | +# For more details on how to configure your build environment visit | ||
6 | +# http://www.gradle.org/docs/current/userguide/build_environment.html | ||
7 | +# Specifies the JVM arguments used for the daemon process. | ||
8 | +# The setting is particularly useful for tweaking memory settings. | ||
9 | +org.gradle.jvmargs=-Xmx1536m | ||
10 | +# When configured, Gradle will run in incubating parallel mode. | ||
11 | +# This option should only be used with decoupled projects. More details, visit | ||
12 | +# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects | ||
13 | +# org.gradle.parallel=true | ||
14 | + | ||
15 | + |
gradle/wrapper/gradle-wrapper.jar
0 → 100644
No preview for this file type
gradle/wrapper/gradle-wrapper.properties
0 → 100644
gradlew
0 → 100644
1 | +#!/usr/bin/env sh | ||
2 | + | ||
3 | +############################################################################## | ||
4 | +## | ||
5 | +## Gradle start up script for UN*X | ||
6 | +## | ||
7 | +############################################################################## | ||
8 | + | ||
9 | +# Attempt to set APP_HOME | ||
10 | +# Resolve links: $0 may be a link | ||
11 | +PRG="$0" | ||
12 | +# Need this for relative symlinks. | ||
13 | +while [ -h "$PRG" ] ; do | ||
14 | + ls=`ls -ld "$PRG"` | ||
15 | + link=`expr "$ls" : '.*-> \(.*\)$'` | ||
16 | + if expr "$link" : '/.*' > /dev/null; then | ||
17 | + PRG="$link" | ||
18 | + else | ||
19 | + PRG=`dirname "$PRG"`"/$link" | ||
20 | + fi | ||
21 | +done | ||
22 | +SAVED="`pwd`" | ||
23 | +cd "`dirname \"$PRG\"`/" >/dev/null | ||
24 | +APP_HOME="`pwd -P`" | ||
25 | +cd "$SAVED" >/dev/null | ||
26 | + | ||
27 | +APP_NAME="Gradle" | ||
28 | +APP_BASE_NAME=`basename "$0"` | ||
29 | + | ||
30 | +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. | ||
31 | +DEFAULT_JVM_OPTS="" | ||
32 | + | ||
33 | +# Use the maximum available, or set MAX_FD != -1 to use that value. | ||
34 | +MAX_FD="maximum" | ||
35 | + | ||
36 | +warn () { | ||
37 | + echo "$*" | ||
38 | +} | ||
39 | + | ||
40 | +die () { | ||
41 | + echo | ||
42 | + echo "$*" | ||
43 | + echo | ||
44 | + exit 1 | ||
45 | +} | ||
46 | + | ||
47 | +# OS specific support (must be 'true' or 'false'). | ||
48 | +cygwin=false | ||
49 | +msys=false | ||
50 | +darwin=false | ||
51 | +nonstop=false | ||
52 | +case "`uname`" in | ||
53 | + CYGWIN* ) | ||
54 | + cygwin=true | ||
55 | + ;; | ||
56 | + Darwin* ) | ||
57 | + darwin=true | ||
58 | + ;; | ||
59 | + MINGW* ) | ||
60 | + msys=true | ||
61 | + ;; | ||
62 | + NONSTOP* ) | ||
63 | + nonstop=true | ||
64 | + ;; | ||
65 | +esac | ||
66 | + | ||
67 | +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar | ||
68 | + | ||
69 | +# Determine the Java command to use to start the JVM. | ||
70 | +if [ -n "$JAVA_HOME" ] ; then | ||
71 | + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then | ||
72 | + # IBM's JDK on AIX uses strange locations for the executables | ||
73 | + JAVACMD="$JAVA_HOME/jre/sh/java" | ||
74 | + else | ||
75 | + JAVACMD="$JAVA_HOME/bin/java" | ||
76 | + fi | ||
77 | + if [ ! -x "$JAVACMD" ] ; then | ||
78 | + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME | ||
79 | + | ||
80 | +Please set the JAVA_HOME variable in your environment to match the | ||
81 | +location of your Java installation." | ||
82 | + fi | ||
83 | +else | ||
84 | + JAVACMD="java" | ||
85 | + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. | ||
86 | + | ||
87 | +Please set the JAVA_HOME variable in your environment to match the | ||
88 | +location of your Java installation." | ||
89 | +fi | ||
90 | + | ||
91 | +# Increase the maximum file descriptors if we can. | ||
92 | +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then | ||
93 | + MAX_FD_LIMIT=`ulimit -H -n` | ||
94 | + if [ $? -eq 0 ] ; then | ||
95 | + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then | ||
96 | + MAX_FD="$MAX_FD_LIMIT" | ||
97 | + fi | ||
98 | + ulimit -n $MAX_FD | ||
99 | + if [ $? -ne 0 ] ; then | ||
100 | + warn "Could not set maximum file descriptor limit: $MAX_FD" | ||
101 | + fi | ||
102 | + else | ||
103 | + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" | ||
104 | + fi | ||
105 | +fi | ||
106 | + | ||
107 | +# For Darwin, add options to specify how the application appears in the dock | ||
108 | +if $darwin; then | ||
109 | + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" | ||
110 | +fi | ||
111 | + | ||
112 | +# For Cygwin, switch paths to Windows format before running java | ||
113 | +if $cygwin ; then | ||
114 | + APP_HOME=`cygpath --path --mixed "$APP_HOME"` | ||
115 | + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` | ||
116 | + JAVACMD=`cygpath --unix "$JAVACMD"` | ||
117 | + | ||
118 | + # We build the pattern for arguments to be converted via cygpath | ||
119 | + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` | ||
120 | + SEP="" | ||
121 | + for dir in $ROOTDIRSRAW ; do | ||
122 | + ROOTDIRS="$ROOTDIRS$SEP$dir" | ||
123 | + SEP="|" | ||
124 | + done | ||
125 | + OURCYGPATTERN="(^($ROOTDIRS))" | ||
126 | + # Add a user-defined pattern to the cygpath arguments | ||
127 | + if [ "$GRADLE_CYGPATTERN" != "" ] ; then | ||
128 | + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" | ||
129 | + fi | ||
130 | + # Now convert the arguments - kludge to limit ourselves to /bin/sh | ||
131 | + i=0 | ||
132 | + for arg in "$@" ; do | ||
133 | + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` | ||
134 | + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option | ||
135 | + | ||
136 | + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition | ||
137 | + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` | ||
138 | + else | ||
139 | + eval `echo args$i`="\"$arg\"" | ||
140 | + fi | ||
141 | + i=$((i+1)) | ||
142 | + done | ||
143 | + case $i in | ||
144 | + (0) set -- ;; | ||
145 | + (1) set -- "$args0" ;; | ||
146 | + (2) set -- "$args0" "$args1" ;; | ||
147 | + (3) set -- "$args0" "$args1" "$args2" ;; | ||
148 | + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; | ||
149 | + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; | ||
150 | + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; | ||
151 | + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; | ||
152 | + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; | ||
153 | + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; | ||
154 | + esac | ||
155 | +fi | ||
156 | + | ||
157 | +# Escape application args | ||
158 | +save () { | ||
159 | + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done | ||
160 | + echo " " | ||
161 | +} | ||
162 | +APP_ARGS=$(save "$@") | ||
163 | + | ||
164 | +# Collect all arguments for the java command, following the shell quoting and substitution rules | ||
165 | +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" | ||
166 | + | ||
167 | +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong | ||
168 | +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then | ||
169 | + cd "$(dirname "$0")" | ||
170 | +fi | ||
171 | + | ||
172 | +exec "$JAVACMD" "$@" |
gradlew.bat
0 → 100644
1 | +@if "%DEBUG%" == "" @echo off | ||
2 | +@rem ########################################################################## | ||
3 | +@rem | ||
4 | +@rem Gradle startup script for Windows | ||
5 | +@rem | ||
6 | +@rem ########################################################################## | ||
7 | + | ||
8 | +@rem Set local scope for the variables with windows NT shell | ||
9 | +if "%OS%"=="Windows_NT" setlocal | ||
10 | + | ||
11 | +set DIRNAME=%~dp0 | ||
12 | +if "%DIRNAME%" == "" set DIRNAME=. | ||
13 | +set APP_BASE_NAME=%~n0 | ||
14 | +set APP_HOME=%DIRNAME% | ||
15 | + | ||
16 | +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. | ||
17 | +set DEFAULT_JVM_OPTS= | ||
18 | + | ||
19 | +@rem Find java.exe | ||
20 | +if defined JAVA_HOME goto findJavaFromJavaHome | ||
21 | + | ||
22 | +set JAVA_EXE=java.exe | ||
23 | +%JAVA_EXE% -version >NUL 2>&1 | ||
24 | +if "%ERRORLEVEL%" == "0" goto init | ||
25 | + | ||
26 | +echo. | ||
27 | +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. | ||
28 | +echo. | ||
29 | +echo Please set the JAVA_HOME variable in your environment to match the | ||
30 | +echo location of your Java installation. | ||
31 | + | ||
32 | +goto fail | ||
33 | + | ||
34 | +:findJavaFromJavaHome | ||
35 | +set JAVA_HOME=%JAVA_HOME:"=% | ||
36 | +set JAVA_EXE=%JAVA_HOME%/bin/java.exe | ||
37 | + | ||
38 | +if exist "%JAVA_EXE%" goto init | ||
39 | + | ||
40 | +echo. | ||
41 | +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% | ||
42 | +echo. | ||
43 | +echo Please set the JAVA_HOME variable in your environment to match the | ||
44 | +echo location of your Java installation. | ||
45 | + | ||
46 | +goto fail | ||
47 | + | ||
48 | +:init | ||
49 | +@rem Get command-line arguments, handling Windows variants | ||
50 | + | ||
51 | +if not "%OS%" == "Windows_NT" goto win9xME_args | ||
52 | + | ||
53 | +:win9xME_args | ||
54 | +@rem Slurp the command line arguments. | ||
55 | +set CMD_LINE_ARGS= | ||
56 | +set _SKIP=2 | ||
57 | + | ||
58 | +:win9xME_args_slurp | ||
59 | +if "x%~1" == "x" goto execute | ||
60 | + | ||
61 | +set CMD_LINE_ARGS=%* | ||
62 | + | ||
63 | +:execute | ||
64 | +@rem Setup the command line | ||
65 | + | ||
66 | +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar | ||
67 | + | ||
68 | +@rem Execute Gradle | ||
69 | +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% | ||
70 | + | ||
71 | +:end | ||
72 | +@rem End local scope for the variables with windows NT shell | ||
73 | +if "%ERRORLEVEL%"=="0" goto mainEnd | ||
74 | + | ||
75 | +:fail | ||
76 | +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of | ||
77 | +rem the _cmd.exe /c_ return code! | ||
78 | +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 | ||
79 | +exit /b 1 | ||
80 | + | ||
81 | +:mainEnd | ||
82 | +if "%OS%"=="Windows_NT" endlocal | ||
83 | + | ||
84 | +:omega |
settings.gradle
0 → 100644
-
Please register or login to post a comment