Giter Site home page Giter Site logo

coreprogress's Introduction

CoreProgress is a framework to support OkHttp upload and download progress

Download

Changelog

Current version 0.0.2 released on 10th Sept 2015

See details in CHANGELOG

Examples

I have provided a sample . See samples here on Github To run Sample application, simply clone the repository and use android studio to compile, install it on connected device

Usage

Gradle

dependencies {
  compile 'cn.edu.zafu:coreprogress:0.0.2'
}

Notice that it will not compile Okhttp library,you should add it yourself

upload

//OkHttpClient请求Client
private static final OkHttpClient client = new OkHttpClient();

//这个是非ui线程回调,不可直接操作UI
final ProgressRequestListener progressListener = new ProgressRequestListener() {
	@Override
	public void onRequestProgress(long bytesWrite, long contentLength, boolean done) {
		Log.e("TAG", "bytesWrite:" + bytesWrite);
		Log.e("TAG", "contentLength" + contentLength);
		Log.e("TAG", (100 * bytesWrite) / contentLength + " % done ");
		Log.e("TAG", "done:" + done);
		Log.e("TAG", "================================");
	}
};
//这个是ui线程回调,可直接操作UI
final UIProgressRequestListener uiProgressRequestListener = new UIProgressRequestListener() {
	@Override
	public void onUIRequestProgress(long bytesWrite, long contentLength, boolean done) {
		Log.e("TAG", "bytesWrite:" + bytesWrite);
		Log.e("TAG", "contentLength" + contentLength);
		Log.e("TAG", (100 * bytesWrite) / contentLength + " % done ");
		Log.e("TAG", "done:" + done);
		Log.e("TAG", "================================");
	}
};
//文件
File file = new File("文件路径");
//构造上传请求,类似web表单
RequestBody requestBody = new MultipartBuilder().type(MultipartBuilder.FORM)
		.addFormDataPart("hello", "android")
		.addFormDataPart("photo", file.getName(), RequestBody.create(null, file))
		.addPart(Headers.of("Content-Disposition", "form-data; name=\"another\";filename=\"another.dex\""), RequestBody.create(MediaType.parse("application/octet-stream"), file))
		.build();

//进行包装,使其支持进度回调
final Request request = new Request.Builder().url("上传的服务器路径").post(ProgressHelper.addProgressRequestListener(requestBody, uiProgressRequestListener)).build();
//开始请求
client.newCall(request).enqueue(new Callback() {
	@Override
	public void onFailure(Request request, IOException e) {
		Log.e("TAG", "error ", e);
	}

	@Override
	public void onResponse(Response response) throws IOException {
		Log.e("TAG", response.body().string());
	}
});

download

//OkHttpClient请求Client
private static final OkHttpClient client = new OkHttpClient();

//这个是非ui线程回调,不可直接操作UI
final ProgressResponseListener progressResponseListener = new ProgressResponseListener() {
	@Override
	public void onResponseProgress(long bytesRead, long contentLength, boolean done) {
		Log.e("TAG", "bytesRead:" + bytesRead);
		Log.e("TAG", "contentLength:" + contentLength);
		Log.e("TAG", "done:" + done);
		if (contentLength != -1) {
			//长度未知的情况下回返回-1
			Log.e("TAG", (100 * bytesRead) / contentLength + "% done");
		}
		Log.e("TAG", "================================");
	}
};


//这个是ui线程回调,可直接操作UI
final UIProgressResponseListener uiProgressResponseListener = new UIProgressResponseListener() {
	@Override
	public void onUIResponseProgress(long bytesRead, long contentLength, boolean done) {
		Log.e("TAG", "bytesRead:" + bytesRead);
		Log.e("TAG", "contentLength:" + contentLength);
		Log.e("TAG", "done:" + done);
		if (contentLength != -1) {
			//长度未知的情况下回返回-1
			Log.e("TAG", (100 * bytesRead) / contentLength + "% done");
		}
		Log.e("TAG", "================================");
		//ui层回调
	}
};

//构造请求
final Request request1 = new Request.Builder()
		.url("下载的服务器路径")
		.build();

//包装Response使其支持进度回调
ProgressHelper.addProgressResponseListener(client, uiProgressResponseListener).newCall(request1).enqueue(new Callback() {
	@Override
	public void onFailure(Request request, IOException e) {
		Log.e("TAG", "error ", e);
	}

	@Override
	public void onResponse(Response response) throws IOException {
		Log.e("TAG", response.body().string());
	}
});

start or finish(version 0.0.2 support)

If you need listener the start or finish callback,you need to override the method onUIRequestStart or onUIRequestFinish in UIProgressRequestListener,onUIResponseStart or onUIResponseFinish in UIProgressResponseListener

//这个是ui线程回调,可直接操作UI
final UIProgressRequestListener uiProgressRequestListener = new UIProgressRequestListener() {
    @Override
    public void onUIRequestProgress(long bytesWrite, long contentLength, boolean done) {
        Toast.makeText(getApplicationContext(), bytesWrite + " " + contentLength + " " + done, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onUIRequestStart(long bytesWrite, long contentLength, boolean done) {
        Toast.makeText(getApplicationContext(),"start",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onUIRequestFinish(long bytesWrite, long contentLength, boolean done) {
        Toast.makeText(getApplicationContext(),"end",Toast.LENGTH_SHORT).show();
    }
};
//这个是ui线程回调,可直接操作UI
final UIProgressResponseListener uiProgressResponseListener = new UIProgressResponseListener() {
    @Override
    public void onUIResponseProgress(long bytesRead, long contentLength, boolean done) {
       Toast.makeText(getApplicationContext(), bytesRead + " " + contentLength + " " + done, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onUIResponseStart(long bytesRead, long contentLength, boolean done) {
        Toast.makeText(getApplicationContext(),"start",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onUIResponseFinish(long bytesRead, long contentLength, boolean done) {
        Toast.makeText(getApplicationContext(),"end",Toast.LENGTH_SHORT).show();
    }
};

License

Copyright 2015 ZhangQu Li

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

coreprogress's People

Contributors

lizhangqu avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.