├── OkHttpDemo ├── src │ └── main │ │ ├── res │ │ ├── values │ │ │ ├── styles.xml │ │ │ ├── strings.xml │ │ │ └── colors.xml │ │ ├── mipmap-hdpi │ │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ │ └── ic_launcher.png │ │ └── layout │ │ │ └── activity_main.xml │ │ ├── asset │ │ └── demo.html │ │ ├── java │ │ └── com │ │ │ ├── example │ │ │ └── jack │ │ │ │ └── okhttpdemo │ │ │ │ ├── Constants.java │ │ │ │ ├── BaiduWeight.java │ │ │ │ ├── Result.java │ │ │ │ └── DnsBean.java │ │ │ └── jack │ │ │ └── okhttp │ │ │ ├── exception │ │ │ └── OkHttpException.java │ │ │ ├── listener │ │ │ ├── DownloadResponseHandler.java │ │ │ ├── DisposeDataListener.java │ │ │ ├── DisposeDataHandler.java │ │ │ └── MyDownloadCallback.java │ │ │ ├── request │ │ │ ├── RequestParams.java │ │ │ └── CommonRequest.java │ │ │ ├── response │ │ │ ├── CommonFileCallback.java │ │ │ └── CommonJsonCallback.java │ │ │ ├── utils │ │ │ └── JsonHelper.java │ │ │ ├── body │ │ │ └── ResponseProgressBody.java │ │ │ └── CommonOkHttpClient.java │ │ └── AndroidManifest.xml ├── libs │ └── gson-2.2.4.jar ├── build.gradle └── proguard-rules.pro ├── Volley ├── ic_launcher-web.png ├── libs │ └── android-support-v4.jar ├── res │ ├── drawable-hdpi │ │ └── ic_launcher.png │ ├── drawable-ldpi │ │ └── ic_launcher.png │ ├── drawable-mdpi │ │ └── ic_launcher.png │ ├── drawable-xhdpi │ │ └── ic_launcher.png │ ├── values │ │ ├── strings.xml │ │ └── styles.xml │ ├── menu │ │ └── activity_main.xml │ ├── values-v11 │ │ └── styles.xml │ └── values-v14 │ │ └── styles.xml ├── .settings │ └── org.eclipse.jdt.core.prefs ├── project.properties ├── proguard-project.txt ├── src │ └── com │ │ └── android │ │ └── volley │ │ ├── TimeoutError.java │ │ ├── NoConnectionError.java │ │ ├── Network.java │ │ ├── ServerError.java │ │ ├── ParseError.java │ │ ├── toolbox │ │ ├── Authenticator.java │ │ ├── NoCache.java │ │ ├── HttpStack.java │ │ ├── ClearCacheRequest.java │ │ ├── JsonArrayRequest.java │ │ ├── StringRequest.java │ │ ├── Volley.java │ │ ├── JsonObjectRequest.java │ │ ├── PoolingByteArrayOutputStream.java │ │ ├── JsonRequest.java │ │ ├── AndroidAuthenticator.java │ │ ├── RequestFuture.java │ │ ├── HttpHeaderParser.java │ │ └── ByteArrayPool.java │ │ ├── NetworkError.java │ │ ├── ResponseDelivery.java │ │ ├── RetryPolicy.java │ │ ├── VolleyError.java │ │ ├── NetworkResponse.java │ │ ├── AuthFailureError.java │ │ ├── Response.java │ │ ├── Cache.java │ │ ├── DefaultRetryPolicy.java │ │ ├── ExecutorDelivery.java │ │ ├── NetworkDispatcher.java │ │ ├── CacheDispatcher.java │ │ └── VolleyLog.java ├── AndroidManifest.xml ├── build.gradle └── README.md ├── VolleyDemo ├── ic_launcher-web.png ├── res │ ├── drawable-hdpi │ │ └── ic_launcher.png │ ├── drawable-mdpi │ │ └── ic_launcher.png │ ├── drawable-xhdpi │ │ └── ic_launcher.png │ ├── drawable-xxhdpi │ │ └── ic_launcher.png │ ├── values │ │ ├── strings.xml │ │ └── styles.xml │ └── layout │ │ ├── activity_img.xml │ │ └── activity_main.xml ├── .settings │ └── org.eclipse.jdt.core.prefs ├── src │ └── com │ │ └── example │ │ └── volley_demo │ │ ├── Constants.java │ │ ├── MyApplication.java │ │ ├── BitmapCache.java │ │ ├── VolleyListener.java │ │ ├── CustomVolleyRequest.java │ │ ├── ImageActivity.java │ │ └── MainActivity.java ├── project.properties ├── build.gradle ├── proguard-project.txt └── AndroidManifest.xml ├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── WebViewDemo ├── src │ └── main │ │ ├── res │ │ ├── values │ │ │ ├── strings.xml │ │ │ ├── colors.xml │ │ │ └── styles.xml │ │ ├── mipmap-hdpi │ │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ │ └── ic_launcher.png │ │ └── layout │ │ │ ├── activity_second.xml │ │ │ └── activity_web_view.xml │ │ ├── assets │ │ ├── error.html │ │ └── showjs.html │ │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── david │ │ │ └── webviewdemo │ │ │ ├── SecondActivity.java │ │ │ └── ShowJS.java │ │ └── AndroidManifest.xml ├── proguard-rules.pro └── build.gradle ├── AnroidHttpDemo ├── src │ └── main │ │ ├── res │ │ ├── values │ │ │ ├── strings.xml │ │ │ ├── colors.xml │ │ │ └── styles.xml │ │ ├── mipmap-hdpi │ │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ │ └── ic_launcher.png │ │ └── layout │ │ │ └── activity_main.xml │ │ └── AndroidManifest.xml ├── build.gradle └── proguard-rules.pro ├── JavaSocketDemo ├── build.gradle ├── src │ └── main │ │ └── java │ │ ├── com │ │ └── example │ │ │ ├── InetAddressDemo.java │ │ │ ├── UrlDemo.java │ │ │ └── UrlReadDemo.java │ │ ├── udp_socket_server_client │ │ ├── UDPClient.java │ │ └── UDPServer.java │ │ └── http_socket_server_client │ │ ├── multi │ │ ├── MultiServer.java │ │ ├── MultiClient.java │ │ └── ServerThread.java │ │ └── single │ │ ├── Client.java │ │ └── Server.java └── html │ └── urlreaddemo_baidu.html ├── gradlew.bat ├── README.md └── gradlew /OkHttpDemo/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /Volley/ic_launcher-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuningjack/AndroidNet/HEAD/Volley/ic_launcher-web.png -------------------------------------------------------------------------------- /OkHttpDemo/libs/gson-2.2.4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuningjack/AndroidNet/HEAD/OkHttpDemo/libs/gson-2.2.4.jar -------------------------------------------------------------------------------- /VolleyDemo/ic_launcher-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuningjack/AndroidNet/HEAD/VolleyDemo/ic_launcher-web.png -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':Volley', ':VolleyDemo', ':JavaSocketDemo', ':WebViewDemo', ':AnroidHttpDemo', ':OkHttpDemo' -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuningjack/AndroidNet/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /OkHttpDemo/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | OkHttpDemo 3 | 4 | -------------------------------------------------------------------------------- /Volley/libs/android-support-v4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuningjack/AndroidNet/HEAD/Volley/libs/android-support-v4.jar -------------------------------------------------------------------------------- /WebViewDemo/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | WebViewDemo 3 | 4 | -------------------------------------------------------------------------------- /AnroidHttpDemo/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | My Application 3 | 4 | -------------------------------------------------------------------------------- /Volley/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuningjack/AndroidNet/HEAD/Volley/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /Volley/res/drawable-ldpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuningjack/AndroidNet/HEAD/Volley/res/drawable-ldpi/ic_launcher.png -------------------------------------------------------------------------------- /Volley/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuningjack/AndroidNet/HEAD/Volley/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /Volley/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuningjack/AndroidNet/HEAD/Volley/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /VolleyDemo/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuningjack/AndroidNet/HEAD/VolleyDemo/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /VolleyDemo/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuningjack/AndroidNet/HEAD/VolleyDemo/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /VolleyDemo/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuningjack/AndroidNet/HEAD/VolleyDemo/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /VolleyDemo/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuningjack/AndroidNet/HEAD/VolleyDemo/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /OkHttpDemo/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuningjack/AndroidNet/HEAD/OkHttpDemo/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /OkHttpDemo/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuningjack/AndroidNet/HEAD/OkHttpDemo/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /OkHttpDemo/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuningjack/AndroidNet/HEAD/OkHttpDemo/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /OkHttpDemo/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuningjack/AndroidNet/HEAD/OkHttpDemo/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /WebViewDemo/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuningjack/AndroidNet/HEAD/WebViewDemo/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /WebViewDemo/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuningjack/AndroidNet/HEAD/WebViewDemo/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /WebViewDemo/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuningjack/AndroidNet/HEAD/WebViewDemo/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /AnroidHttpDemo/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuningjack/AndroidNet/HEAD/AnroidHttpDemo/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /AnroidHttpDemo/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuningjack/AndroidNet/HEAD/AnroidHttpDemo/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /OkHttpDemo/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuningjack/AndroidNet/HEAD/OkHttpDemo/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /WebViewDemo/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuningjack/AndroidNet/HEAD/WebViewDemo/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /WebViewDemo/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuningjack/AndroidNet/HEAD/WebViewDemo/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /AnroidHttpDemo/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuningjack/AndroidNet/HEAD/AnroidHttpDemo/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /AnroidHttpDemo/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuningjack/AndroidNet/HEAD/AnroidHttpDemo/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /AnroidHttpDemo/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuningjack/AndroidNet/HEAD/AnroidHttpDemo/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /WebViewDemo/src/main/assets/error.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 404 error 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Volley/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 3 | org.eclipse.jdt.core.compiler.compliance=1.6 4 | org.eclipse.jdt.core.compiler.source=1.6 5 | -------------------------------------------------------------------------------- /VolleyDemo/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Volley_Demo 5 | Hello world! 6 | 7 | 8 | -------------------------------------------------------------------------------- /OkHttpDemo/src/main/asset/demo.html: -------------------------------------------------------------------------------- 1 | {"nature":"企业","icp":"京ICP证030173号","indexUrl":"www.baidu.com","sitename":"百度","domain":" baidu.com ","nowIcp":"京ICP证030173号-1","type":200,"search":"www.baidu.com","checkDate":"","name":"北京百度网讯科技有限公司"} -------------------------------------------------------------------------------- /VolleyDemo/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 3 | org.eclipse.jdt.core.compiler.compliance=1.6 4 | org.eclipse.jdt.core.compiler.source=1.6 5 | -------------------------------------------------------------------------------- /OkHttpDemo/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /Volley/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Volley 5 | Hello world! 6 | Settings 7 | 8 | -------------------------------------------------------------------------------- /AnroidHttpDemo/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /WebViewDemo/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Sep 25 23:17:30 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 | -------------------------------------------------------------------------------- /JavaSocketDemo/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | 3 | dependencies { 4 | compile fileTree(dir: 'libs', include: ['*.jar']) 5 | } 6 | 7 | tasks.withType(JavaCompile) { 8 | options.encoding = "UTF-8" 9 | } 10 | 11 | sourceCompatibility = "1.8" 12 | targetCompatibility = "1.8" 13 | -------------------------------------------------------------------------------- /Volley/res/menu/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 9 | -------------------------------------------------------------------------------- /Volley/res/values-v11/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 10 | 11 | -------------------------------------------------------------------------------- /VolleyDemo/src/com/example/volley_demo/Constants.java: -------------------------------------------------------------------------------- 1 | package com.example.volley_demo; 2 | 3 | /** 4 | * 完美世界 5 | * 6 | * @Author Jack 7 | * @Date 2017/9/25 9:43 8 | * @Copyright:wanmei.com Inc. All rights reserved. 9 | */ 10 | public class Constants { 11 | 12 | public static String URL = "http://apis.juhe.cn/mobile/get?phone=13811253688&key=cf365ff9aa9f1daddea8357b43267fd7"; 13 | 14 | } 15 | -------------------------------------------------------------------------------- /OkHttpDemo/src/main/java/com/example/jack/okhttpdemo/Constants.java: -------------------------------------------------------------------------------- 1 | package com.example.jack.okhttpdemo; 2 | 3 | /** 4 | * 完美世界 5 | * 6 | * @Author Jack 7 | * @Date 2017/9/25 9:42 8 | * @Copyright:wanmei.com Inc. All rights reserved. 9 | */ 10 | public class Constants { 11 | 12 | public static String CITY_URL = "http://v.juhe.cn/driverLicense/query.php?sf=北京&jszh=1111&dabh=2222&key=dbb7a8e95228cab32e4128f559d9bf85"; 13 | 14 | } 15 | -------------------------------------------------------------------------------- /OkHttpDemo/src/main/java/com/jack/okhttp/exception/OkHttpException.java: -------------------------------------------------------------------------------- 1 | package com.jack.okhttp.exception; 2 | 3 | /** 4 | * 自定义的网络请求异常 5 | * Created by xuning on 17/9/3. 6 | */ 7 | public class OkHttpException extends Exception{ 8 | 9 | private int mCode; 10 | private String mMsg; 11 | 12 | 13 | 14 | public OkHttpException(int mCode, String mMsg) { 15 | this.mCode = mCode; 16 | this.mMsg = mMsg; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Volley/res/values-v14/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 11 | 12 | -------------------------------------------------------------------------------- /VolleyDemo/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 9 | 10 | 11 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /OkHttpDemo/src/main/java/com/jack/okhttp/listener/DownloadResponseHandler.java: -------------------------------------------------------------------------------- 1 | package com.jack.okhttp.listener; 2 | 3 | import java.io.File; 4 | 5 | /** 6 | * Created by xuning on 17/9/14. 7 | */ 8 | 9 | public abstract class DownloadResponseHandler { 10 | 11 | public abstract void onFinish(File download_file); 12 | public abstract void onProgress(long currentBytes, long totalBytes); 13 | public abstract void onFailure(String error_msg); 14 | } 15 | -------------------------------------------------------------------------------- /WebViewDemo/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /AnroidHttpDemo/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /WebViewDemo/src/main/assets/showjs.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /OkHttpDemo/src/main/java/com/jack/okhttp/listener/DisposeDataListener.java: -------------------------------------------------------------------------------- 1 | package com.jack.okhttp.listener; 2 | 3 | /** 4 | * 获取到数据后的回调接口 5 | * Created by xuning on 17/9/3. 6 | */ 7 | public interface DisposeDataListener { 8 | 9 | /** 10 | * 请求成功 11 | * @param responseObj 12 | */ 13 | void onSuccess(T responseObj); 14 | 15 | 16 | /** 17 | * 请求失败 18 | * @param responseObj 19 | */ 20 | void onFail(Object responseObj); 21 | 22 | } 23 | -------------------------------------------------------------------------------- /WebViewDemo/src/main/res/layout/activity_second.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /WebViewDemo/src/main/java/com/example/david/webviewdemo/SecondActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.david.webviewdemo; 2 | 3 | import android.app.Activity; 4 | import android.os.Bundle; 5 | import android.support.annotation.Nullable; 6 | 7 | 8 | 9 | /** 10 | * Created by xuning on 17/8/3. 11 | */ 12 | public class SecondActivity extends Activity { 13 | 14 | @Override 15 | protected void onCreate(@Nullable Bundle savedInstanceState) { 16 | super.onCreate(savedInstanceState); 17 | setContentView(R.layout.activity_second); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /OkHttpDemo/src/main/java/com/jack/okhttp/listener/DisposeDataHandler.java: -------------------------------------------------------------------------------- 1 | package com.jack.okhttp.listener; 2 | 3 | /** 4 | * Created by xuning on 17/9/3. 5 | */ 6 | 7 | public class DisposeDataHandler { 8 | 9 | public DisposeDataListener mListener; 10 | public Class mClass; 11 | 12 | 13 | 14 | public DisposeDataHandler(DisposeDataListener mListener) { 15 | this.mListener = mListener; 16 | } 17 | 18 | public DisposeDataHandler(DisposeDataListener mListener, Class mClass) { 19 | this.mListener = mListener; 20 | this.mClass = mClass; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /WebViewDemo/src/main/java/com/example/david/webviewdemo/ShowJS.java: -------------------------------------------------------------------------------- 1 | package com.example.david.webviewdemo; 2 | 3 | import android.content.Context; 4 | import android.webkit.JavascriptInterface; 5 | import android.widget.Toast; 6 | 7 | 8 | 9 | /** 10 | * Created by xuning on 17/8/3. 11 | */ 12 | public class ShowJS { 13 | 14 | private Context mContext; 15 | 16 | public ShowJS(Context context){ 17 | mContext = context; 18 | } 19 | 20 | @JavascriptInterface 21 | public void callJs(){ 22 | Toast.makeText(mContext, "call js", Toast.LENGTH_LONG).show(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /VolleyDemo/src/com/example/volley_demo/MyApplication.java: -------------------------------------------------------------------------------- 1 | package com.example.volley_demo; 2 | 3 | import com.android.volley.RequestQueue; 4 | import com.android.volley.toolbox.Volley; 5 | 6 | import android.app.Application; 7 | 8 | 9 | 10 | 11 | /** 12 | * 自定义application 13 | */ 14 | public class MyApplication extends Application { 15 | 16 | public static RequestQueue queues; 17 | 18 | @Override 19 | public void onCreate() { 20 | super.onCreate(); 21 | queues = Volley.newRequestQueue(getApplicationContext()); 22 | } 23 | 24 | 25 | public static RequestQueue getHttpRequestQueue(){ 26 | return queues; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Volley/project.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must be checked in Version Control Systems. 5 | # 6 | # To customize properties used by the Ant build system edit 7 | # "ant.properties", and override values to adapt the script to your 8 | # project structure. 9 | # 10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): 11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt 12 | 13 | # Project target. 14 | target=android-16 15 | android.library=true 16 | -------------------------------------------------------------------------------- /VolleyDemo/project.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must be checked in Version Control Systems. 5 | # 6 | # To customize properties used by the Ant build system edit 7 | # "ant.properties", and override values to adapt the script to your 8 | # project structure. 9 | # 10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): 11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt 12 | 13 | # Project target. 14 | target=android-21 15 | android.library.reference.1=../Volley 16 | -------------------------------------------------------------------------------- /OkHttpDemo/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 24 5 | buildToolsVersion "25.0.0" 6 | 7 | defaultConfig { 8 | applicationId "com.example.jack.okhttpdemo" 9 | minSdkVersion 15 10 | targetSdkVersion 24 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | 15 | buildTypes { 16 | release { 17 | minifyEnabled false 18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 19 | } 20 | } 21 | } 22 | 23 | dependencies { 24 | compile fileTree(dir: 'libs', include: ['*.jar']) 25 | compile 'com.squareup.okhttp3:okhttp:3.0.1' 26 | compile 'com.zhy:okhttputils:2.6.2' 27 | } 28 | -------------------------------------------------------------------------------- /Volley/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 14 | 15 | 16 | 19 | 20 | -------------------------------------------------------------------------------- /VolleyDemo/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | dependencies { 4 | compile fileTree(dir: 'libs', include: '*.jar') 5 | compile project(':Volley') 6 | } 7 | 8 | android { 9 | compileSdkVersion 23 10 | buildToolsVersion "25.0.0" 11 | 12 | defaultConfig{ 13 | minSdkVersion 12 14 | targetSdkVersion 25 15 | versionCode 1 16 | versionName "1.0" 17 | } 18 | 19 | sourceSets { 20 | main { 21 | manifest.srcFile 'AndroidManifest.xml' 22 | java.srcDirs = ['src'] 23 | resources.srcDirs = ['src'] 24 | aidl.srcDirs = ['src'] 25 | renderscript.srcDirs = ['src'] 26 | res.srcDirs = ['res'] 27 | assets.srcDirs = ['assets'] 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /AnroidHttpDemo/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "25.0.0" 6 | 7 | defaultConfig { 8 | applicationId "com.example.david.myapplication" 9 | minSdkVersion 15 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | 21 | useLibrary("org.apache.http.legacy") 22 | } 23 | 24 | dependencies { 25 | compile fileTree(dir: 'libs', include: ['*.jar']) 26 | compile 'com.android.support:appcompat-v7:23.4.0' 27 | compile 'com.android.support.constraint:constraint-layout:1.0.1' 28 | } 29 | -------------------------------------------------------------------------------- /AnroidHttpDemo/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /Volley/proguard-project.txt: -------------------------------------------------------------------------------- 1 | # To enable ProGuard in your project, edit project.properties 2 | # to define the proguard.config property as described in that file. 3 | # 4 | # Add project specific ProGuard rules here. 5 | # By default, the flags in this file are appended to flags specified 6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt 7 | # You can edit the include path and order by changing the ProGuard 8 | # include property in project.properties. 9 | # 10 | # For more details, see 11 | # http://developer.android.com/guide/developing/tools/proguard.html 12 | 13 | # Add any project specific keep options here: 14 | 15 | # If your project uses WebView with JS, uncomment the following 16 | # and specify the fully qualified class name to the JavaScript interface 17 | # class: 18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 19 | # public *; 20 | #} 21 | -------------------------------------------------------------------------------- /VolleyDemo/proguard-project.txt: -------------------------------------------------------------------------------- 1 | # To enable ProGuard in your project, edit project.properties 2 | # to define the proguard.config property as described in that file. 3 | # 4 | # Add project specific ProGuard rules here. 5 | # By default, the flags in this file are appended to flags specified 6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt 7 | # You can edit the include path and order by changing the ProGuard 8 | # include property in project.properties. 9 | # 10 | # For more details, see 11 | # http://developer.android.com/guide/developing/tools/proguard.html 12 | 13 | # Add any project specific keep options here: 14 | 15 | # If your project uses WebView with JS, uncomment the following 16 | # and specify the fully qualified class name to the JavaScript interface 17 | # class: 18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 19 | # public *; 20 | #} 21 | -------------------------------------------------------------------------------- /Volley/src/com/android/volley/TimeoutError.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 The Android Open Source Project 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 | 17 | package com.android.volley; 18 | 19 | /** 20 | * Indicates that the connection or the socket timed out. 21 | */ 22 | @SuppressWarnings("serial") 23 | public class TimeoutError extends VolleyError { } 24 | -------------------------------------------------------------------------------- /OkHttpDemo/src/main/java/com/jack/okhttp/request/RequestParams.java: -------------------------------------------------------------------------------- 1 | package com.jack.okhttp.request; 2 | 3 | import java.util.Map; 4 | import java.util.concurrent.ConcurrentHashMap; 5 | 6 | /** 7 | * 请求参数 8 | * Created by xuning on 17/9/3. 9 | */ 10 | 11 | public class RequestParams { 12 | 13 | public ConcurrentHashMap urlParams = 14 | new ConcurrentHashMap(); 15 | public ConcurrentHashMap fileParams = 16 | new ConcurrentHashMap(); 17 | 18 | 19 | public RequestParams(){ 20 | this((Map)null); 21 | } 22 | 23 | public RequestParams(Map source){ 24 | if(source != null){ 25 | for(Map.Entry entry :source.entrySet()){ 26 | urlParams.put(entry.getKey(), entry.getValue()); 27 | } 28 | } 29 | 30 | } 31 | 32 | 33 | } 34 | -------------------------------------------------------------------------------- /OkHttpDemo/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /Volley/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 10 | 11 | 16 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /VolleyDemo/res/layout/activity_img.xml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 14 | 15 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /VolleyDemo/src/com/example/volley_demo/BitmapCache.java: -------------------------------------------------------------------------------- 1 | package com.example.volley_demo; 2 | 3 | import android.graphics.Bitmap; 4 | import android.util.LruCache; 5 | 6 | import com.android.volley.toolbox.ImageLoader; 7 | 8 | 9 | 10 | /** 11 | * 自定义图片缓存 12 | * Created by xuning on 17/8/13. 13 | */ 14 | public class BitmapCache implements ImageLoader.ImageCache { 15 | 16 | public LruCache mCache; 17 | public final int MAX = 10 * 1024; 18 | 19 | public BitmapCache(){ 20 | mCache = new LruCache(MAX){ 21 | @Override 22 | protected int sizeOf(String key, Bitmap value) { 23 | return value.getRowBytes() * value.getHeight(); 24 | } 25 | }; 26 | } 27 | 28 | @Override 29 | public Bitmap getBitmap(String url) { 30 | return mCache.get(url); 31 | } 32 | 33 | @Override 34 | public void putBitmap(String url, Bitmap bitmap) { 35 | mCache.put(url, bitmap); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /VolleyDemo/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 15 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /JavaSocketDemo/src/main/java/com/example/InetAddressDemo.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import java.net.InetAddress; 4 | import java.net.UnknownHostException; 5 | import java.util.Arrays; 6 | 7 | public class InetAddressDemo { 8 | 9 | public static void main(String[] args) throws UnknownHostException { 10 | 11 | //获取本机的InetAddress实例 12 | InetAddress address = InetAddress.getLocalHost(); 13 | System.out.println("计算机名:" + address.getHostName()); 14 | System.out.println("IP地址:" + address.getHostAddress()); 15 | System.out.println("获取字节数组形式的IP地址:" + 16 | Arrays.toString(address.getAddress())); 17 | System.out.println("计算机名和IP地址:" + address); 18 | 19 | //根据主机名获取InetAddress实例 20 | InetAddress address1 = InetAddress.getByName("JackdeMacBook-Pro.local"); 21 | System.out.println("计算机名和IP地址:" + address1); 22 | 23 | InetAddress address2 = InetAddress.getByName("192.168.1.104"); 24 | System.out.println("计算机名和IP地址:" + address2); 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /OkHttpDemo/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/xuning/Desktop/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 | -------------------------------------------------------------------------------- /WebViewDemo/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/xuning/Desktop/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 | -------------------------------------------------------------------------------- /AnroidHttpDemo/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/xuning/Desktop/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 | -------------------------------------------------------------------------------- /WebViewDemo/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /WebViewDemo/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "25.0.0" 6 | 7 | defaultConfig { 8 | applicationId "com.example.david.webviewdemo" 9 | minSdkVersion 15 10 | targetSdkVersion 23 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(dir: 'libs', include: ['*.jar']) 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:23.4.0' 31 | compile 'com.android.support.constraint:constraint-layout:1.0.1' 32 | testCompile 'junit:junit:4.12' 33 | } 34 | -------------------------------------------------------------------------------- /Volley/src/com/android/volley/NoConnectionError.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 The Android Open Source Project 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 | 17 | package com.android.volley; 18 | 19 | /** 20 | * Error indicating that no connection could be established when performing a Volley request. 21 | */ 22 | @SuppressWarnings("serial") 23 | public class NoConnectionError extends NetworkError { 24 | public NoConnectionError() { 25 | super(); 26 | } 27 | 28 | public NoConnectionError(Throwable reason) { 29 | super(reason); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /OkHttpDemo/src/main/java/com/jack/okhttp/response/CommonFileCallback.java: -------------------------------------------------------------------------------- 1 | package com.jack.okhttp.response; 2 | 3 | import android.os.Handler; 4 | import android.os.Looper; 5 | 6 | import com.jack.okhttp.listener.DisposeDataHandler; 7 | import com.jack.okhttp.listener.DisposeDataListener; 8 | 9 | import java.io.IOException; 10 | 11 | import okhttp3.Call; 12 | import okhttp3.Callback; 13 | import okhttp3.Response; 14 | 15 | 16 | 17 | /** 18 | * 文件操作的回调接口 19 | * Created by xuning on 17/9/9. 20 | */ 21 | public class CommonFileCallback implements Callback{ 22 | 23 | private DisposeDataListener mListener; 24 | private Class mClass; 25 | private Handler mHandler; 26 | 27 | 28 | public CommonFileCallback(DisposeDataHandler mDelieverHandler) { 29 | mHandler = new Handler(Looper.getMainLooper()); 30 | mListener = mDelieverHandler.mListener; 31 | mClass = mDelieverHandler.mClass; 32 | } 33 | 34 | @Override 35 | public void onFailure(Call call, IOException e) { 36 | 37 | } 38 | 39 | @Override 40 | public void onResponse(Call call, Response response) throws IOException { 41 | 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /JavaSocketDemo/src/main/java/com/example/UrlDemo.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import java.net.MalformedURLException; 4 | import java.net.URL; 5 | 6 | 7 | 8 | /** 9 | * URL常用方法 10 | * Created by xuning on 17/6/6. 11 | */ 12 | public class UrlDemo { 13 | 14 | public static void main(String[] args){ 15 | 16 | try { 17 | //创建URL实例 18 | URL url = new URL("http://www.baidu.com"); 19 | //?后面表示参数,#后面表示锚点 20 | URL baiUrl = new URL(url, "/index.html?username=jack#test"); 21 | System.out.println("协议:" + baiUrl.getProtocol()); 22 | System.out.println("主机:" + baiUrl.getHost()); 23 | //如果未指定端口号,根据协议使用默认的端口,未设置返回-1 24 | System.out.println("端口:" + baiUrl.getPort()); 25 | System.out.println("文件路径:" + baiUrl.getPath()); 26 | System.out.println("文件的名称:" + baiUrl.getFile()); 27 | System.out.println("文件相对路径:" + baiUrl.getRef()); 28 | System.out.println("查询字符串:" + baiUrl.getQuery()); 29 | } catch (MalformedURLException e) { 30 | e.printStackTrace(); 31 | } 32 | 33 | 34 | } 35 | 36 | 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Volley/src/com/android/volley/Network.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 The Android Open Source Project 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 | 17 | package com.android.volley; 18 | 19 | /** 20 | * An interface for performing requests. 21 | */ 22 | public interface Network { 23 | /** 24 | * Performs the specified request. 25 | * @param request Request to process 26 | * @return A {@link NetworkResponse} with data and caching metadata; will never be null 27 | * @throws VolleyError on errors 28 | */ 29 | public NetworkResponse performRequest(Request request) throws VolleyError; 30 | } 31 | -------------------------------------------------------------------------------- /Volley/src/com/android/volley/ServerError.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 The Android Open Source Project 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 | 17 | package com.android.volley; 18 | 19 | import com.android.volley.NetworkResponse; 20 | import com.android.volley.VolleyError; 21 | 22 | /** 23 | * Indicates that the error responded with an error response. 24 | */ 25 | @SuppressWarnings("serial") 26 | public class ServerError extends VolleyError { 27 | public ServerError(NetworkResponse networkResponse) { 28 | super(networkResponse); 29 | } 30 | 31 | public ServerError() { 32 | super(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Volley/src/com/android/volley/ParseError.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 The Android Open Source Project 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 | 17 | package com.android.volley; 18 | 19 | import com.android.volley.NetworkResponse; 20 | import com.android.volley.VolleyError; 21 | 22 | /** 23 | * Indicates that the server's response could not be parsed. 24 | */ 25 | @SuppressWarnings("serial") 26 | public class ParseError extends VolleyError { 27 | public ParseError() { } 28 | 29 | public ParseError(NetworkResponse networkResponse) { 30 | super(networkResponse); 31 | } 32 | 33 | public ParseError(Throwable cause) { 34 | super(cause); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Volley/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'android-library' 2 | 3 | dependencies { 4 | compile fileTree(dir: 'libs', include: '*.jar') 5 | } 6 | 7 | android { 8 | compileSdkVersion 23 9 | buildToolsVersion "25.0.0" 10 | useLibrary 'org.apache.http.legacy' 11 | 12 | sourceSets { 13 | main { 14 | manifest.srcFile 'AndroidManifest.xml' 15 | java.srcDirs = ['src'] 16 | resources.srcDirs = ['src'] 17 | aidl.srcDirs = ['src'] 18 | renderscript.srcDirs = ['src'] 19 | res.srcDirs = ['res'] 20 | assets.srcDirs = ['assets'] 21 | } 22 | 23 | // Move the tests to tests/java, tests/res, etc... 24 | instrumentTest.setRoot('tests') 25 | 26 | // Move the build types to build-types/ 27 | // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ... 28 | // This moves them out of them default location under src//... which would 29 | // conflict with src/ being used by the main source set. 30 | // Adding new build types or product flavors should be accompanied 31 | // by a similar customization. 32 | debug.setRoot('build-types/debug') 33 | release.setRoot('build-types/release') 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Volley/src/com/android/volley/toolbox/Authenticator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 The Android Open Source Project 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 | 17 | package com.android.volley.toolbox; 18 | 19 | import com.android.volley.AuthFailureError; 20 | 21 | /** 22 | * An interface for interacting with auth tokens. 23 | */ 24 | public interface Authenticator { 25 | /** 26 | * Synchronously retrieves an auth token. 27 | * 28 | * @throws AuthFailureError If authentication did not succeed 29 | */ 30 | public String getAuthToken() throws AuthFailureError; 31 | 32 | /** 33 | * Invalidates the provided auth token. 34 | */ 35 | public void invalidateAuthToken(String authToken); 36 | } 37 | -------------------------------------------------------------------------------- /Volley/src/com/android/volley/NetworkError.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 The Android Open Source Project 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 | 17 | package com.android.volley; 18 | 19 | import com.android.volley.NetworkResponse; 20 | import com.android.volley.VolleyError; 21 | 22 | /** 23 | * Indicates that there was a network error when performing a Volley request. 24 | */ 25 | @SuppressWarnings("serial") 26 | public class NetworkError extends VolleyError { 27 | public NetworkError() { 28 | super(); 29 | } 30 | 31 | public NetworkError(Throwable cause) { 32 | super(cause); 33 | } 34 | 35 | public NetworkError(NetworkResponse networkResponse) { 36 | super(networkResponse); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /OkHttpDemo/src/main/java/com/jack/okhttp/utils/JsonHelper.java: -------------------------------------------------------------------------------- 1 | package com.jack.okhttp.utils; 2 | 3 | import com.google.gson.Gson; 4 | import com.google.gson.reflect.TypeToken; 5 | import java.lang.reflect.Type; 6 | import java.util.List; 7 | 8 | 9 | 10 | /** 11 | * Json解析工具类 12 | */ 13 | public class JsonHelper { 14 | 15 | private static Gson gson = new Gson(); 16 | 17 | /** 18 | * 解析单个实体 19 | * @param jsonData 20 | * @param type 21 | * @param 22 | * @return 23 | */ 24 | @SuppressWarnings("unchecked") 25 | public static T getResult(String jsonData, TypeToken type) { 26 | return (T) gson.fromJson(jsonData, type.getType()); 27 | } 28 | 29 | /** 30 | * 解析单个实体 31 | * @param jsonData 32 | * @param type 33 | * @param 34 | * @return 35 | */ 36 | public static T getResult(String jsonData, Class type) { 37 | return gson.fromJson(jsonData, type); 38 | } 39 | 40 | /** 41 | * 获得json字符串对应的数据List 42 | * @param jsonData json数据源 43 | * @param type 实体类的类型 如Drafts.class 44 | * @param 实体类的类型 45 | * @return 46 | */ 47 | public static List getResultList(String jsonData, T type){ 48 | Type listType = new TypeToken>(){}.getType(); 49 | return gson.fromJson(jsonData, listType); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Volley/src/com/android/volley/ResponseDelivery.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 The Android Open Source Project 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 | 17 | package com.android.volley; 18 | 19 | public interface ResponseDelivery { 20 | /** 21 | * Parses a response from the network or cache and delivers it. 22 | */ 23 | public void postResponse(Request request, Response response); 24 | 25 | /** 26 | * Parses a response from the network or cache and delivers it. The provided 27 | * Runnable will be executed after delivery. 28 | */ 29 | public void postResponse(Request request, Response response, Runnable runnable); 30 | 31 | /** 32 | * Posts an error for the given request. 33 | */ 34 | public void postError(Request request, VolleyError error); 35 | } 36 | -------------------------------------------------------------------------------- /Volley/src/com/android/volley/toolbox/NoCache.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 The Android Open Source Project 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 | 17 | package com.android.volley.toolbox; 18 | 19 | import com.android.volley.Cache; 20 | 21 | /** 22 | * A cache that doesn't. 23 | */ 24 | public class NoCache implements Cache { 25 | @Override 26 | public void clear() { 27 | } 28 | 29 | @Override 30 | public Entry get(String key) { 31 | return null; 32 | } 33 | 34 | @Override 35 | public void put(String key, Entry entry) { 36 | } 37 | 38 | @Override 39 | public void invalidate(String key, boolean fullExpire) { 40 | } 41 | 42 | @Override 43 | public void remove(String key) { 44 | } 45 | 46 | @Override 47 | public void initialize() { 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /JavaSocketDemo/src/main/java/udp_socket_server_client/UDPClient.java: -------------------------------------------------------------------------------- 1 | package udp_socket_server_client; 2 | 3 | import java.io.IOException; 4 | import java.net.DatagramPacket; 5 | import java.net.DatagramSocket; 6 | import java.net.InetAddress; 7 | 8 | 9 | 10 | /** 11 | * UDP客户端 12 | */ 13 | public class UDPClient { 14 | 15 | public static void main(String[] args) throws IOException { 16 | 17 | //一、给服务器发送数据 18 | //step1 定义服务器的地址、端口号、数据 19 | InetAddress address = InetAddress.getByName("localhost"); 20 | int port = 8888; 21 | byte[] data = "用户名:Jack 密码:123".getBytes(); 22 | 23 | //step2 创建数据报,包括发送的相关信息 24 | DatagramPacket packet = new DatagramPacket(data, data.length, 25 | address, port); 26 | 27 | //step3 创建DatagramSocket 28 | DatagramSocket socket = new DatagramSocket(); 29 | 30 | //step4 向服务器发送数据 31 | socket.send(packet); 32 | 33 | 34 | 35 | 36 | //二、接受服务器的返回数据 37 | //step1 创建数据报,接受响应的信息 38 | byte[] dataBack = new byte[1024]; 39 | DatagramPacket packetBack = new DatagramPacket(dataBack, dataBack.length); 40 | //step2 接受服务器的响应信息 41 | socket.receive(packetBack); 42 | //step3 打印服务器的响应信息。 注意:要加入length,否则有空的乱码 43 | String infoBack = new String(dataBack, 0 , packetBack.getLength()); 44 | System.out.println("服务器响应:" + infoBack); 45 | //step4 关闭资源 46 | socket.close(); 47 | } 48 | 49 | 50 | } 51 | -------------------------------------------------------------------------------- /JavaSocketDemo/src/main/java/udp_socket_server_client/UDPServer.java: -------------------------------------------------------------------------------- 1 | package udp_socket_server_client; 2 | 3 | import java.io.IOException; 4 | import java.net.DatagramPacket; 5 | import java.net.DatagramSocket; 6 | import java.net.InetAddress; 7 | 8 | 9 | 10 | /** 11 | * 基于UDP的用户登陆 12 | */ 13 | public class UDPServer { 14 | 15 | public static void main(String[] args) throws IOException { 16 | 17 | //服务器接受客户端发送数据 18 | 19 | //Step1 创建服务器端的DatagramSocket,指定端口 20 | DatagramSocket socket = new DatagramSocket(8888); 21 | 22 | //Step2 创建数据报,用于接收客户端发送的数据 23 | byte[] data = new byte[1024]; 24 | DatagramPacket packet = new DatagramPacket(data, data.length); 25 | 26 | //Step3 接受客户端发送的数据 27 | socket.receive(packet); //此方法在接受数据报之前一直阻塞 28 | 29 | //Step4 读取数据 30 | String info = new String(data, 0, packet.getLength()); 31 | 32 | System.out.println("我是服务器,客户端说:" + info); 33 | 34 | 35 | 36 | //服务器响应客户端 37 | //step1 客户端的IP、端口、数据 38 | InetAddress clientAddress = packet.getAddress(); 39 | int port = packet.getPort(); 40 | byte[] bytesBack = "欢迎您!!".getBytes(); 41 | //step2 创建数据报,包括响应的信息 42 | DatagramPacket packetBack = new DatagramPacket(bytesBack, 43 | bytesBack.length, clientAddress, port); 44 | //step3 响应客户端 45 | socket.send(packetBack); 46 | //step4 关闭相应的信息 47 | socket.close(); 48 | 49 | } 50 | 51 | 52 | 53 | } 54 | -------------------------------------------------------------------------------- /Volley/src/com/android/volley/RetryPolicy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 The Android Open Source Project 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 | 17 | package com.android.volley; 18 | 19 | /** 20 | * Retry policy for a request. 21 | */ 22 | public interface RetryPolicy { 23 | 24 | /** 25 | * Returns the current timeout (used for logging). 26 | */ 27 | public int getCurrentTimeout(); 28 | 29 | /** 30 | * Returns the current retry count (used for logging). 31 | */ 32 | public int getCurrentRetryCount(); 33 | 34 | /** 35 | * Prepares for the next retry by applying a backoff to the timeout. 36 | * @param error The error code of the last attempt. 37 | * @throws VolleyError In the event that the retry could not be performed (for example if we 38 | * ran out of attempts), the passed in error is thrown. 39 | */ 40 | public void retry(VolleyError error) throws VolleyError; 41 | } 42 | -------------------------------------------------------------------------------- /JavaSocketDemo/src/main/java/http_socket_server_client/multi/MultiServer.java: -------------------------------------------------------------------------------- 1 | package http_socket_server_client.multi; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.net.InetAddress; 6 | import java.net.ServerSocket; 7 | import java.net.Socket; 8 | 9 | /** 10 | * 基于TCP协议的Socket通信,实现用户登陆的Server 11 | */ 12 | public class MultiServer { 13 | 14 | 15 | public static void main(String[] args){ 16 | 17 | ServerSocket serverSocket; 18 | InputStream is; 19 | Socket socket; 20 | /**记录客户端的连接数量*/ 21 | int count = 0; 22 | 23 | try { 24 | //step1 创建一个服务器端的Socket,指定端口号并监听 25 | serverSocket = new ServerSocket(8888); //指定1023以后的端口 26 | //step2 调用accept()方法开始监听,等待客户端的连接 27 | System.out.println("---服务器即将启动,等待客户端的连接---"); 28 | 29 | //循环监听客户端的连接 30 | while(true){ 31 | socket = serverSocket.accept(); 32 | //创建线程与client通信 33 | ServerThread serverThread = new ServerThread(socket); 34 | //启动线程 35 | serverThread.start(); 36 | 37 | count++; //统计客户端的数量 38 | System.out.println("客户端的连接数量为:" + count); 39 | InetAddress address = socket.getInetAddress(); 40 | System.out.println("当前客户端的ip:" + address.getHostAddress()); 41 | } 42 | 43 | } catch (IOException e) { 44 | e.printStackTrace(); 45 | } 46 | 47 | 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /VolleyDemo/src/com/example/volley_demo/VolleyListener.java: -------------------------------------------------------------------------------- 1 | package com.example.volley_demo; 2 | 3 | import android.content.Context; 4 | 5 | import com.android.volley.Response; 6 | import com.android.volley.VolleyError; 7 | 8 | 9 | /** 10 | * 自定义的请求监听 11 | * Created by xuning on 17/5/31. 12 | */ 13 | public abstract class VolleyListener { 14 | 15 | private Context mContext; 16 | public static Response.Listener mListener; 17 | public static Response.ErrorListener mErrorListener; 18 | 19 | public VolleyListener(Context context, 20 | Response.Listener listener, 21 | Response.ErrorListener errorListener){ 22 | mContext = context; 23 | mListener = listener; 24 | mErrorListener = errorListener; 25 | } 26 | 27 | public Response.Listener loadingListener(){ 28 | mListener = new Response.Listener() { 29 | @Override 30 | public void onResponse(String response) { 31 | onMySuccess(response); 32 | } 33 | }; 34 | return mListener; 35 | } 36 | 37 | public Response.ErrorListener errorListener(){ 38 | mErrorListener = new Response.ErrorListener() { 39 | @Override 40 | public void onErrorResponse(VolleyError error) { 41 | onMyError(error); 42 | } 43 | }; 44 | return mErrorListener; 45 | } 46 | 47 | public abstract void onMySuccess(String response); 48 | public abstract void onMyError(VolleyError error); 49 | 50 | } 51 | -------------------------------------------------------------------------------- /OkHttpDemo/src/main/java/com/example/jack/okhttpdemo/BaiduWeight.java: -------------------------------------------------------------------------------- 1 | package com.example.jack.okhttpdemo; 2 | 3 | import com.google.gson.annotations.Expose; 4 | import com.google.gson.annotations.SerializedName; 5 | 6 | /** 7 | * 网络请求实体类 8 | * Created by xuning on 17/9/9. 9 | */ 10 | public class BaiduWeight { 11 | 12 | 13 | @Expose 14 | @SerializedName("Weight") 15 | private String mWeight; 16 | 17 | @Expose 18 | @SerializedName("From") 19 | private String mFrom; 20 | 21 | @Expose 22 | @SerializedName("To") 23 | private int mTo; 24 | 25 | public BaiduWeight() { 26 | super(); 27 | } 28 | 29 | public BaiduWeight(String mWeight, String mFrom, int mTo) { 30 | this.mWeight = mWeight; 31 | this.mFrom = mFrom; 32 | this.mTo = mTo; 33 | } 34 | 35 | @Override 36 | public String toString() { 37 | return "BaiduWeight{" + 38 | "mWeight='" + mWeight + '\'' + 39 | ", mFrom='" + mFrom + '\'' + 40 | ", mTo=" + mTo + 41 | '}'; 42 | } 43 | 44 | public String getmWeight() { 45 | return mWeight; 46 | } 47 | 48 | public void setmWeight(String mWeight) { 49 | this.mWeight = mWeight; 50 | } 51 | 52 | public String getmFrom() { 53 | return mFrom; 54 | } 55 | 56 | public void setmFrom(String mFrom) { 57 | this.mFrom = mFrom; 58 | } 59 | 60 | public int getmTo() { 61 | return mTo; 62 | } 63 | 64 | public void setmTo(int mTo) { 65 | this.mTo = mTo; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Volley/src/com/android/volley/VolleyError.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 The Android Open Source Project 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 | 17 | package com.android.volley; 18 | 19 | /** 20 | * Exception style class encapsulating Volley errors 21 | */ 22 | @SuppressWarnings("serial") 23 | public class VolleyError extends Exception { 24 | public final NetworkResponse networkResponse; 25 | 26 | public VolleyError() { 27 | networkResponse = null; 28 | } 29 | 30 | public VolleyError(NetworkResponse response) { 31 | networkResponse = response; 32 | } 33 | 34 | public VolleyError(String exceptionMessage) { 35 | super(exceptionMessage); 36 | networkResponse = null; 37 | } 38 | 39 | public VolleyError(String exceptionMessage, Throwable reason) { 40 | super(exceptionMessage, reason); 41 | networkResponse = null; 42 | } 43 | 44 | public VolleyError(Throwable cause) { 45 | super(cause); 46 | networkResponse = null; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Volley/src/com/android/volley/NetworkResponse.java: -------------------------------------------------------------------------------- 1 | 2 | package com.android.volley; 3 | 4 | import org.apache.http.HttpStatus; 5 | 6 | import java.util.Collections; 7 | import java.util.Map; 8 | 9 | /** 10 | * Data and headers returned from {@link Network#performRequest(Request)}. 11 | */ 12 | public class NetworkResponse { 13 | /** 14 | * Creates a new network response. 15 | * @param statusCode the HTTP status code 16 | * @param data Response body 17 | * @param headers Headers returned with this response, or null for none 18 | * @param notModified True if the server returned a 304 and the data was already in cache 19 | */ 20 | public NetworkResponse(int statusCode, byte[] data, Map headers, 21 | boolean notModified) { 22 | this.statusCode = statusCode; 23 | this.data = data; 24 | this.headers = headers; 25 | this.notModified = notModified; 26 | } 27 | 28 | public NetworkResponse(byte[] data) { 29 | this(HttpStatus.SC_OK, data, Collections.emptyMap(), false); 30 | } 31 | 32 | public NetworkResponse(byte[] data, Map headers) { 33 | this(HttpStatus.SC_OK, data, headers, false); 34 | } 35 | 36 | /** The HTTP status code. */ 37 | public final int statusCode; 38 | 39 | /** Raw data from this response. */ 40 | public final byte[] data; 41 | 42 | /** Response headers. */ 43 | public final Map headers; 44 | 45 | /** True if the server returned a 304 (Not Modified). */ 46 | public final boolean notModified; 47 | } -------------------------------------------------------------------------------- /JavaSocketDemo/src/main/java/http_socket_server_client/single/Client.java: -------------------------------------------------------------------------------- 1 | package http_socket_server_client.single; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStream; 6 | import java.io.InputStreamReader; 7 | import java.io.OutputStream; 8 | import java.io.PrintWriter; 9 | import java.net.Socket; 10 | 11 | 12 | 13 | /** 14 | * 基于TCP协议的Socket通信,实现用户登陆的Client 15 | */ 16 | public class Client { 17 | 18 | 19 | public static void main(String[] args){ 20 | 21 | try { 22 | //step1 创建客户端的Socket,指定服务器地址和端口 23 | Socket socket = new Socket("localhost", 8888); 24 | System.out.println("---客户端连接上服务器---"); 25 | 26 | //step2 获取输出流,想服务器发送信息 27 | OutputStream os = socket.getOutputStream(); //字节流 28 | PrintWriter pw = new PrintWriter(os); //将输出流包装为打印流 29 | pw.write("用户名:Jack 密码:Jack"); //向Server发送数据 30 | pw.flush(); 31 | socket.shutdownOutput(); 32 | 33 | //step3 获取输入流,读取服务器的响应 34 | InputStream is = socket.getInputStream(); 35 | InputStreamReader isr = new InputStreamReader(is); //字符输入流 36 | BufferedReader br = new BufferedReader(isr); //缓冲输入流 37 | String info; 38 | while((info = br.readLine()) != null){ 39 | System.out.println("我是客户端,服务器说:" + info); 40 | } 41 | 42 | //step4 关闭资源 43 | br.close(); 44 | isr.close(); 45 | is.close(); 46 | pw.close(); 47 | os.close(); 48 | 49 | } catch (IOException e) { 50 | e.printStackTrace(); 51 | } 52 | } 53 | 54 | 55 | } 56 | -------------------------------------------------------------------------------- /OkHttpDemo/src/main/java/com/example/jack/okhttpdemo/Result.java: -------------------------------------------------------------------------------- 1 | package com.example.jack.okhttpdemo; 2 | 3 | import com.google.gson.annotations.Expose; 4 | import com.google.gson.annotations.SerializedName; 5 | 6 | /** 7 | * 网络请求实体类 8 | * Created by xuning on 17/9/9. 9 | */ 10 | public class Result { 11 | 12 | 13 | @Expose 14 | @SerializedName("reason") 15 | private String reason; 16 | 17 | @Expose 18 | @SerializedName("result") 19 | private T result; 20 | 21 | @Expose 22 | @SerializedName("error_code") 23 | private int errorCode; 24 | 25 | @Expose 26 | @SerializedName("resultcode") 27 | private String resultCode; 28 | 29 | 30 | public Result() { 31 | super(); 32 | } 33 | 34 | public Result(String reason, T result, int errorCode) { 35 | this.reason = reason; 36 | this.result = result; 37 | this.errorCode = errorCode; 38 | } 39 | 40 | public String getReason() { 41 | return reason; 42 | } 43 | 44 | public void setReason(String reason) { 45 | this.reason = reason; 46 | } 47 | 48 | public T getResult() { 49 | return result; 50 | } 51 | 52 | public void setResult(T result) { 53 | this.result = result; 54 | } 55 | 56 | public int getErrorCode() { 57 | return errorCode; 58 | } 59 | 60 | public void setErrorCode(int errorCode) { 61 | this.errorCode = errorCode; 62 | } 63 | 64 | public Result(String reason, T result, int errorCode, String resultCode) { 65 | this.reason = reason; 66 | this.result = result; 67 | this.errorCode = errorCode; 68 | this.resultCode = resultCode; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /JavaSocketDemo/src/main/java/http_socket_server_client/multi/MultiClient.java: -------------------------------------------------------------------------------- 1 | package http_socket_server_client.multi; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStream; 6 | import java.io.InputStreamReader; 7 | import java.io.OutputStream; 8 | import java.io.PrintWriter; 9 | import java.net.Socket; 10 | 11 | 12 | /** 13 | * 基于TCP协议的Socket通信,实现用户登陆的Client 14 | */ 15 | public class MultiClient { 16 | 17 | 18 | public static void main(String[] args) { 19 | 20 | try { 21 | //step1 创建客户端的Socket,指定服务器地址和端口 22 | Socket socket = new Socket("localhost", 8888); 23 | System.out.println("---客户端连接上服务器---"); 24 | 25 | //step2 获取输出流,想服务器发送信息 26 | OutputStream os = socket.getOutputStream(); //字节流 27 | PrintWriter pw = new PrintWriter(os); //将输出流包装为打印流 28 | pw.write("用户名:Jack 密码:Jack"); //todo update 向Server发送数据 29 | pw.flush(); 30 | socket.shutdownOutput(); 31 | 32 | //step3 获取输入流,读取服务器的响应 33 | InputStream is = socket.getInputStream(); 34 | InputStreamReader isr = new InputStreamReader(is); //字符输入流 35 | BufferedReader br = new BufferedReader(isr); //缓冲输入流 36 | String info; 37 | while ((info = br.readLine()) != null) { 38 | System.out.println("我是客户端,服务器说:" + info); 39 | } 40 | 41 | //step4 关闭资源 42 | br.close(); 43 | isr.close(); 44 | is.close(); 45 | pw.close(); 46 | os.close(); 47 | 48 | } catch (IOException e) { 49 | e.printStackTrace(); 50 | } 51 | } 52 | 53 | 54 | } 55 | -------------------------------------------------------------------------------- /Volley/src/com/android/volley/toolbox/HttpStack.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 The Android Open Source Project 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 | 17 | package com.android.volley.toolbox; 18 | 19 | import com.android.volley.AuthFailureError; 20 | import com.android.volley.Request; 21 | 22 | import org.apache.http.HttpResponse; 23 | 24 | import java.io.IOException; 25 | import java.util.Map; 26 | 27 | /** 28 | * An HTTP stack abstraction. 29 | */ 30 | public interface HttpStack { 31 | /** 32 | * Performs an HTTP request with the given parameters. 33 | * 34 | *

A GET request is sent if request.getPostBody() == null. A POST request is sent otherwise, 35 | * and the Content-Type header is set to request.getPostBodyContentType().

36 | * 37 | * @param request the request to perform 38 | * @param additionalHeaders additional headers to be sent together with 39 | * {@link Request#getHeaders()} 40 | * @return the HTTP response 41 | */ 42 | public HttpResponse performRequest(Request request, Map additionalHeaders) 43 | throws IOException, AuthFailureError; 44 | 45 | } 46 | -------------------------------------------------------------------------------- /AnroidHttpDemo/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 11 | 12 | 18 | 19 | 25 | 26 | 32 | 33 | 39 | 40 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /VolleyDemo/src/com/example/volley_demo/CustomVolleyRequest.java: -------------------------------------------------------------------------------- 1 | package com.example.volley_demo; 2 | 3 | 4 | import android.content.Context; 5 | 6 | import com.android.volley.AuthFailureError; 7 | import com.android.volley.Request; 8 | import com.android.volley.toolbox.StringRequest; 9 | 10 | import java.util.Map; 11 | 12 | 13 | 14 | 15 | /** 16 | * 自己封装get和post请求 17 | */ 18 | public class CustomVolleyRequest { 19 | 20 | public static StringRequest mStringRequest; 21 | 22 | 23 | /** 24 | * 执行get请求 25 | * @param url 26 | * @param tag 27 | * @param listener 28 | */ 29 | public static void requestGet(String url, String tag, VolleyListener listener){ 30 | MyApplication.getHttpRequestQueue().cancelAll(tag); 31 | mStringRequest = new StringRequest(Request.Method.GET, url, 32 | listener.loadingListener(), listener.errorListener()); 33 | mStringRequest.setTag(tag); 34 | MyApplication.getHttpRequestQueue().add(mStringRequest); 35 | MyApplication.getHttpRequestQueue().start(); 36 | } 37 | 38 | 39 | /** 40 | * 执行post请求 41 | * @param url 42 | * @param tag 43 | * @param params 44 | * @param listener 45 | */ 46 | public static void requestPost(String url, String tag, 47 | final Map params, VolleyListener listener){ 48 | MyApplication.getHttpRequestQueue().cancelAll(tag); 49 | mStringRequest = new StringRequest(Request.Method.POST, url, 50 | listener.loadingListener(), listener.errorListener()){ 51 | @Override 52 | protected Map getParams() throws AuthFailureError { 53 | return params; 54 | } 55 | }; 56 | mStringRequest.setTag(tag); 57 | MyApplication.getHttpRequestQueue().add(mStringRequest); 58 | MyApplication.getHttpRequestQueue().start(); 59 | 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /WebViewDemo/src/main/res/layout/activity_web_view.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 13 | 14 | 21 | 22 | 29 | 30 | 37 | 38 | 45 | 46 | 47 | 48 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /JavaSocketDemo/src/main/java/http_socket_server_client/single/Server.java: -------------------------------------------------------------------------------- 1 | package http_socket_server_client.single; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStream; 6 | import java.io.InputStreamReader; 7 | import java.io.OutputStream; 8 | import java.io.PrintWriter; 9 | import java.net.ServerSocket; 10 | import java.net.Socket; 11 | 12 | /** 13 | * 基于TCP协议的Socket通信,实现用户登陆的Server 14 | */ 15 | public class Server { 16 | 17 | 18 | public static void main(String[] args){ 19 | 20 | ServerSocket serverSocket; 21 | InputStream is; 22 | Socket socket; 23 | 24 | try { 25 | //step1 创建一个服务器端的Socket,指定端口号并监听 26 | serverSocket = new ServerSocket(8888); //指定1023以后的端口 27 | //step2 调用accept()方法开始监听,等待客户端的连接 28 | System.out.println("---服务器即将启动,等待客户端的连接---"); 29 | socket = serverSocket.accept(); 30 | //step3 获取输入流,读取客户端所发送的信息 31 | is = socket.getInputStream(); 32 | //将字节流包装为字符流 33 | InputStreamReader isr = new InputStreamReader(is); 34 | //为输入流添加缓冲 35 | BufferedReader br = new BufferedReader(isr); 36 | 37 | String info; 38 | while((info = br.readLine()) != null){ //循环读取客户端的信息 39 | System.out.println("我是服务器,客户端说:" + info); 40 | } 41 | socket.shutdownInput(); 42 | 43 | //step4 获取输出流,用来响应客户端 44 | OutputStream os = socket.getOutputStream(); 45 | //包装为打印流 46 | PrintWriter pw = new PrintWriter(os); 47 | pw.write("欢迎您"); 48 | pw.flush(); //刷新缓冲区 49 | 50 | //step5 关闭资源 51 | pw.close(); 52 | os.close(); 53 | br.close(); 54 | isr.close(); 55 | is.close(); 56 | 57 | } catch (IOException e) { 58 | e.printStackTrace(); 59 | } 60 | 61 | 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /Volley/src/com/android/volley/AuthFailureError.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 The Android Open Source Project 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 | 17 | package com.android.volley; 18 | 19 | import android.content.Intent; 20 | 21 | import com.android.volley.NetworkResponse; 22 | import com.android.volley.VolleyError; 23 | 24 | /** 25 | * Error indicating that there was an authentication failure when performing a Request. 26 | */ 27 | @SuppressWarnings("serial") 28 | public class AuthFailureError extends VolleyError { 29 | /** An intent that can be used to resolve this exception. (Brings up the password dialog.) */ 30 | private Intent mResolutionIntent; 31 | 32 | public AuthFailureError() { } 33 | 34 | public AuthFailureError(Intent intent) { 35 | mResolutionIntent = intent; 36 | } 37 | 38 | public AuthFailureError(NetworkResponse response) { 39 | super(response); 40 | } 41 | 42 | public AuthFailureError(String message) { 43 | super(message); 44 | } 45 | 46 | public AuthFailureError(String message, Exception reason) { 47 | super(message, reason); 48 | } 49 | 50 | public Intent getResolutionIntent() { 51 | return mResolutionIntent; 52 | } 53 | 54 | @Override 55 | public String getMessage() { 56 | if (mResolutionIntent != null) { 57 | return "User needs to (re)enter credentials."; 58 | } 59 | return super.getMessage(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /JavaSocketDemo/src/main/java/com/example/UrlReadDemo.java: -------------------------------------------------------------------------------- 1 | package com.example; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.ByteArrayOutputStream; 5 | import java.io.IOException; 6 | import java.io.InputStream; 7 | import java.io.InputStreamReader; 8 | import java.net.MalformedURLException; 9 | import java.net.URL; 10 | 11 | 12 | 13 | 14 | /** 15 | * 使用URL读取页面内容 16 | * Created by xuning on 17/6/6. 17 | */ 18 | public class UrlReadDemo { 19 | 20 | public static void main(String[] args) throws IOException { 21 | 22 | InputStream is = null; 23 | ByteArrayOutputStream baos = null; 24 | InputStreamReader isr = null; 25 | BufferedReader bis = null; 26 | 27 | try { 28 | //创建URL实例 29 | URL url = new URL("http://www.baidu.com"); 30 | //获取字节的输入流 31 | is = url.openStream(); 32 | /* 33 | baos = new ByteArrayOutputStream(); 34 | int length = 0; 35 | byte[] bytes = new byte[1024]; 36 | while((length = is.read(bytes)) != -1){ 37 | baos.write(bytes); 38 | } 39 | System.out.println("页面内容:" + baos.toString());*/ 40 | 41 | 42 | //将自己饿输入流转换成为字符输入流 43 | isr = new InputStreamReader(is, "utf-8"); 44 | //为字符输入流添加缓冲 45 | bis = new BufferedReader(isr); 46 | String data = bis.readLine(); 47 | System.out.println("页面内容:\n"); 48 | while(data != null){ 49 | System.out.println(data); 50 | data = bis.readLine(); //读取下一行 51 | } 52 | } catch (MalformedURLException e) { 53 | e.printStackTrace(); 54 | } catch (IOException e) { 55 | e.printStackTrace(); 56 | } finally { 57 | if(is != null) { 58 | is.close(); 59 | } 60 | if(baos != null){ 61 | baos.close(); 62 | } 63 | if(isr != null){ 64 | is.close(); 65 | } 66 | if(bis != null){ 67 | bis.close(); 68 | } 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /OkHttpDemo/src/main/java/com/jack/okhttp/body/ResponseProgressBody.java: -------------------------------------------------------------------------------- 1 | package com.jack.okhttp.body; 2 | 3 | /** 4 | * Created by xuning on 17/9/14. 5 | */ 6 | 7 | import com.jack.okhttp.listener.DownloadResponseHandler; 8 | 9 | import java.io.IOException; 10 | 11 | import okhttp3.MediaType; 12 | import okhttp3.ResponseBody; 13 | import okio.Buffer; 14 | import okio.BufferedSource; 15 | import okio.ForwardingSource; 16 | import okio.Okio; 17 | import okio.Source; 18 | 19 | /** 20 | * 重写responsebody 设置下载进度监听 21 | * Created by tsy on 16/8/16. 22 | */ 23 | public class ResponseProgressBody extends ResponseBody { 24 | 25 | private ResponseBody mResponseBody; 26 | private DownloadResponseHandler mDownloadResponseHandler; 27 | private BufferedSource bufferedSource; 28 | 29 | public ResponseProgressBody(ResponseBody responseBody, 30 | DownloadResponseHandler downloadResponseHandler) { 31 | this.mResponseBody = responseBody; 32 | this.mDownloadResponseHandler = downloadResponseHandler; 33 | } 34 | 35 | @Override 36 | public MediaType contentType() { 37 | return mResponseBody.contentType(); 38 | } 39 | 40 | @Override 41 | public long contentLength() { 42 | return mResponseBody.contentLength(); 43 | } 44 | 45 | @Override 46 | public BufferedSource source() { 47 | if (bufferedSource == null) { 48 | bufferedSource = Okio.buffer(source(mResponseBody.source())); 49 | } 50 | return bufferedSource; 51 | } 52 | 53 | private Source source(Source source) { 54 | 55 | return new ForwardingSource(source) { 56 | 57 | long totalBytesRead; 58 | 59 | @Override 60 | public long read(Buffer sink, long byteCount) throws IOException { 61 | long bytesRead = super.read(sink, byteCount); 62 | totalBytesRead += ((bytesRead != -1) ? bytesRead : 0); 63 | if (mDownloadResponseHandler != null) { 64 | mDownloadResponseHandler.onProgress(totalBytesRead, mResponseBody.contentLength()); 65 | } 66 | return bytesRead; 67 | } 68 | }; 69 | } 70 | } 71 | 72 | -------------------------------------------------------------------------------- /Volley/README.md: -------------------------------------------------------------------------------- 1 | ## Volley ## 2 | Volley框架代码的学习,相关代码添加了中文注释,有利于代码阅读 3 | 官方git地址:https://android.googlesource.com/platform/frameworks/volley 4 | Volley提供的功能: 5 | 简单来说,它提供了如下的便利功能: 6 | JSON,图像等的异步下载; 7 | 网络请求的排序(scheduling); 8 | 网络请求的优先级处理; 9 | 缓存; 10 | 多级别取消请求; 11 | 和Activity和生命周期的联动(Activity结束时同时取消所有网络请求). 12 | json请求: 13 | 14 | mQueue = Volley.newRequestQueue(getApplicationContext()); 15 | 16 | mQueue.add(new JsonObjectRequest( 17 | Method.GET, url, null,new Listener() { 18 | @Override 19 | public void onResponse(JSONObject response) { 20 | Log.d(TAG, "response : " + response.toString()); 21 | } 22 | }, null)); 23 | mQueue.start(); 24 | 25 | 获取网络图片: 26 | 27 | // imageView是一个ImageView实例 28 | 29 | // ImageLoader.getImageListener的第二个参数是默认的图片resource id 30 | // 第三个参数是请求失败时候的资源id,可以指定为0 31 | 32 | ImageListener listener = ImageLoader.getImageListener( 33 | imageView,android.R.drawable.ic_menu_rotate, android.R.drawable.ic_delete); 34 | mImageLoader.get(url, listener); 35 | 36 | 或者: 37 | 38 | mImageView.setImageUrl(url, imageLoader) 39 | 40 | Volley源码分析: 41 | 在RequestQueue类里面有这么一块代码: 42 | //可以传入一个自定义的HttpStack,比如OkHttpClient 43 | 44 | 45 | if (stack == null) { 46 | if (Build.VERSION.SDK_INT >= 9) { 47 | stack = new HurlStack(); 48 | } else { 49 | // Prior to Gingerbread, HttpUrlConnection was unreliable. 50 | // See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html 51 | stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent)); 52 | } 53 | } 54 | 55 | 代码中网络请求的实现由两种类型: 56 | 一种是Java原生的HttpURLConnection实现(HurlStack),一种是Apache的HttpClient实现(HttpClientStack),Volley会在android2.3以前使用HttpClient实现,在android2.3及以后使用HttpURLConnection实现,至于原因,官方的解释是:在Eclair和Froyo上Apache HTTP client拥有更少的bug,更好的稳定性,在Gingerbread以及以后的版本中,HttpURLConnection是最好的选择,它简单的api以及轻量级非常适合Android。压缩和缓存机制降低了网路使用,提高了速度、节省了电量。 57 | Volley优化 58 | 59 | [添加支持GZIP的网络请求;添加进度显示;关于HTTPS][1] 60 | 61 | [开放网络请求dispatcher线程数量;定义一个优化后的ImageLoader;添加一个返回字节数据的ImageRequest;给图片添加有效期;][2] 62 | [1]: https://www.zybuluo.com/flyouting/note/22485 63 | [2]: https://www.zybuluo.com/flyouting/note/21391 64 | -------------------------------------------------------------------------------- /JavaSocketDemo/src/main/java/http_socket_server_client/multi/ServerThread.java: -------------------------------------------------------------------------------- 1 | package http_socket_server_client.multi; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.IOException; 5 | import java.io.InputStream; 6 | import java.io.InputStreamReader; 7 | import java.io.OutputStream; 8 | import java.io.PrintWriter; 9 | import java.net.Socket; 10 | 11 | 12 | 13 | /** 14 | * 服务器线程处理类 15 | * Created by xuning on 17/6/8. 16 | */ 17 | public class ServerThread extends Thread{ 18 | 19 | /**和本线程相关的Socket*/ 20 | Socket socket; 21 | 22 | public ServerThread(Socket socket){ 23 | this.socket = socket; 24 | } 25 | 26 | //线程执行的操作,响应客户端的请求 27 | @Override 28 | public void run() { 29 | InputStream is = null; 30 | InputStreamReader isr = null; 31 | BufferedReader br = null; 32 | OutputStream os = null; 33 | PrintWriter pw = null; 34 | try { 35 | //step3 获取输入流,读取客户端所发送的信息 36 | is = socket.getInputStream(); 37 | //将字节流包装为字符流 38 | isr = new InputStreamReader(is); 39 | //为输入流添加缓冲 40 | br = new BufferedReader(isr); 41 | 42 | String info; 43 | while((info = br.readLine()) != null){ //循环读取客户端的信息 44 | System.out.println("我是服务器,客户端说:" + info); 45 | } 46 | socket.shutdownInput(); 47 | 48 | //step4 获取输出流,用来响应客户端 49 | os = socket.getOutputStream(); 50 | //包装为打印流 51 | pw = new PrintWriter(os); 52 | pw.write("欢迎您"); 53 | pw.flush(); //刷新缓冲区 54 | } catch (IOException e) { 55 | e.printStackTrace(); 56 | } finally { 57 | //step5 关闭资源 58 | try { 59 | if(pw != null){ 60 | pw.close(); 61 | } 62 | if(os != null){ 63 | os.close(); 64 | } 65 | if(br != null){ 66 | br.close(); 67 | } 68 | if(isr != null){ 69 | isr.close(); 70 | } 71 | if(is != null){ 72 | is.close(); 73 | } 74 | } catch (IOException e) { 75 | e.printStackTrace(); 76 | } 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /Volley/src/com/android/volley/toolbox/ClearCacheRequest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 The Android Open Source Project 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 | 17 | package com.android.volley.toolbox; 18 | 19 | import com.android.volley.Cache; 20 | import com.android.volley.NetworkResponse; 21 | import com.android.volley.Request; 22 | import com.android.volley.Response; 23 | 24 | import android.os.Handler; 25 | import android.os.Looper; 26 | 27 | /** 28 | * A synthetic request used for clearing the cache. 29 | */ 30 | public class ClearCacheRequest extends Request { 31 | private final Cache mCache; 32 | private final Runnable mCallback; 33 | 34 | /** 35 | * Creates a synthetic request for clearing the cache. 36 | * @param cache Cache to clear 37 | * @param callback Callback to make on the main thread once the cache is clear, 38 | * or null for none 39 | */ 40 | public ClearCacheRequest(Cache cache, Runnable callback) { 41 | super(Method.GET, null, null); 42 | mCache = cache; 43 | mCallback = callback; 44 | } 45 | 46 | @Override 47 | public boolean isCanceled() { 48 | // This is a little bit of a hack, but hey, why not. 49 | mCache.clear(); 50 | if (mCallback != null) { 51 | Handler handler = new Handler(Looper.getMainLooper()); 52 | handler.postAtFrontOfQueue(mCallback); 53 | } 54 | return true; 55 | } 56 | 57 | @Override 58 | public Priority getPriority() { 59 | return Priority.IMMEDIATE; 60 | } 61 | 62 | @Override 63 | protected Response parseNetworkResponse(NetworkResponse response) { 64 | return null; 65 | } 66 | 67 | @Override 68 | protected void deliverResponse(Object response) { 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /Volley/src/com/android/volley/toolbox/JsonArrayRequest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2011 The Android Open Source Project 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 | 17 | package com.android.volley.toolbox; 18 | 19 | import com.android.volley.NetworkResponse; 20 | import com.android.volley.ParseError; 21 | import com.android.volley.Response; 22 | import com.android.volley.Response.ErrorListener; 23 | import com.android.volley.Response.Listener; 24 | 25 | import org.json.JSONArray; 26 | import org.json.JSONException; 27 | 28 | import java.io.UnsupportedEncodingException; 29 | 30 | /** 31 | * A request for retrieving a {@link JSONArray} response body at a given URL. 32 | */ 33 | public class JsonArrayRequest extends JsonRequest { 34 | 35 | /** 36 | * Creates a new request. 37 | * @param url URL to fetch the JSON from 38 | * @param listener Listener to receive the JSON response 39 | * @param errorListener Error listener, or null to ignore errors. 40 | */ 41 | public JsonArrayRequest(String url, Listener listener, ErrorListener errorListener) { 42 | super(Method.GET, url, null, listener, errorListener); 43 | } 44 | 45 | @Override 46 | protected Response parseNetworkResponse(NetworkResponse response) { 47 | try { 48 | String jsonString = 49 | new String(response.data, HttpHeaderParser.parseCharset(response.headers)); 50 | return Response.success(new JSONArray(jsonString), 51 | HttpHeaderParser.parseCacheHeaders(response)); 52 | } catch (UnsupportedEncodingException e) { 53 | return Response.error(new ParseError(e)); 54 | } catch (JSONException je) { 55 | return Response.error(new ParseError(je)); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /VolleyDemo/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 14 | 15 | 23 | 24 | 32 | 33 | 41 | 42 | 51 | 52 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /OkHttpDemo/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 9 | 10 | 11 | 18 | 19 |