{"id":19483650,"url":"https://github.com/lizhangqu/coreprogress","last_synced_at":"2025-04-05T03:12:03.604Z","repository":{"id":1945971,"uuid":"41793105","full_name":"lizhangqu/CoreProgress","owner":"lizhangqu","description":"OkHttp upload and download progress support","archived":false,"fork":false,"pushed_at":"2022-04-14T15:40:59.000Z","size":18693,"stargazers_count":518,"open_issues_count":5,"forks_count":139,"subscribers_count":20,"default_branch":"master","last_synced_at":"2025-03-29T02:08:26.642Z","etag":null,"topics":["okhttp","progress"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lizhangqu.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-09-02T09:42:12.000Z","updated_at":"2024-06-24T17:32:07.000Z","dependencies_parsed_at":"2022-08-06T11:16:11.007Z","dependency_job_id":null,"html_url":"https://github.com/lizhangqu/CoreProgress","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lizhangqu%2FCoreProgress","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lizhangqu%2FCoreProgress/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lizhangqu%2FCoreProgress/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lizhangqu%2FCoreProgress/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lizhangqu","download_url":"https://codeload.github.com/lizhangqu/CoreProgress/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247280272,"owners_count":20912967,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["okhttp","progress"],"created_at":"2024-11-10T20:16:14.147Z","updated_at":"2025-04-05T03:12:03.569Z","avatar_url":"https://github.com/lizhangqu.png","language":"Java","readme":"CoreProgress is a framework to support OkHttp upload and download progress\n====================================\n\n[ ![Download](https://api.bintray.com/packages/lizhangqu/maven/coreprogress/images/download.svg) ](https://bintray.com/lizhangqu/maven/coreprogress/_latestVersion)\n\nChangelog\n---------\n\nCurrent version 1.0.2 released on 13th July 2017\n\nSee details in [CHANGELOG](https://github.com/lizhangqu/CoreProgress/blob/master/CHANGELOG.md)\n\n\nExamples\n--------\n\nI have provided a sample .\nSee samples [here on Github](https://github.com/lizhangqu/CoreProgress/tree/master/sample)\nTo run Sample application, clone the repository and use android studio to compile, install it on a connected device.\n\nYou can alse see the test code [here on Github](https://github.com/lizhangqu/CoreProgress/blob/master/library/src/test/java/io/github/lizhangqu/coreprogress/ProgressTest.java)\n\nUsage\n-----\n\n\n**Gradle**\n\n```\ndependencies {\n  compile 'io.github.lizhangqu:coreprogress:1.0.2'\n}\n```\n\n\n**upload**\n\n```\n//client\nOkHttpClient okHttpClient = new OkHttpClient();\n//request builder\nRequest.Builder builder = new Request.Builder();\nbuilder.url(url);\n\n//your original request body\nMultipartBody.Builder bodyBuilder = new MultipartBody.Builder();\nbodyBuilder.addFormDataPart(\"testFile\", file.getName(), RequestBody.create(null, file));\nMultipartBody body = bodyBuilder.build();\n\n//wrap your original request body with progress\nRequestBody requestBody = ProgressHelper.withProgress(body, new ProgressUIListener() {\n\n    //if you don't need this method, don't override this methd. It isn't an abstract method, just an empty method.\n    @Override\n    public void onUIProgressStart(long totalBytes) {\n        super.onUIProgressStart(totalBytes);\n        Log.e(\"TAG\", \"onUIProgressStart:\" + totalBytes);\n    }\n\n    @Override\n    public void onUIProgressChanged(long numBytes, long totalBytes, float percent, float speed) {\n        Log.e(\"TAG\", \"=============start===============\");\n        Log.e(\"TAG\", \"numBytes:\" + numBytes);\n        Log.e(\"TAG\", \"totalBytes:\" + totalBytes);\n        Log.e(\"TAG\", \"percent:\" + percent);\n        Log.e(\"TAG\", \"speed:\" + speed);\n        Log.e(\"TAG\", \"============= end ===============\");\n        uploadProgress.setProgress((int) (100 * percent));\n        uploadInfo.setText(\"numBytes:\" + numBytes + \" bytes\" + \"\\ntotalBytes:\" + totalBytes + \" bytes\" + \"\\npercent:\" + percent * 100 + \" %\" + \"\\nspeed:\" + speed * 1000 / 1024 / 1024 + \"  MB/秒\");\n    }\n    \n    //if you don't need this method, don't override this methd. It isn't an abstract method, just an empty method.\n    @Override\n    public void onUIProgressFinish() {\n        super.onUIProgressFinish();\n        Log.e(\"TAG\", \"onUIProgressFinish:\");\n        Toast.makeText(getApplicationContext(), \"结束上传\", Toast.LENGTH_SHORT).show();\n    }\n    \n});\n\n//post the wrapped request body\nbuilder.post(requestBody);\n//call\nCall call = okHttpClient.newCall(builder.build());\n//enqueue\ncall.enqueue(new Callback() {\n    @Override\n    public void onFailure(Call call, IOException e) {\n        Log.e(\"TAG\", \"=============onFailure===============\");\n        e.printStackTrace();\n    }\n\n    @Override\n    public void onResponse(Call call, Response response) throws IOException {\n        Log.e(\"TAG\", \"=============onResponse===============\");\n        Log.e(\"TAG\", \"request headers:\" + response.request().headers());\n        Log.e(\"TAG\", \"response headers:\" + response.headers());\n    }\n});\n```\n\nif you don't need callback in UI thread, you can use ProgressListener to callback in your caller's original Thread.\n\n**download**\n\n```\n//client\nOkHttpClient okHttpClient = new OkHttpClient();\n//request builder\nRequest.Builder builder = new Request.Builder();\nbuilder.url(url);\nbuilder.get();\n//call\nCall call = okHttpClient.newCall(builder.build());\n//enqueue\ncall.enqueue(new Callback() {\n    @Override\n    public void onFailure(Call call, IOException e) {\n        Log.e(\"TAG\", \"=============onFailure===============\");\n        e.printStackTrace();\n    }\n\n    @Override\n    public void onResponse(Call call, Response response) throws IOException {\n        Log.e(\"TAG\", \"=============onResponse===============\");\n        Log.e(\"TAG\", \"request headers:\" + response.request().headers());\n        Log.e(\"TAG\", \"response headers:\" + response.headers());\n        \n        //your original response body\n        ResponseBody body = response.body()\n        //wrap the original response body with progress\n        ResponseBody responseBody = ProgressHelper.withProgress(body, new ProgressUIListener() {\n        \n            //if you don't need this method, don't override this methd. It isn't an abstract method, just an empty method.\n            @Override\n            public void onUIProgressStart(long totalBytes) {\n                super.onUIProgressStart(totalBytes);\n                Log.e(\"TAG\", \"onUIProgressStart:\" + totalBytes);\n            }\n            \n            @Override\n            public void onUIProgressChanged(long numBytes, long totalBytes, float percent, float speed) {\n                Log.e(\"TAG\", \"=============start===============\");\n                Log.e(\"TAG\", \"numBytes:\" + numBytes);\n                Log.e(\"TAG\", \"totalBytes:\" + totalBytes);\n                Log.e(\"TAG\", \"percent:\" + percent);\n                Log.e(\"TAG\", \"speed:\" + speed);\n                Log.e(\"TAG\", \"============= end ===============\");\n                downloadProgeress.setProgress((int) (100 * percent));\n                downloadInfo.setText(\"numBytes:\" + numBytes + \" bytes\" + \"\\ntotalBytes:\" + totalBytes + \" bytes\" + \"\\npercent:\" + percent * 100 + \" %\" + \"\\nspeed:\" + speed * 1000 / 1024 / 1024 + \" MB/秒\");\n            }\n            \n            //if you don't need this method, don't override this methd. It isn't an abstract method, just an empty method.\n            @Override\n            public void onUIProgressFinish() {\n                super.onUIProgressFinish();\n                Log.e(\"TAG\", \"onUIProgressFinish:\");\n                Toast.makeText(getApplicationContext(), \"结束上传\", Toast.LENGTH_SHORT).show();\n            }\n            \n        });\n        //read the body to file\n        BufferedSource source = responseBody.source();\n        File outFile = new File(\"sdcard/temp.file\");\n        outFile.delete();\n        outFile.getParentFile().mkdirs();\n        outFile.createNewFile();\n        BufferedSink sink = Okio.buffer(Okio.sink(outFile));\n        source.readAll(sink);\n        sink.flush();\n        source.close();\n    }\n});\n        \n```\n\nif you don't need callback in UI thread, you can use ProgressListener to callback in your caller's original Thread.\n\n**callback in original thread**\n\nif you don't need callback in UI thread, you can use ProgressListener to callback in your caller's original Thread.\n\n```\n//callback in original thread.\nProgressListener progressListener = new ProgressListener() {\n\n    //if you don't need this method, don't override this methd. It isn't an abstract method, just an empty method.\n    @Override\n    public void onProgressStart(long totalBytes) {\n        super.onProgressStart(totalBytes);\n    }\n\n    @Override\n    public void onProgressChanged(long numBytes, long totalBytes, float percent, float speed) {\n\n    }\n\n    //if you don't need this method, don't override this methd. It isn't an abstract method, just an empty method.\n    @Override\n    public void onProgressFinish() {\n        super.onProgressFinish();\n    }\n};\n```\n\n\n## License\n\n    Copyright 2017 区长\n\n    Licensed under the Apache License, Version 2.0 (the \"License\");\n    you may not use this file except in compliance with the License.\n    You may obtain a copy of the License at\n\n       http://www.apache.org/licenses/LICENSE-2.0\n\n    Unless required by applicable law or agreed to in writing, software\n    distributed under the License is distributed on an \"AS IS\" BASIS,\n    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n    See the License for the specific language governing permissions and\n    limitations under the License.\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flizhangqu%2Fcoreprogress","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flizhangqu%2Fcoreprogress","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flizhangqu%2Fcoreprogress/lists"}