├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── colors.xml │ │ │ │ └── styles.xml │ │ │ ├── mipmap-hdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-mdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ └── layout │ │ │ │ └── activity_main.xml │ │ ├── java │ │ │ └── cn │ │ │ │ └── mycommons │ │ │ │ └── androidhttp │ │ │ │ └── MainActivity.java │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── cn │ │ │ └── mycommons │ │ │ └── androidhttp │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── cn │ │ └── mycommons │ │ └── androidhttp │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── doc ├── chuck.gif ├── curl.png ├── paw.png ├── post_man.png ├── http_reponse.jpg ├── http_request.png ├── multiwindow.gif ├── chrome_dev_tools.png └── http_request_response.jpg ├── .gitignore ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradle.properties ├── gradlew.bat ├── gradlew ├── LICENSE └── README.md /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /doc/chuck.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidHttp/HEAD/doc/chuck.gif -------------------------------------------------------------------------------- /doc/curl.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidHttp/HEAD/doc/curl.png -------------------------------------------------------------------------------- /doc/paw.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidHttp/HEAD/doc/paw.png -------------------------------------------------------------------------------- /doc/post_man.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidHttp/HEAD/doc/post_man.png -------------------------------------------------------------------------------- /doc/http_reponse.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidHttp/HEAD/doc/http_reponse.jpg -------------------------------------------------------------------------------- /doc/http_request.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidHttp/HEAD/doc/http_request.png -------------------------------------------------------------------------------- /doc/multiwindow.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidHttp/HEAD/doc/multiwindow.gif -------------------------------------------------------------------------------- /doc/chrome_dev_tools.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidHttp/HEAD/doc/chrome_dev_tools.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | .DS_Store 5 | /build 6 | /captures 7 | .externalNativeBuild 8 | .idea -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | AndroidHttp 3 | 4 | -------------------------------------------------------------------------------- /doc/http_request_response.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidHttp/HEAD/doc/http_request_response.jpg -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidHttp/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidHttp/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidHttp/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidHttp/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidHttp/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidHttp/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidHttp/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidHttp/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidHttp/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidHttp/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidHttp/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Thu Mar 09 20:01:44 CST 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip 7 | -------------------------------------------------------------------------------- /app/src/main/java/cn/mycommons/androidhttp/MainActivity.java: -------------------------------------------------------------------------------- 1 | package cn.mycommons.androidhttp; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | 6 | public class MainActivity extends AppCompatActivity { 7 | 8 | @Override 9 | protected void onCreate(Bundle savedInstanceState) { 10 | super.onCreate(savedInstanceState); 11 | setContentView(R.layout.activity_main); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/cn/mycommons/androidhttp/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package cn.mycommons.androidhttp; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /app/src/androidTest/java/cn/mycommons/androidhttp/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package cn.mycommons.androidhttp; 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 Testing documentation 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("cn.mycommons.androidhttp", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/xiaqiulei/Documents/dev/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 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.2" 6 | defaultConfig { 7 | applicationId "cn.mycommons.androidhttp" 8 | minSdkVersion 15 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | compile 'com.android.support:appcompat-v7:25.2.0' 28 | compile 'com.android.support.constraint:constraint-layout:1.0.2' 29 | testCompile 'junit:junit:4.12' 30 | } 31 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 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 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 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 Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Android Http网络开发神兵利器 2 | 3 | --- 4 | 5 | [TOC] 6 | 7 | 本人在Android移动端开发已经有不少年的经验,经历颇多,感受颇深,特写下此文记录下在Android Http网络开发中的各种工具使用,如果有其他想法和建议,欢迎前来交流 xiaqiulei@126.com 。 本人文笔较差,请不要见怪。 8 | 9 | Android开发的时候,很多APP都是基于网络请求的,大部分应用都是基于http的,少部分是基于Socket的,本文主要介绍Android Http开发的主要实践和工具。 10 | 11 | 本文最先发表于[Github](https://github.com/LiushuiXiaoxia/AndroidHttp),如有转载,请注明转载出处。 12 | 13 | # Http协议 14 | 15 | 说起Http网络请求,那么要介绍下Http协议,我在公司面试的时候也很喜欢问这个问题。很多做好了3-5年的工程师,竟然不知道Http协议,只知道怎么调用类库,不知道原理。 16 | 17 | 作为一个有追求的程序员,Http协议还是需要了解的。知其然知其所以然,在了解Http协议的情况下,才能在实际开发中灵活运用和优化。 18 | 19 | 这里有篇 [文章](http://www.cnblogs.com/ranyonsue/p/5984001.html) ,Http协议介绍的很详细。 20 | 21 | ## HTTP简介 22 | 23 | HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送协议。 24 | 25 | HTTP是一个基于TCP/IP通信协议来传递数据(HTML 文件, 图片文件, 查询结果等)。 26 | 27 | HTTP是一个属于应用层的面向对象的协议,由于其简捷、快速的方式,适用于分布式超媒体信息系统。它于1990年提出,经过几年的使用与发展,得到不断地完善和扩展。目前在WWW中使用的是HTTP/1.0的第六版,HTTP/1.1的规范化工作正在进行之中,而且HTTP-NG(Next Generation of HTTP)的建议已经提出。 28 | 29 | HTTP协议工作于客户端-服务端架构为上。浏览器作为HTTP客户端通过URL向HTTP服务端即WEB服务器发送所有请求。Web服务器根据接收到的请求后,向客户端发送响应信息。 30 | 31 | ![](https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidHttp/master/doc/http_request_response.jpg) 32 | 33 | ## 主要特点 34 | 35 | 1、简单快速:客户向服务器请求服务时,只需传送请求方法和路径。请求方法常用的有GET、HEAD、POST。每种方法规定了客户与服务器联系的类型不同。由于HTTP协议简单,使得HTTP服务器的程序规模小,因而通信速度很快。 36 | 37 | 2、灵活:HTTP允许传输任意类型的数据对象。正在传输的类型由Content-Type加以标记。 38 | 39 | 3.无连接:无连接的含义是限制每次连接只处理一个请求。服务器处理完客户的请求,并收到客户的应答后,即断开连接。采用这种方式可以节省传输时间。 40 | 41 | 4.无状态:HTTP协议是无状态协议。无状态是指协议对于事务处理没有记忆能力。缺少状态意味着如果后续处理需要前面的信息,则它必须重传,这样可能导致每次连接传送的数据量增大。另一方面,在服务器不需要先前信息时它的应答就较快。 42 | 43 | 5、支持B/S及C/S模式。 44 | 45 | ## Request 46 | 47 | ![Http请求消息结构](https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidHttp/master/doc/http_request.png) 48 | 49 | 上图是一个Http请求消息结构,我们可以使用抓包工具抓取网络请求,因为是Get请求,所以后面没有请求数据 50 | 51 | ``` 52 | GET /562f25980001b1b106000338.jpg HTTP/1.1 53 | Host img.mukewang.com 54 | User-Agent Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36 55 | Accept image/webp,image/*,*/*;q=0.8 56 | Referer http://www.imooc.com/ 57 | Accept-Encoding gzip, deflate, sdch 58 | Accept-Language zh-CN,zh;q=0.8 59 | ``` 60 | 61 | 第一部分:请求行,用来说明请求类型,要访问的资源以及所使用的HTTP版本. 62 | 63 | GET说明请求类型为GET,[/562f25980001b1b106000338.jpg]为要访问的资源,该行的最后一部分说明使用的是HTTP1.1版本。 64 | 65 | 第二部分:请求头部,紧接着请求行(即第一行)之后的部分,用来说明服务器要使用的附加信息 66 | 67 | 从第二行起为请求头部,HOST将指出请求的目的地.User-Agent,服务器端和客户端脚本都能访问它,它是浏览器类型检测逻辑的重要基础.该信息由你的浏览器来定义,并且在每个请求中自动发送等等 68 | 69 | 第三部分:空行,请求头部后面的空行是必须的 70 | 71 | 即使第四部分的请求数据为空,也必须有空行。 72 | 73 | 第四部分:请求数据也叫主体,可以添加任意的其他数据。 74 | 75 | 这个例子的请求数据为空。 76 | 77 | 下面这个例子是Post方式。 78 | 79 | ``` 80 | POST / HTTP1.1 81 | Host:www.wrox.com 82 | User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022) 83 | Content-Type:application/x-www-form-urlencoded 84 | Content-Length:40 85 | Connection: Keep-Alive 86 | 87 | name=Professional%20Ajax&publisher=Wiley 88 | ``` 89 | 90 | 第一部分:请求行,第一行明了是post请求,以及http1.1版本。 91 | 92 | 第二部分:请求头部,第二行至第六行。 93 | 94 | 第三部分:空行,第七行的空行。 95 | 96 | 第四部分:请求数据,第八行。 97 | 98 | ## Response 99 | 100 | 一般情况下,服务器接收并处理客户端发过来的请求后会返回一个HTTP的响应消息。 101 | 102 | HTTP响应也由四个部分组成,分别是:状态行、消息报头、空行和响应正文。 103 | 104 | ![Http响应消息结构](https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidHttp/master/doc/http_reponse.jpg) 105 | 106 | 107 | ``` 108 | HTTP/1.1 200 OK 109 | Date: Fri, 22 May 2009 06:07:21 GMT 110 | Content-Type: text/html; charset=UTF-8 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | ``` 119 | 120 | 第一部分:状态行,由HTTP协议版本号, 状态码, 状态消息 三部分组成。 121 | 122 | 第一行为状态行,(HTTP/1.1)表明HTTP版本为1.1版本,状态码为200,状态消息为(ok) 123 | 124 | 第二部分:消息报头,用来说明客户端要使用的一些附加信息 125 | 126 | 第二行和第三行为消息报头, 127 | Date:生成响应的日期和时间;Content-Type:指定了MIME类型的HTML(text/html),编码类型是UTF-8 128 | 129 | 第三部分:空行,消息报头后面的空行是必须的 130 | 131 | 第四部分:响应正文,服务器返回给客户端的文本信息。 132 | 133 | 空行后面的html部分为响应正文。 134 | 135 | ## Code 136 | 137 | 状态代码有三位数字组成,第一个数字定义了响应的类别,共分五种类别: 138 | 139 | 1xx:指示信息--表示请求已接收,继续处理 140 | 141 | 2xx:成功--表示请求已被成功接收、理解、接受 142 | 143 | 3xx:重定向--要完成请求必须进行更进一步的操作 144 | 145 | 4xx:客户端错误--请求有语法错误或请求无法实现 146 | 147 | 5xx:服务器端错误--服务器未能实现合法的请求 148 | 149 | 常见状态码: 150 | 151 | ``` 152 | 200 OK // 客户端请求成功 153 | 400 Bad Request // 客户端请求有语法错误,不能被服务器所理解 154 | 401 Unauthorized // 请求未经授权,这个状态代码必须和WWW-Authenticate报头域一起使用 155 | 403 Forbidden // 服务器收到请求,但是拒绝提供服务 156 | 404 Not Found // 请求资源不存在,eg:输入了错误的URL 157 | 500 Internal Server Error // 服务器发生不可预期的错误 158 | 503 Server Unavailable // 服务器当前不能处理客户端的请求,一段时间后可能恢复正常 159 | ``` 160 | 161 | ## Method 162 | 163 | 根据HTTP标准,HTTP请求可以使用多种请求方法。 164 | HTTP1.0定义了三种请求方法: GET, POST 和 HEAD方法。 165 | HTTP1.1新增了五种请求方法:OPTIONS, PUT, DELETE, TRACE 和 CONNECT 方法。 166 | 167 | ``` 168 | GET 请求指定的页面信息,并返回实体主体。 169 | HEAD 类似于get请求,只不过返回的响应中没有具体的内容,用于获取报头 170 | POST 向指定资源提交数据进行处理请求(例如提交表单或者上传文件)。数据被包含在请求体中。POST请求可能会导致新的资源的建立和/或已有资源的修改。 171 | PUT 从客户端向服务器传送的数据取代指定的文档的内容。 172 | DELETE 请求服务器删除指定的页面。 173 | CONNECT HTTP/1.1协议中预留给能够将连接改为管道方式的代理服务器。 174 | OPTIONS 允许客户端查看服务器的性能。 175 | TRACE 回显服务器收到的请求,主要用于测试或诊断。 176 | ``` 177 | 178 | ## Header 179 | 180 | HTTP消息由客户端到服务器的请求和服务器到客户端的响应组成。请求消息和响应消息都是由开始行(对于请求消息,开始行就是请求行,对于响应消息,开始行就是状态行),消息报头(可选),空行(只有CRLF的行),消息正文(可选)组成。 181 | 182 | HTTP消息报头包括普通报头、请求报头、响应报头、实体报头。 183 | 每一个报头域都是由名字+“:”+空格+值 组成,消息报头域的名字是大小写无关的。 184 | 185 | # 网络请求库 186 | 187 | 下面介绍几个常见的Android http网路开发的工具库。 188 | 189 | ## URLConnection 190 | 191 | URLConnection是Java JDK中自带的网络请求类,使用的场景不多,因为使用比较繁琐,直接使用底层IO流进行开发,需要了解Http协议才能很好的使用,很多时候需要自己封装,不方便。一般的一些提供第三方服务的sdk会使用,一是为了保证sdk体积大小,二是减少不必要的第三方库引入。 192 | 193 | 这里不做详细介绍, [这里有一篇博可以详细介绍了](http://www.blogjava.net/supercrsky/articles/247449.html) 。 194 | 195 | ## Apache HttpClient 196 | 197 | HttpClient是Apache Jakarta Common下的子项目,用来提供高效的、最新的、功能丰富的支持HTTP协议的客户端编程工具包,并且它支持HTTP协议最新的版本和建议。HttpClient已经应用在很多的项目中,比如Apache Jakarta上很著名的另外两个开源项目Cactus和HTMLUnit都使用了HttpClient。 198 | 199 | HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性,它不仅是客户端发送Http请求变得容易,而且也方便了开发人员测试接口(基于Http协议的),即提高了开发的效率,也方便提高代码的健壮性。 200 | 201 | 202 | Android在低版本的系统Api中也有Httpclient相关类,但是后来就已经废弃,相关资料可以 [参考这里](https://developer.android.com/reference/org/apache/http/client/package-summary.html). 203 | 204 | [HttpClient相关博客](http://blog.csdn.net/wangpeng047/article/details/19624529) 205 | 206 | ## Volley 207 | 208 | [Android Volley](https://developer.android.com/training/volley/index.html) 是在2013年Google I/O大会上推出了一个新的网络通信框架——Volley。Volley可是说是把AsyncHttpClient和Universal-Image-Loader的优点集于了一身,既可以像AsyncHttpClient一样非常简单地进行HTTP通信,也可以像Universal-Image-Loader一样轻松加载网络上的图片。除了简单易用之外,Volley在性能方面也进行了大幅度的调整,它的设计目标就是非常适合去进行数据量不大,但通信频繁的网络操作,而对于大数据量的网络操作,比如说下载文件等,Volley的表现就会非常糟糕。 209 | 210 | 有 [资料](http://blog.csdn.net/guolin_blog/article/details/12452307) 指出Volley在进行网络请求时,在Android 2.3及以上版本,使用的是HttpURLConnection,而在Android 2.2及以下版本,使用的是HttpClient。主要是因为HttpURLConnection存在相关bug。 211 | 212 | [Volley相关资料](http://blog.csdn.net/guolin_blog/article/details/17482095) 213 | 214 | [Volley相关资料](http://blog.csdn.net/guolin_blog/article/details/12452307) 215 | 216 | ## OkHttp & Retrofit 217 | 218 | OkHttp 是Square公司开源的处理网络请求的开源项目,是安卓端最火热的轻量级框架,用于替代HttpUrlConnection和Apache HttpClient。最新的版本是OkHttp3,Github地址在 [这里](https://github.com/square/okhttp) 。 219 | 220 | Retrofit 也是Square公司开发的一款针对Android网络请求的框架,Retrofit2底层基于OkHttp实现的,OkHttp现在已经得到Google官方认可,大量的app都采用OkHttp做网络请求, [Github地址](https://github.com/square/retrofit) 。 221 | 222 | 网络上也有很多关于OkHttp、Retrofit的示例和资料,而且是目前Android移动端最流行的框架,还是很值得了解一下,在这里就不做详细介绍。 223 | 224 | # 工具 225 | 226 | ## Charles 227 | 228 | Charles是一个HTTP代理服务器,HTTP监视器,反转代理服务器·它允许一个开发者查看所有连接互联网的HTTP通信·这些包括request、response、headers(包含cookies与caching信息),由于笔者的开发机器是Mac,所以只介绍了Charles,Windows开发环境的同学可以使用fiddler。 229 | 230 | [Charles介绍](http://blog.devtang.com/2015/11/14/charles-introduction/) 231 | 232 | Charles 主要的功能包括: 233 | 234 | > 截取 Http 和 Https 网络封包。 235 | 236 | > 支持网络请求调试,设置断点,方便调试 237 | 238 | > 支持重发网络请求,方便后端调试。 239 | 240 | > 支持修改网络请求参数。 241 | 242 | > 支持网络请求的截获并动态修改。 243 | 244 | > 支持模拟弱网。 245 | 246 | 247 | 上面介绍了,可以使用Charles方便设置断点,方便调试,可以很容易的修改数据包,这样可以动态修改数据,mock api接口,在实际开发过程中很有用。 248 | 249 | [Charles Mock 数据](http://blog.csdn.net/guijiaoba/article/details/43198503) 250 | 251 | 252 | ## PostMan & Paw 253 | 254 | 上面介绍了网络抓包工具Charles,先介绍2款模拟http请求的软件。 255 | 256 | Postman是一款功能强大的网页调试与发送网页HTTP请求的Chrome插件,使用很简单。 257 | 258 | 下图模拟发送知乎Api的网络请求。 259 | 260 | `http://news-at.zhihu.com/api/4/themes` 261 | 262 | ![](https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidHttp/master/doc/post_man.png) 263 | 264 | [Postman官网](https://www.getpostman.com/) 265 | 266 | [Postman资料](http://chromecj.com/web-development/2014-09/60.html) 267 | 268 | Paw 也是类似于Postman的一款模拟http请求的软件,只不过postman依赖于chrome(经网友指正,postman也有独立客户端),所以个人更加喜欢paw,下图是paw请求知乎api的示例。BTW, paw是一款收费的软件,不过么网上也有破解的,在这里就不介绍如何获取了,反正大家都懂的。 269 | 270 | ![](https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidHttp/master/doc/paw.png) 271 | 272 | [Paw 官网](https://paw.cloud/) 273 | 274 | [Paw 使用教程](https://yq.aliyun.com/articles/29772) 275 | 276 | ## Chrome Dev Tools 277 | 278 | Chrome dev tools是Google Chrome浏览器中自带一块调试工具。如果你是做软件开发的,基本都了解过。 279 | 280 | 虽然Chrome dev tools 在web开发中使用很多,但是在移动端中也是有用到的,这个后面再介绍。 281 | 282 | ![](https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidHttp/master/doc/chrome_dev_tools.png) 283 | 284 | [Chrome dev tools 相关资料](https://developer.chrome.com/devtools) 285 | 286 | [Chrome dev tools 相关资料](https://github.com/GoogleChrome/devtools-docs) 287 | 288 | [Chrome dev tools 相关资料](https://segmentfault.com/a/1190000000683599) 289 | 290 | ## curl 291 | 292 | curl也是和上面poastman与paw功能类似的,不过没有和上面两款放在一起讲是因为curl比较重要,而且具有统一标准性与方便性。 293 | 294 | curl是利用URL语法在命令行方式下工作的开源文件传输工具。它被广泛应用在Unix、多种Linux发行版中,并且有DOS和Win32、Win64下的移植版本。 295 | 296 | 还是原先的例子,请求知乎的api。 297 | 298 | ![](https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidHttp/master/doc/curl.png) 299 | 300 | 很简单在curl命令直接加入url即可发送网络请求。 301 | 302 | 为什么有postman和paw这么友好的软件后,还需要介绍的curl呢,因为curl是一个命令,是文本,是字符串,当你其他同事开发联调的时候,直接通过im工具发送即可,这样网络请求就能很容移动复现。很多的上面介绍的charles、paw、google dev tool都支持把某一条请求导出为curl命令。这就相当于curl是一个标准,可以在不同机器上面复用,所以了解和使用curl是很有必要的。比如联调开发时候,服务器某个api调试不通过,需要与服务器同学沟通与复习,你直接丢一个curl语句即可,如果对方很懂,那么会觉的你很专业,如果对方不懂,顺便可以装个逼。 303 | 304 | [curl 资料](http://www.cnblogs.com/gbyukg/p/3326825.html) 305 | 306 | [curl 官网](https://curl.haxx.se/) 307 | 308 | 309 | # OkHttp相关 310 | 311 | 前面介绍了OkHttp,下面介绍几个基于OkHttp拦截器机制衍生出的常用的工具。 312 | 313 | ## logging-interceptor 314 | 315 | 开发中经常需要对网络请求进行查看,抓包是可以的,不过现在很多app都是全栈https,想要抓包就没有那么容易了,那么最简单的还是打日志。 316 | 317 | `logging-interceptor`就是干这个事的,在初始化okhttpclient的时候,添加下这个拦截器,那么所有的网络请求会被记录下来。 318 | 319 | [相关资料](https://github.com/square/okhttp/wiki/Interceptors) 320 | 321 | [相关资料](https://github.com/ihsanbal/LoggingInterceptor) 322 | 323 | ## Stetho 324 | 325 | Stetho是Facebook开源的一个工具,前面说过Google Chrome dev tools,那么Stetho可以让Chrome调试Android,监控网络请求。 326 | 327 | 同时Stetho还支持查看数据库,SharePreference等,而这个前提是必须root,查看view树,使用Stetho也可以做到。 328 | 329 | 网上也有很多也有很多相关资料,这里不介绍详细使用。 330 | 331 | [stetho github](https://github.com/facebook/stetho) 332 | 333 | [stetho 相关资料](http://www.jianshu.com/p/03da9f91f41f) 334 | 335 | ## chuck 336 | 337 | 上面介绍的两款工具,一种是需要在logcat中查看网络请求数据,一种是需要在chrome中查看数据,有没有办法不需要借助任何第三方工具,直接查看呢,答案是肯定的。chuck是我最近关注比较高的一个开源工具,而且很快的就在项目中使用了。 338 | 339 | 下面官网的截图,可以先了解下chuck的使用效果。 340 | 341 | ![](https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidHttp/master/doc/chuck.gif) 342 | 343 | 在高版本的Android中,还支持多屏显示,那么一边操作一边显示。 344 | 345 | ![](https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidHttp/master/doc/multiwindow.gif) 346 | 347 | 集成的方法都很简单,只需要在初始化OkHttpClient的时候,添加对应的拦截器即可。 348 | 349 | Gradle依赖 350 | 351 | ```gradle 352 | dependencies { 353 | debugCompile 'com.readystatesoftware.chuck:library:1.0.4' 354 | releaseCompile 'com.readystatesoftware.chuck:library-no-op:1.0.4' 355 | } 356 | ``` 357 | 358 | 添加拦截器 359 | 360 | ```java 361 | OkHttpClient client = new OkHttpClient.Builder() 362 | .addInterceptor(new ChuckInterceptor(context)) 363 | .build(); 364 | ``` 365 | 366 | [Chuck github](https://github.com/jgilfelt/chuck) 367 | 368 | # 其他 369 | 370 | ## RESTful 371 | 372 | RESTful一种软件架构风格,设计风格而不是标准,只是提供了一组设计原则和约束条件。它主要用于客户端和服务器交互类的软件。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。 373 | 374 | REST(英文:Representational State Transfer,简称REST)描述了一个架构样式的网络系统,比如 web 应用程序。它首次出现在 2000 年 Roy Fielding 的博士论文中,他是 HTTP 规范的主要编写者之一。在目前主流的三种Web服务交互方案中,REST相比于SOAP(Simple Object Access protocol,简单对象访问协议)以及XML-RPC更加简单明了,无论是对URL的处理还是对Payload的编码,REST都倾向于用更加简单轻量的方法设计和实现。值得注意的是REST并没有一个明确的标准,而更像是一种设计的风格。 375 | 376 | 虽然本文的主题是介绍Android Http开发的相关内容,并且RESTful是服务端的一种概念。但是我觉的,还是有很必要了解的,最起码可以作为判断你的服务端小伙伴给的接口是否满足RESTful的标准的依据。 377 | 378 | 后面贴一篇[阮一峰大神的博客介绍RESTful](http://www.ruanyifeng.com/blog/2011/09/restful) 379 | 380 | # 总结 381 | 382 | ## 心得 383 | 384 | 上面介绍了很多种关于网络请求的工具,具体可以分为这几类。 385 | 386 | HTTP接口调用工具,这类工具主要是模拟客户端,发起网络请求,方便调试服务器接口。 387 | 388 | * postman 389 | * paw 390 | * curl 391 | 392 | 抓包工具,这类工具不光适合Android开发,也适合其他网络开发,工具使用简单,不过在使用前需要做少许代理配置。WiresShark是一款更加强大的工具,使用比较复杂,而且比较底层,所以在本文中就没介绍,有兴趣的同学可以自己查阅相关资料。 393 | 394 | * Charles 395 | * Fiddler 396 | * WiresShark 397 | 398 | Android Http请求工具库,这类工具类变化很快,经常会过个几年就会有新的一种工具替换,但是本质思想还是一样的,都是基于URLConnection和HTTP做了一次封装调用。 399 | 400 | * URLConnection 401 | * HttpClient 402 | * Volley 403 | * OkHttp 404 | 405 | OkHttp相关,这类工具都是基于OkHttp做了二次封装开发,这个主要归功于OkHttp强大开发模式和架构,开发人员可以方便对OkHttp进行自定义和二次开发。工具很强大,不过也有局限性,只能依附于OkHttp的框架,不利于扩展到其他平台和工具。 406 | 407 | * logging-interceptor 408 | * Stetho 409 | * Chuck 410 | 411 | ## 工具对比 412 | 413 | 上面介绍好几款可以监控App网络请求的工具,那么如何根据实际情况,选择一款适合自己项目和团队的工具呢? 414 | 415 | 416 | ### Charles & Fiddler 417 | 418 | 这类工具不依赖与平台和App开发的工具库,不管你是Android、还是iOS,不管使用的是Volley,还是OkHttp都是可以监控的,同时还支持HTTP的调试,方便mock数据,简直是神器。 419 | 420 | 不过也有少许缺点,比如是不支持Https,如果你的app使用的是https接口,那么这类工具一般情况是不能使用的,不过也有办法,比如把charles的证书设置为根证书,则可以检测到。但是也是有局限的,如果app设置强制校验https证书,此类方法还是不可行。 421 | 422 | 因为平台无光,可检测到某个手机设备上面的全部网络请求,数据量有时候会很大,如果经常调试某一个app,那么域名一般是固定的,可进行相对应的过滤。 423 | 424 | ### OkHttp相关 425 | 426 | 通过标题可以看成,此类工具必须依附于OKHttp,那么只能满足Android平台并且App的网络请求库是OkHttp。上面介绍的三款工具,可以选择性的选择其中几种或者是全部,暂时还没有发现冲突的地方。因为是直接嵌入到代码中,那么Https也是可以监控到。同时也只能监控当前设备上的当前应用,不可以检测其他的app。 427 | 428 | 不过在发布线上或者正式颁布时,需要手动去除这些代码,否则会有很大麻烦,同时也会把相关的隐私和安全反馈给使用的用户。 429 | 430 | 好了,上面就是关于App网络情况的监控工具比较,建议两个大分类都可以几个或者多个配合使用,具体问题在根据具体工具使用,方便问题解决与网络监控。 431 | 432 | ## 其他建议 433 | 434 | 上面介绍的OkHttp的相关的工具,都是需要在初始化OkHttpClient的时候,手动添加对应的拦截器,如果忘记添加则不会产生效果。 435 | 436 | 那么可以使用现在流行的Aop技术,在编译时候,会在所有调用OkHttpClient的地方,自动添加,保证不会忘记,如果你的App还是用其他的sdk,sdk里面也是用OKhttp的话,也会监控到sdk里面的网络请求,不少大厂已经使用了这种方法,然后收集请求数据到后端,方便管理和优化。 437 | 438 | 下面简单介绍相关代码: 439 | 440 | 首先集成`gradle_plugin_android_aspectjx`插件 441 | 442 | ```gradle 443 | dependencies { 444 | classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:1.0.9' 445 | } 446 | ``` 447 | 448 | 然后定义Aspectj切入点 449 | 450 | ```java 451 | 452 | import com.facebook.stetho.okhttp3.StethoInterceptor; 453 | import com.readystatesoftware.chuck.ChuckInterceptor; 454 | 455 | import org.aspectj.lang.ProceedingJoinPoint; 456 | import org.aspectj.lang.annotation.Around; 457 | import org.aspectj.lang.annotation.Aspect; 458 | import org.aspectj.lang.annotation.Pointcut; 459 | 460 | import me.ele.breakfastapp.AppContext; 461 | import okhttp3.OkHttpClient; 462 | 463 | @Aspect 464 | public class OkHttpAspect { 465 | 466 | @Pointcut("call(public okhttp3.OkHttpClient build())") 467 | public void build() { 468 | } 469 | 470 | @Around("build()") 471 | public Object aroundBuild(ProceedingJoinPoint joinPoint) throws Throwable { 472 | Object target = joinPoint.getTarget(); 473 | if (target instanceof OkHttpClient.Builder) { 474 | OkHttpClient.Builder builder = (OkHttpClient.Builder) target; 475 | builder.addNetworkInterceptor(new StethoInterceptor()); 476 | builder.addNetworkInterceptor(new ChuckInterceptor(AppContext.get())); 477 | } 478 | 479 | return joinPoint.proceed(); 480 | } 481 | } 482 | ``` 483 | 484 | 那么在代码编译的时候,会自动插入字节码。 485 | 486 | Aop相关资料: 487 | 488 | [Aop介绍](http://blog.csdn.net/innost/article/details/49387395) 489 | 490 | [Aop资料](http://blog.csdn.net/crazy__chen/article/details/52014672) 491 | 492 | [Assectj](http://www.jianshu.com/p/bb9343e8af17) 493 | 494 | [Assectj Gradle 插件](https://github.com/HujiangTechnology/gradle_plugin_android_aspectjx) 495 | 496 | # 参考资料 497 | 498 | [Http博客](http://www.cnblogs.com/ranyonsue/p/5984001.html) 499 | 500 | [Http博客](http://www.cnblogs.com/li0803/archive/2008/11/03/1324746.html) 501 | 502 | [URLConnection](http://www.blogjava.net/supercrsky/articles/247449.html) 503 | 504 | [Android HttpClient](https://developer.android.com/reference/org/apache/http/client/package-summary.html) 505 | 506 | [HttpClient相关博客](http://blog.csdn.net/wangpeng047/article/details/19624529) 507 | 508 | [Volley相关资料](http://blog.csdn.net/guolin_blog/article/details/17482095) 509 | 510 | [Volley相关资料](http://blog.csdn.net/guolin_blog/article/details/12452307) 511 | 512 | [OkHttp](https://github.com/square/okhttp) 513 | 514 | [Retrofit Github地址](https://github.com/square/retrofit) 515 | 516 | [Charles介绍](http://blog.devtang.com/2015/11/14/charles-introduction/) 517 | 518 | [Charles Mock 数据](http://blog.csdn.net/guijiaoba/article/details/43198503) 519 | 520 | [Postman官网](https://www.getpostman.com/) 521 | 522 | [Postman资料](http://chromecj.com/web-development/2014-09/60.html) 523 | 524 | [Paw 官网](https://paw.cloud/) 525 | 526 | [Paw 使用教程](https://yq.aliyun.com/articles/29772) 527 | 528 | [Chrome dev tools 相关资料](https://developer.chrome.com/devtools) 529 | 530 | [Chrome dev tools 相关资料](https://github.com/GoogleChrome/devtools-docs) 531 | 532 | [Chrome dev tools 相关资料](https://segmentfault.com/a/1190000000683599) 533 | 534 | [curl 资料](http://www.cnblogs.com/gbyukg/p/3326825.html) 535 | 536 | [curl 官网](https://curl.haxx.se/) 537 | 538 | [Interceptors相关资料](https://github.com/square/okhttp/wiki/Interceptors) 539 | 540 | [LoggingInterceptors相关资料](https://github.com/ihsanbal/LoggingInterceptor) 541 | 542 | [stetho github](https://github.com/facebook/stetho) 543 | 544 | [stetho 相关资料](http://www.jianshu.com/p/03da9f91f41f) 545 | 546 | [Chuck github](https://github.com/jgilfelt/chuck) 547 | 548 | [RESTful](http://www.ruanyifeng.com/blog/2011/09/restful) 549 | 550 | [Aop介绍](http://blog.csdn.net/innost/article/details/49387395) 551 | 552 | [Aop资料](http://blog.csdn.net/crazy__chen/article/details/52014672) 553 | 554 | [Assectj](http://www.jianshu.com/p/bb9343e8af17) 555 | 556 | [Assectj Gradle 插件](https://github.com/HujiangTechnology/gradle_plugin_android_aspectjx) 557 | --------------------------------------------------------------------------------