├── .gradle └── 2.4 │ └── taskArtifacts │ ├── cache.properties │ ├── cache.properties.lock │ ├── fileHashes.bin │ ├── fileSnapshots.bin │ ├── outputFileStates.bin │ └── taskArtifacts.bin ├── .idea ├── .name ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── gradle.xml ├── libraries │ ├── appcompat_v7_23_0_1.xml │ ├── support_annotations_23_0_1.xml │ └── support_v4_23_0_1.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml ├── vcs.xml └── workspace.xml ├── README.md ├── app ├── .gitignore ├── app.iml ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── cn │ │ └── geckhan │ │ └── okhttpdemo │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── cn │ │ │ └── geckhan │ │ │ └── okhttpdemo │ │ │ └── MainActivity.java │ └── res │ │ ├── layout │ │ └── activity_main.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 │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── cn │ └── geckhan │ └── okhttpdemo │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── local.properties ├── okhttpdemo.iml └── settings.gradle /.gradle/2.4/taskArtifacts/cache.properties: -------------------------------------------------------------------------------- 1 | #Wed Oct 14 15:27:59 CST 2015 2 | -------------------------------------------------------------------------------- /.gradle/2.4/taskArtifacts/cache.properties.lock: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geckhan/okhttpdemo/bd4002b782f0733d5eda5520d6e6922dc2aa8d5a/.gradle/2.4/taskArtifacts/cache.properties.lock -------------------------------------------------------------------------------- /.gradle/2.4/taskArtifacts/fileHashes.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geckhan/okhttpdemo/bd4002b782f0733d5eda5520d6e6922dc2aa8d5a/.gradle/2.4/taskArtifacts/fileHashes.bin -------------------------------------------------------------------------------- /.gradle/2.4/taskArtifacts/fileSnapshots.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geckhan/okhttpdemo/bd4002b782f0733d5eda5520d6e6922dc2aa8d5a/.gradle/2.4/taskArtifacts/fileSnapshots.bin -------------------------------------------------------------------------------- /.gradle/2.4/taskArtifacts/outputFileStates.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geckhan/okhttpdemo/bd4002b782f0733d5eda5520d6e6922dc2aa8d5a/.gradle/2.4/taskArtifacts/outputFileStates.bin -------------------------------------------------------------------------------- /.gradle/2.4/taskArtifacts/taskArtifacts.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/geckhan/okhttpdemo/bd4002b782f0733d5eda5520d6e6922dc2aa8d5a/.gradle/2.4/taskArtifacts/taskArtifacts.bin -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | okhttpdemo -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/libraries/appcompat_v7_23_0_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /.idea/libraries/support_annotations_23_0_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/libraries/support_v4_23_0_1.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | 47 | 48 | 49 | 50 | 1.8 51 | 52 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 11 | 12 | 13 | 14 | 15 | 22 | 23 | 24 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 102 | 103 | 104 | 128 | 1211 | 1221 | 1532 | 1535 | 1536 | 1537 | 1546 | 1547 | 1548 | 1549 | 1550 | 1551 | 1555 | 1556 | 1557 | 1558 | 1559 | 1560 | 1561 | 1562 | 1563 | 1564 | 1565 | 1566 | 1567 | 1568 | 1569 | 1570 | 1571 | 1572 | 1573 | 1574 | 1575 | 1576 | 1577 | 1578 | 1579 | 1580 | 1581 | 1582 | 1583 | 1584 | 1585 | 1588 | 1589 | 1590 | 1591 | 1594 | 1595 | 1598 | 1599 | 1600 | 1601 | 1604 | 1605 | 1608 | 1609 | 1612 | 1613 | 1614 | 1615 | 1618 | 1619 | 1622 | 1623 | 1626 | 1627 | 1630 | 1631 | 1632 | 1633 | 1636 | 1637 | 1640 | 1641 | 1644 | 1645 | 1648 | 1649 | 1652 | 1653 | 1654 | 1655 | 1658 | 1659 | 1662 | 1663 | 1666 | 1667 | 1670 | 1671 | 1674 | 1675 | 1678 | 1679 | 1682 | 1683 | 1684 | 1685 | 1686 | 1687 | 1688 | 1689 | 1692 | 1693 | 1694 | 1695 | 1698 | 1699 | 1702 | 1703 | 1704 | 1705 | 1708 | 1709 | 1712 | 1713 | 1716 | 1717 | 1718 | 1719 | 1720 | 1721 | 1722 | 1723 | 1724 | 1725 | 1726 | 1727 | 1728 | 1729 | 1730 | 1731 | 1732 | 1733 | 1734 | 1735 | 1736 | 1737 | 1738 | 1739 | 1740 | 1769 | 1770 | 1771 | 1800 | 1801 | 1802 | 1815 | 1816 | 1817 | 1818 | 1832 | 1833 | 1834 | 1835 | 1836 | 1837 | 1838 | 1839 | 1840 | 1841 | 1842 | 1849 | 1850 | 1851 | 1852 | 1870 | 1877 | 1878 | 1879 | 1908 | 1909 | 1910 | 1911 | 1912 | 1920 | 1921 | 1923 | 1924 | 1925 | 1926 | 1927 | 1928 | 1929 | 1930 | 1931 | 1444807675180 1932 | 1935 | 1936 | 1937 | 1938 | 1939 | 1940 | 1941 | 1942 | 1943 | 1944 | 1945 | 1946 | 1947 | 1948 | 1949 | 1950 | 1951 | 1952 | 1953 | 1954 | 1955 | 1956 | 1957 | 1958 | 1959 | 1960 | 1961 | 1962 | 1963 | 1964 | 1965 | 1966 | 1967 | 1968 | 1969 | 1970 | 1971 | 1972 | 1975 | 1978 | 1979 | 1980 | 1982 | 1983 | 1984 | 1985 | 1986 | 1987 | 1988 | 1989 | 1990 | 1991 | 1992 | 1993 | 1994 | 1995 | 1996 | 1997 | 1998 | 1999 | 2000 | 2001 | 2002 | 2003 | 2004 | 2005 | 2006 | 2007 | 2008 | 2009 | 2010 | 2011 | 2012 | 2013 | 2014 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020 | 2021 | 2022 | 2023 | 2024 | 2025 | 2026 | 2027 | 2028 | 2029 | 2030 | 2031 | 2032 | 2033 | 2034 | 2035 | 2036 | 2037 | 2038 | 2039 | 2040 | 2041 | 2042 | 2043 | 2044 | 2045 | 2046 | 2047 | 2048 | 2049 | 2050 | 2051 | 2052 | 2053 | 2054 | 2055 | 2056 | 2057 | 2058 | 2059 | 2060 | 2061 | 2062 | 2063 | 2064 | 2065 | 2066 | 2067 | 2068 | 2069 | 2070 | 2071 | 2072 | 2073 | 2074 | 2075 | 2076 | 2077 | 2078 | 2079 | 2080 | 2081 | 2082 | 2083 | 2084 | 2085 | 2086 | 2087 | 2088 | 2089 | 2090 | 2091 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # okhttpdemo 2 | Hello everyOne. 3 | 实习了这么久终于开源一次了。 4 | 这次导师让我,做网络模块封装,底层采用的是okhttp。 5 | 翻了一遍网络上的,也木有找到能返回相关状态的demo,于是之自己写。 6 | 因为公司不让上传内部代码,所以说重新写了个小的demo,不能和业务重合,demo有点简陋,只有核心的几行代码,请见谅。 7 | 8 | ```java 9 | public interface IProgressListener { 10 | void onStart(long contentLength); 11 | void onProgress(long currentLength); 12 | void onSuccess(String filePath); 13 | } 14 | //先定义一个接口 15 | 16 | private void downloading(final OkHttpClient client, final Request request, IProgressListener listener) throws IOException { 17 | Response response = client.newCall(request).execute(); 18 | //连接网络 19 | 20 | InputStream inputStream=null; 21 | int count ; 22 | byte[] bytes = new byte[BYTESIZE]; 23 | 24 | if (response.isSuccessful()) { 25 | //如果返回的状态码是成功的 获取inputstream 并且回调传入文件的长度 26 | inputStream = response.body().byteStream(); 27 | listener.onStart(response.body().contentLength()); 28 | } 29 | 30 | //io时 传出 目前读取进度 31 | if (inputStream!=null){ 32 | BufferedInputStream bis = new BufferedInputStream(inputStream); 33 | int sumCount =0; 34 | while ((count=bis.read(bytes))!=-1){ 35 | sumCount+=count; 36 | listener.onProgress(sumCount); 37 | } 38 | } 39 | 40 | 41 | /** 42 | * todo 校验文件 是否完整等问题 43 | */ 44 | 45 | listener.onSuccess("ok"); 46 | 47 | 48 | } 49 | 50 | 51 | ``` 52 | 53 | 如果上传的请使用 大神写的 [CoreProgess](https://github.com/lizhangqu/CoreProgress),膜拜过,但在这里有个bug:sink 中获取的byteCount然后传出,这里bytecount 是 byte数组的size 是固定死的,但在io时,每次读出来的count是小于 byte数组的size的 。我也给大神fork过了。 54 | 55 | 56 | 57 | 58 | 良辰在此有理了,geckhan。 59 | 60 | 61 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/app.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.1" 6 | 7 | defaultConfig { 8 | applicationId "cn.geckhan.okhttpdemo" 9 | minSdkVersion 16 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 | 22 | dependencies { 23 | compile fileTree(include: ['*.jar'], dir: 'libs') 24 | testCompile 'junit:junit:4.12' 25 | compile 'com.android.support:appcompat-v7:23.0.1' 26 | compile 'com.squareup.okhttp:okhttp:2.5.0' 27 | } 28 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/geckhan/Library/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 | -------------------------------------------------------------------------------- /app/src/androidTest/java/cn/geckhan/okhttpdemo/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package cn.geckhan.okhttpdemo; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /app/src/main/java/cn/geckhan/okhttpdemo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package cn.geckhan.okhttpdemo; 2 | 3 | import android.os.AsyncTask; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.os.Bundle; 6 | import android.view.View; 7 | 8 | import com.squareup.okhttp.OkHttpClient; 9 | import com.squareup.okhttp.Request; 10 | import com.squareup.okhttp.Response; 11 | 12 | import java.io.BufferedInputStream; 13 | import java.io.IOException; 14 | import java.io.InputStream; 15 | 16 | public class MainActivity extends AppCompatActivity { 17 | 18 | private final OkHttpClient client = new OkHttpClient(); 19 | 20 | private final int BYTESIZE= 2048; 21 | @Override 22 | protected void onCreate(Bundle savedInstanceState) { 23 | super.onCreate(savedInstanceState); 24 | setContentView(R.layout.activity_main); 25 | findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { 26 | @Override 27 | public void onClick(View v) { 28 | mTask.execute(); 29 | } 30 | }); 31 | 32 | 33 | } 34 | AsyncTask mTask = new AsyncTask() { 35 | @Override 36 | protected Void doInBackground(Void... params) { 37 | Request mRquest = new Request.Builder().url("http://121.41.119.107:81/test/1.doc").build(); 38 | try { 39 | downloading(client, mRquest, new IProgressListener() { 40 | @Override 41 | public void onStart(long contentLength) { 42 | MtLog.LogV("需要下载的文件长度"+contentLength); 43 | } 44 | 45 | @Override 46 | public void onProgress(long currentLength) { 47 | MtLog.LogV("当前进度"+currentLength); 48 | } 49 | 50 | @Override 51 | public void onSuccess(String filePath) { 52 | MtLog.LogV("下载成功,文件地址"+filePath); 53 | } 54 | }); 55 | } catch (IOException e) { 56 | e.printStackTrace(); 57 | } 58 | return null; 59 | } 60 | }; 61 | 62 | private void downloading(final OkHttpClient client, final Request request, IProgressListener listener) throws IOException { 63 | Response response = client.newCall(request).execute(); 64 | InputStream inputStream=null; 65 | int count ; 66 | byte[] bytes = new byte[BYTESIZE]; 67 | 68 | if (response.isSuccessful()) { 69 | inputStream = response.body().byteStream(); 70 | listener.onStart(response.body().contentLength()); 71 | } 72 | 73 | if (inputStream!=null){ 74 | BufferedInputStream bis = new BufferedInputStream(inputStream); 75 | int sumCount =0; 76 | while ((count=bis.read(bytes))!=-1){ 77 | sumCount+=count; 78 | listener.onProgress(sumCount); 79 | } 80 | } 81 | 82 | 83 | /** 84 | * todo 校验文件 是否完整等问题 85 | */ 86 | 87 | listener.onSuccess("ok"); 88 | 89 | 90 | } 91 | 92 | 93 | } 94 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 17 | 18 |