멀티 파트 파일 업로드 진행률을 나타내는 진행률 표시 줄을 구현하려고합니다.OKHTTP를 사용하여 멀티 파트 파일 업로드의 진행 상황을 추적
나는이 응답에 대한 의견 - https://stackoverflow.com/a/24285633/1022454을 읽고 RequestBody로 전달 된 싱크를 래핑하고 이동 된 바이트를 추적하는 콜백을 제공해야합니다.
사용자 정의 RequestBody를 작성하고 싱크를 CustomSink 클래스로 랩핑했지만 디버깅을 통해 바이트가 RealBufferedSink11n으로 작성되고 사용자 정의 싱크 쓰기 메소드가 한 번만 실행되어 나를 허용하지 않는다는 것을 알 수 있습니다. 이동 된 바이트를 추적하십시오.
private class CustomRequestBody extends RequestBody {
MediaType contentType;
byte[] content;
private CustomRequestBody(final MediaType contentType, final byte[] content) {
this.contentType = contentType;
this.content = content;
}
@Override
public MediaType contentType() {
return contentType;
}
@Override
public long contentLength() {
return content.length;
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
CustomSink customSink = new CustomSink(sink);
customSink.write(content);
}
}
private class CustomSink implements BufferedSink {
private static final String TAG = "CUSTOM_SINK";
BufferedSink bufferedSink;
private CustomSink(BufferedSink bufferedSink) {
this.bufferedSink = bufferedSink;
}
@Override
public void write(Buffer source, long byteCount) throws IOException {
Log.d(TAG, "source size: " + source.size() + " bytecount" + byteCount);
bufferedSink.write(source, byteCount);
}
@Override
public void flush() throws IOException {
bufferedSink.flush();
}
@Override
public Timeout timeout() {
return bufferedSink.timeout();
}
@Override
public void close() throws IOException {
bufferedSink.close();
}
@Override
public Buffer buffer() {
return bufferedSink.buffer();
}
@Override
public BufferedSink write(ByteString byteString) throws IOException {
return bufferedSink.write(byteString);
}
@Override
public BufferedSink write(byte[] source) throws IOException {
return bufferedSink.write(source);
}
@Override
public BufferedSink write(byte[] source, int offset, int byteCount) throws IOException {
return bufferedSink.write(source, offset, byteCount);
}
@Override
public long writeAll(Source source) throws IOException {
return bufferedSink.writeAll(source);
}
@Override
public BufferedSink writeUtf8(String string) throws IOException {
return bufferedSink.writeUtf8(string);
}
@Override
public BufferedSink writeString(String string, Charset charset) throws IOException {
return bufferedSink.writeString(string, charset);
}
@Override
public BufferedSink writeByte(int b) throws IOException {
return bufferedSink.writeByte(b);
}
@Override
public BufferedSink writeShort(int s) throws IOException {
return bufferedSink.writeShort(s);
}
@Override
public BufferedSink writeShortLe(int s) throws IOException {
return bufferedSink.writeShortLe(s);
}
@Override
public BufferedSink writeInt(int i) throws IOException {
return bufferedSink.writeInt(i);
}
@Override
public BufferedSink writeIntLe(int i) throws IOException {
return bufferedSink.writeIntLe(i);
}
@Override
public BufferedSink writeLong(long v) throws IOException {
return bufferedSink.writeLong(v);
}
@Override
public BufferedSink writeLongLe(long v) throws IOException {
return bufferedSink.writeLongLe(v);
}
@Override
public BufferedSink emitCompleteSegments() throws IOException {
return bufferedSink.emitCompleteSegments();
}
@Override
public OutputStream outputStream() {
return bufferedSink.outputStream();
}
}
아무도이 작업을 수행하는 방법에 대한 예제가 있습니까?
이 느린 네트워크 연결을 통해 작은 파일을 업로드 할 경우 작동하지 않습니다. https://github.com/square/okhttp/issues/1078을 참조하십시오. 이 사건에 대한 해결책이 있습니까? – coalmee
@Edy Bolos RxJava & Observable과 함께 사용하는 방법이 있습니까? – Sree
질문에 대답하려면 : Observable로 모든 것을 감쌀 수 있습니다. :)하지만 다른 사람에게 맡겨야합니다. 나의 유일한 제안은'UploadsHandler'에서 진도 값을 출력하기 위해'BehaviorSubject'를 사용하는 것일 것입니다. –