Skip to content

Commit

Permalink
add downloader
Browse files Browse the repository at this point in the history
  • Loading branch information
CPPAlien committed Jul 6, 2016
1 parent 444fe77 commit 9675b6a
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 50 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ Glide

4, 支持使用POST方法获得图片;

5, 支持创建多线程池

6, 支持上传、下载功能

###2, 使用方法

用Gradle的方式导入DaVinci库
Expand All @@ -53,7 +57,7 @@ repositories{
maven { url "https://jitpack.io" }
}
dependencies {
compile 'com.github.CPPAlien:DaVinci:1.2.2'
compile 'com.github.CPPAlien:DaVinci:1.2.5'
}
```

Expand Down
52 changes: 38 additions & 14 deletions app/src/main/java/cn/hadcn/davinci_example/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.hadcn.davinci_example;

import android.content.Intent;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
Expand All @@ -12,16 +13,20 @@
import org.json.JSONException;
import org.json.JSONObject;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import cn.hadcn.davinci.DaVinci;
import cn.hadcn.davinci.log.LogLevel;
import cn.hadcn.davinci.http.OnDaVinciRequestListener;
import cn.hadcn.davinci.log.VinciLog;
import cn.hadcn.davinci.upload.OnDaVinciUploadListener;
import cn.hadcn.davinci.other.OnVinciDownloadListener;
import cn.hadcn.davinci.other.OnVinciUploadListener;

public class MainActivity extends AppCompatActivity implements OnDaVinciRequestListener, OnDaVinciUploadListener{
public class MainActivity extends AppCompatActivity implements OnDaVinciRequestListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -70,35 +75,54 @@ public void onClick(View v) {
JSONObject jsonObject = new JSONObject();
try {
JSONObject header = new JSONObject();
header.put("tokenId", "29d8ea73-c21e-4989-8d36-f3c8facb04ee");
header.put("tokenId", "6240e6e5-10d1-4d1f-83bf-39910870583c");
jsonObject.put("_header_", header);
} catch (JSONException e) {
e.printStackTrace();
}

DaVinci.with(this).getUploader().extra("args", jsonObject).upload("http://192.168.3.117:12821/ecp/openapi/qs/file/upload", path, this);
DaVinci.with(this).getUploader().extra("args", jsonObject).upload("http://192.168.3.117:12821/ecp/openapi/qs/file/upload", path, new OnVinciUploadListener() {
@Override
public void onVinciUploadSuccess(JSONObject response) {

}

@Override
public void onVinciUploadFailed(String reason) {

}
});

DaVinci.with().addThreadPool("one", 1);
DaVinci.with().tag("one").getImageLoader().load("http://y3.ifengimg.com/fashion_spider/dci_2012/02/20a78c36cc31225b1a7efa89f566f591.jpg").into(image3);
}

@Override
public void onDaVinciRequestSuccess(String jsonObject) {
Log.i("DaVinciTest", toString());
}
OutputStream out = null;
try {
out = new FileOutputStream(Environment.getExternalStorageDirectory() + "/download/" + "a.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
DaVinci.with().getDownloader().body(jsonObject.toString()).download("http://ec2-52-192-96-229.ap-northeast-1.compute.amazonaws.com:12821/ecp/openapi/qs/file/download/p/2016/07/06/03/f5d28e3065244ab9952858f991838246.txt"
, out, new OnVinciDownloadListener() {
@Override
public void onVinciDownloadSuccess() {

@Override
public void onDaVinciRequestFailed(String errorInfo) {
}

@Override
public void onVinciDownloadFailed(String reason) {

}
});
}

@Override
public void onDaVinciUploadSuccess(JSONObject response) {

public void onDaVinciRequestSuccess(String jsonObject) {
Log.i("DaVinciTest", toString());
}

@Override
public void onDaVinciUploadFailed(String reason) {
public void onDaVinciRequestFailed(String errorInfo) {

}
}
7 changes: 6 additions & 1 deletion davinci/src/main/java/cn/hadcn/davinci/DaVinci.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
import cn.hadcn.davinci.image.VinciImageLoader;
import cn.hadcn.davinci.http.impl.HttpRequest;
import cn.hadcn.davinci.http.impl.PersistentCookieStore;
import cn.hadcn.davinci.upload.impl.VinciUpload;
import cn.hadcn.davinci.other.impl.VinciDownload;
import cn.hadcn.davinci.other.impl.VinciUpload;
import cn.hadcn.davinci.volley.RequestQueue;


Expand Down Expand Up @@ -205,4 +206,8 @@ public VinciImageLoader getImageLoader() {
public VinciUpload getUploader() {
return new VinciUpload(mRequestQueue);
}

public VinciDownload getDownloader() {
return new VinciDownload(mRequestQueue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.nio.ByteBuffer;

import cn.hadcn.davinci.log.VinciLog;
import cn.hadcn.davinci.volley.DefaultRetryPolicy;
import cn.hadcn.davinci.volley.NetworkResponse;
import cn.hadcn.davinci.volley.ParseError;
import cn.hadcn.davinci.volley.Request;
Expand All @@ -34,12 +33,6 @@
public class ByteRequest extends Request<ByteBuffer> {
protected static final String PROTOCOL_CHARSET = "utf-8";

public static final int DEFAULT_IMAGE_TIMEOUT_MS = 1000;

public static final int DEFAULT_IMAGE_MAX_RETRIES = 2;

public static final float DEFAULT_IMAGE_BACKOFF_MULT = 2f;

private final Response.Listener<ByteBuffer> mListener;

/** Decoding lock so that we don't decode more than one image at a time (to avoid OOM's) */
Expand All @@ -62,8 +55,7 @@ public class ByteRequest extends Request<ByteBuffer> {
*/
public ByteRequest(int method, String url, String requestBody, Response.Listener<ByteBuffer> listener, Response.ErrorListener errorListener) {
super(method, url, errorListener);
setRetryPolicy(new DefaultRetryPolicy(DEFAULT_IMAGE_TIMEOUT_MS, DEFAULT_IMAGE_MAX_RETRIES,
DEFAULT_IMAGE_BACKOFF_MULT));

mListener = listener;
mRequestBody = requestBody;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@
import java.util.HashMap;
import java.util.LinkedList;

import cn.hadcn.davinci.volley.DefaultRetryPolicy;
import cn.hadcn.davinci.volley.Request;
import cn.hadcn.davinci.volley.RequestQueue;
import cn.hadcn.davinci.volley.Response.*;
import cn.hadcn.davinci.volley.VolleyError;


public class ImageLoader {
public static final int DEFAULT_IMAGE_TIMEOUT_MS = 1000;

public static final int DEFAULT_IMAGE_MAX_RETRIES = 2;

public static final float DEFAULT_IMAGE_BACKOFF_MULT = 2f;

/** RequestQueue for dispatching ImageRequests onto. */
private final RequestQueue mRequestQueue;

Expand Down Expand Up @@ -115,7 +122,7 @@ public ImageContainer get(String requestUrl, String requestBody, ImageListener i
}

protected Request<ByteBuffer> makeImageRequest(int method, final String requestUrl, String requestBody) {
return new ByteRequest(method, requestUrl, requestBody, new Listener<ByteBuffer>() {
ByteRequest request = new ByteRequest(method, requestUrl, requestBody, new Listener<ByteBuffer>() {
@Override
public void onResponse(ByteBuffer response) {
onGetImageSuccess(requestUrl, response);
Expand All @@ -126,6 +133,9 @@ public void onErrorResponse(VolleyError error) {
onGetImageError(requestUrl, error);
}
});
request.setRetryPolicy(new DefaultRetryPolicy(DEFAULT_IMAGE_TIMEOUT_MS, DEFAULT_IMAGE_MAX_RETRIES,
DEFAULT_IMAGE_BACKOFF_MULT));
return request;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cn.hadcn.davinci.other;

/**
*
* Created by 90Chris on 2016/7/6.
*/
public interface OnVinciDownloadListener {
void onVinciDownloadSuccess();
void onVinciDownloadFailed(String reason);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package cn.hadcn.davinci.other;

import org.json.JSONObject;

/**
*
* Created by 90Chris on 2016/7/6.
*/
public interface OnVinciUploadListener {
void onVinciUploadSuccess(JSONObject response);
void onVinciUploadFailed(String reason);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package cn.hadcn.davinci.other.impl;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;

import cn.hadcn.davinci.image.base.ByteRequest;
import cn.hadcn.davinci.log.VinciLog;
import cn.hadcn.davinci.other.OnVinciDownloadListener;
import cn.hadcn.davinci.volley.DefaultRetryPolicy;
import cn.hadcn.davinci.volley.Request;
import cn.hadcn.davinci.volley.RequestQueue;
import cn.hadcn.davinci.volley.Response;
import cn.hadcn.davinci.volley.VolleyError;

/**
* VinciDownload
* Created by 90Chris on 2016/7/5.
*/
public class VinciDownload {
private RequestQueue mRequestQueue;
private String mBody;

public VinciDownload(RequestQueue mRequestQueue) {
this.mRequestQueue = mRequestQueue;
}

public VinciDownload body(String body) {
mBody = body;
return this;
}

public void download(String url, final OutputStream out, final OnVinciDownloadListener listener) {
ByteRequest request = new ByteRequest(Request.Method.POST, url, mBody, new Response.Listener<ByteBuffer>() {
@Override
public void onResponse(ByteBuffer response) {
try {
out.write(response.array());
listener.onVinciDownloadSuccess();
} catch (IOException e) {
VinciLog.e("write out error", e);
listener.onVinciDownloadFailed("write file failed");
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
listener.onVinciDownloadFailed("net failed");
}
});
request.setRetryPolicy(new DefaultRetryPolicy(4 * DefaultRetryPolicy.DEFAULT_TIMEOUT_MS,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
mRequestQueue.add(request);
}

public interface Listener {

}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package cn.hadcn.davinci.upload.impl;
package cn.hadcn.davinci.other.impl;


import org.json.JSONObject;

import java.io.File;

import cn.hadcn.davinci.log.VinciLog;
import cn.hadcn.davinci.upload.OnDaVinciUploadListener;
import cn.hadcn.davinci.other.OnVinciUploadListener;
import cn.hadcn.davinci.other.request.UploadRequest;
import cn.hadcn.davinci.volley.DefaultRetryPolicy;
import cn.hadcn.davinci.volley.RequestQueue;
import cn.hadcn.davinci.volley.Response;
Expand All @@ -17,7 +18,7 @@
* Created by 90Chris on 2015/11/11.
*/
public class VinciUpload {
RequestQueue mRequestQueue;
private RequestQueue mRequestQueue;
private String mFilePartName = null;
private String extraName;
private JSONObject extraObject;
Expand Down Expand Up @@ -47,11 +48,11 @@ public VinciUpload extra(String name, JSONObject object) {
* @param filePath local file path
* @param listener listener of uploading
*/
public void upload(String uploadUrl, String filePath, final OnDaVinciUploadListener listener) {
public void upload(String uploadUrl, String filePath, final OnVinciUploadListener listener) {
File file = new File(filePath);
if ( !file.exists() ) {
VinciLog.w("Upload file is not exists");
listener.onDaVinciUploadFailed("Upload file is not exists");
listener.onVinciUploadFailed("Upload file is not exists");
return;
}
UploadRequest uploadRequest = new UploadRequest(uploadUrl,
Expand All @@ -60,7 +61,7 @@ public void upload(String uploadUrl, String filePath, final OnDaVinciUploadListe
@Override
public void onResponse(JSONObject response) {
VinciLog.d("upload response:" + (response == null ? null : response.toString()));
listener.onDaVinciUploadSuccess(response);
listener.onVinciUploadSuccess(response);
}
},
new Response.ErrorListener() {
Expand All @@ -74,7 +75,7 @@ public void onErrorResponse(VolleyError error) {
}
VinciLog.e("http failed: " + reason);
if ( listener != null ) {
listener.onDaVinciUploadFailed(reason);
listener.onVinciUploadFailed(reason);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.hadcn.davinci.upload.impl;
package cn.hadcn.davinci.other.request;

import java.io.ByteArrayOutputStream;
import java.io.File;
Expand Down Expand Up @@ -34,7 +34,6 @@ public class UploadRequest extends Request<JSONObject> {
private static final String CHARSET = "utf-8";
private MultipartEntity mEntity;
private final Response.Listener<JSONObject> mListener;
protected Map<String, String> headers;

public UploadRequest(String url, Listener<JSONObject> listener, ErrorListener errorListener) {
super(Method.POST, url, errorListener);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ public interface ResponseDelivery {
/**
* Parses a response from the network or cache and delivers it.
*/
public void postResponse(Request<?> request, Response<?> response);
void postResponse(Request<?> request, Response<?> response);

/**
* Parses a response from the network or cache and delivers it. The provided
* Runnable will be executed after delivery.
*/
public void postResponse(Request<?> request, Response<?> response, Runnable runnable);
void postResponse(Request<?> request, Response<?> response, Runnable runnable);

/**
* Posts an error for the given request.
*/
public void postError(Request<?> request, VolleyError error);
void postError(Request<?> request, VolleyError error);
}

0 comments on commit 9675b6a

Please sign in to comment.