Skip to content

Commit d326851

Browse files
committed
OkHttp使用及源码解析
1 parent 17ef75e commit d326851

File tree

28 files changed

+689
-2
lines changed

28 files changed

+689
-2
lines changed

.idea/gradle.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.xks.blogdemo;
2+
3+
import org.xmlpull.v1.XmlPullParser;
4+
import org.xmlpull.v1.XmlPullParserException;
5+
import org.xmlpull.v1.XmlPullParserFactory;
6+
7+
import java.io.IOException;
8+
import java.io.StringReader;
9+
10+
/**
11+
* Created by Xingfeng on 2016-10-24.
12+
*/
13+
14+
public class SimpleXMLPullApp {
15+
16+
public static void main(String[] args) throws XmlPullParserException, IOException {
17+
18+
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
19+
factory.setNamespaceAware(true);
20+
XmlPullParser xpp = factory.newPullParser();
21+
22+
xpp.setInput(new StringReader("<foo>Hello World!</foo>"));
23+
int eventType = xpp.getEventType();
24+
while (eventType != XmlPullParser.END_DOCUMENT) {
25+
if (eventType == XmlPullParser.START_DOCUMENT) {
26+
System.out.println("Start document");
27+
} else if (eventType == XmlPullParser.START_TAG) {
28+
System.out.println("Start tag " + xpp.getName());
29+
} else if (eventType == XmlPullParser.END_TAG) {
30+
System.out.println("End tag " + xpp.getName());
31+
} else if (eventType == XmlPullParser.TEXT) {
32+
System.out.println("Text " + xpp.getText());
33+
}
34+
eventType = xpp.next();
35+
}
36+
System.out.println("End document");
37+
38+
}
39+
40+
}

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.2.1'
8+
classpath 'com.android.tools.build:gradle:2.2.2'
99

1010
// NOTE: Do not place your application dependencies here; they belong
1111
// in the individual module build.gradle files

okhttpdemo/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

okhttpdemo/build.gradle

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 24
5+
buildToolsVersion "24.0.1"
6+
7+
defaultConfig {
8+
applicationId "com.xks.okhttpdemo"
9+
minSdkVersion 14
10+
targetSdkVersion 24
11+
versionCode 1
12+
versionName "1.0"
13+
14+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
15+
16+
}
17+
buildTypes {
18+
release {
19+
minifyEnabled false
20+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
21+
}
22+
}
23+
}
24+
25+
dependencies {
26+
compile fileTree(include: ['*.jar'], dir: 'libs')
27+
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
28+
exclude group: 'com.android.support', module: 'support-annotations'
29+
})
30+
compile 'com.android.support:appcompat-v7:24.2.1'
31+
testCompile 'junit:junit:4.12'
32+
compile 'com.squareup.okhttp3:okhttp:3.4.1'
33+
}

okhttpdemo/proguard-rules.pro

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in D:\SDK\Xingfeng\AppData\Local\Android\sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.xks.okhttpdemo;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumentation test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() throws Exception {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("com.xks.okhttpdemo", appContext.getPackageName());
25+
}
26+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.xks.okhttpdemo">
3+
4+
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
5+
android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
6+
<activity android:name=".MainActivity">
7+
<intent-filter>
8+
<action android:name="android.intent.action.MAIN" />
9+
10+
<category android:name="android.intent.category.LAUNCHER" />
11+
</intent-filter>
12+
</activity>
13+
</application>
14+
15+
</manifest>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.xks.okhttpdemo;
2+
3+
import java.io.IOException;
4+
5+
import okhttp3.Interceptor;
6+
import okhttp3.MediaType;
7+
import okhttp3.Request;
8+
import okhttp3.RequestBody;
9+
import okhttp3.Response;
10+
import okio.BufferedSink;
11+
import okio.GzipSink;
12+
import okio.Okio;
13+
14+
/**
15+
* Created by Xingfeng on 2016-10-21.
16+
*/
17+
18+
public class GZipRequestInterceptor implements Interceptor {
19+
20+
@Override
21+
public Response intercept(Chain chain) throws IOException {
22+
23+
Request originalRequest = chain.request();
24+
if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null) {
25+
return chain.proceed(originalRequest);
26+
}
27+
28+
Request compressedRequest = originalRequest.newBuilder()
29+
.header("Content-Encoding", "gzip")
30+
.method(originalRequest.method(), gzip(originalRequest.body()))
31+
.build();
32+
return chain.proceed(compressedRequest);
33+
}
34+
35+
private RequestBody gzip(final RequestBody body) {
36+
return new RequestBody() {
37+
@Override
38+
public MediaType contentType() {
39+
return body.contentType();
40+
}
41+
42+
@Override
43+
public long contentLength() {
44+
return -1; // We don't know the compressed length in advance!
45+
}
46+
47+
@Override
48+
public void writeTo(BufferedSink sink) throws IOException {
49+
BufferedSink gzipSink = Okio.buffer(new GzipSink(sink));
50+
body.writeTo(gzipSink);
51+
gzipSink.close();
52+
}
53+
};
54+
}
55+
}

0 commit comments

Comments
 (0)