>();
29 |
30 | static {
31 | blackList.add(HttpException.class);
32 | blackList.add(Callback.CancelledException.class);
33 | blackList.add(MalformedURLException.class);
34 | blackList.add(URISyntaxException.class);
35 | blackList.add(NoRouteToHostException.class);
36 | blackList.add(PortUnreachableException.class);
37 | blackList.add(ProtocolException.class);
38 | blackList.add(NullPointerException.class);
39 | blackList.add(FileNotFoundException.class);
40 | blackList.add(JSONException.class);
41 | blackList.add(UnknownHostException.class);
42 | blackList.add(IllegalArgumentException.class);
43 | }
44 |
45 | public HttpRetryHandler() {
46 | }
47 |
48 | public void setMaxRetryCount(int maxRetryCount) {
49 | this.maxRetryCount = maxRetryCount;
50 | }
51 |
52 | public boolean canRetry(UriRequest request, Throwable ex, int count) {
53 |
54 | LogUtil.w(ex.getMessage(), ex);
55 |
56 | if (count > maxRetryCount) {
57 | LogUtil.w(request.toString());
58 | LogUtil.w("The Max Retry times has been reached!");
59 | return false;
60 | }
61 |
62 | if (!HttpMethod.permitsRetry(request.getParams().getMethod())) {
63 | LogUtil.w(request.toString());
64 | LogUtil.w("The Request Method can not be retried.");
65 | return false;
66 | }
67 |
68 | if (blackList.contains(ex.getClass())) {
69 | LogUtil.w(request.toString());
70 | LogUtil.w("The Exception can not be retried.");
71 | return false;
72 | }
73 |
74 | return true;
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/xutils/src/main/java/org/xutils/http/app/ParamsBuilder.java:
--------------------------------------------------------------------------------
1 | package org.xutils.http.app;
2 |
3 | import org.xutils.http.RequestParams;
4 | import org.xutils.http.annotation.HttpRequest;
5 |
6 | import javax.net.ssl.SSLSocketFactory;
7 |
8 | /**
9 | * Created by wyouflf on 15/8/20.
10 | *
11 | * {@link org.xutils.http.annotation.HttpRequest} 注解的参数构建的模板接口
12 | */
13 | public interface ParamsBuilder {
14 |
15 | /**
16 | * 根据@HttpRequest构建请求的url
17 | */
18 | String buildUri(RequestParams params, HttpRequest httpRequest) throws Throwable;
19 |
20 | /**
21 | * 根据注解的cacheKeys构建缓存的自定义key,
22 | * 如果返回为空, 默认使用 url 和整个 query string 组成.
23 | */
24 | String buildCacheKey(RequestParams params, String[] cacheKeys);
25 |
26 | /**
27 | * 自定义SSLSocketFactory
28 | */
29 | SSLSocketFactory getSSLSocketFactory() throws Throwable;
30 |
31 | /**
32 | * 为请求添加通用参数等操作
33 | */
34 | void buildParams(RequestParams params) throws Throwable;
35 |
36 | /**
37 | * 自定义参数签名
38 | */
39 | void buildSign(RequestParams params, String[] signs) throws Throwable;
40 | }
41 |
--------------------------------------------------------------------------------
/xutils/src/main/java/org/xutils/http/app/RedirectHandler.java:
--------------------------------------------------------------------------------
1 | package org.xutils.http.app;
2 |
3 | import org.xutils.http.RequestParams;
4 | import org.xutils.http.request.UriRequest;
5 |
6 | /**
7 | * Created by wyouflf on 15/11/12.
8 | * 请求重定向控制接口
9 | */
10 | public interface RedirectHandler {
11 |
12 | /**
13 | * 根据请求信息返回自定义重定向的请求参数
14 | *
15 | * @param request 原始请求
16 | * @return 返回不为null时进行重定向
17 | */
18 | RequestParams getRedirectParams(UriRequest request) throws Throwable;
19 | }
20 |
--------------------------------------------------------------------------------
/xutils/src/main/java/org/xutils/http/app/RequestInterceptListener.java:
--------------------------------------------------------------------------------
1 | package org.xutils.http.app;
2 |
3 |
4 | import org.xutils.http.request.UriRequest;
5 |
6 | /**
7 | * Created by wyouflf on 15/11/10.
8 | * 拦截请求响应(在后台线程工作).
9 | *
10 | * 用法:
11 | * 1. 请求的callback参数同时实现RequestInterceptListener
12 | * 2. 或者使用 @HttpRequest 注解实现ParamsBuilder接口
13 | */
14 | public interface RequestInterceptListener {
15 |
16 | /**
17 | * 检查请求参数等处理
18 | */
19 | void beforeRequest(UriRequest request) throws Throwable;
20 |
21 | /**
22 | * 检查请求相应头等处理
23 | */
24 | void afterRequest(UriRequest request) throws Throwable;
25 | }
--------------------------------------------------------------------------------
/xutils/src/main/java/org/xutils/http/app/RequestTracker.java:
--------------------------------------------------------------------------------
1 | package org.xutils.http.app;
2 |
3 | import org.xutils.http.RequestParams;
4 | import org.xutils.http.request.UriRequest;
5 |
6 | /**
7 | * Created by wyouflf on 15/9/10.
8 | * 请求过程追踪, 适合用来记录请求日志.
9 | * 所有回调方法都在主线程进行.
10 | *
11 | * 用法:
12 | * 1. 将RequestTracker实例设置给请求参数RequestParams.
13 | * 2. 请的callback参数同时实现RequestTracker接口;
14 | * 3. 注册给UriRequestFactory的默认RequestTracker.
15 | * 注意: 请求回调RequestTracker时优先级按照上面的顺序,
16 | * 找到一个RequestTracker的实现会忽略其他.
17 | */
18 | public interface RequestTracker {
19 |
20 | void onWaiting(RequestParams params);
21 |
22 | void onStart(RequestParams params);
23 |
24 | void onRequestCreated(UriRequest request);
25 |
26 | void onCache(UriRequest request, Object result);
27 |
28 | void onSuccess(UriRequest request, Object result);
29 |
30 | void onCancelled(UriRequest request);
31 |
32 | void onError(UriRequest request, Throwable ex, boolean isCallbackError);
33 |
34 | void onFinished(UriRequest request);
35 | }
36 |
--------------------------------------------------------------------------------
/xutils/src/main/java/org/xutils/http/app/ResponseParser.java:
--------------------------------------------------------------------------------
1 | package org.xutils.http.app;
2 |
3 |
4 | import java.lang.reflect.Type;
5 |
6 | /**
7 | * Created by wyouflf on 15/8/4.
8 | * {@link org.xutils.http.annotation.HttpResponse} 注解的返回值转换模板
9 | *
10 | * @param 支持String, byte[], JSONObject, JSONArray, InputStream
11 | */
12 | public interface ResponseParser extends RequestInterceptListener {
13 |
14 | /**
15 | * 转换result为resultType类型的对象
16 | *
17 | * @param resultType 返回值类型(可能带有泛型信息)
18 | * @param resultClass 返回值类型
19 | * @param result 网络返回数据(支持String, byte[], JSONObject, JSONArray, InputStream)
20 | * @return 请求结果, 类型为resultType
21 | */
22 | Object parse(Type resultType, Class> resultClass, ResponseDataType result) throws Throwable;
23 | }
24 |
--------------------------------------------------------------------------------
/xutils/src/main/java/org/xutils/http/body/FileBody.java:
--------------------------------------------------------------------------------
1 | package org.xutils.http.body;
2 |
3 | import android.net.Uri;
4 | import android.text.TextUtils;
5 |
6 | import org.xutils.common.util.LogUtil;
7 |
8 | import java.io.File;
9 | import java.io.FileInputStream;
10 | import java.io.IOException;
11 | import java.net.HttpURLConnection;
12 |
13 | /**
14 | * Created by wyouflf on 15/8/13.
15 | */
16 | public class FileBody extends InputStreamBody {
17 |
18 | private File file;
19 | private String contentType;
20 |
21 | public FileBody(File file) throws IOException {
22 | this(file, null);
23 | }
24 |
25 | public FileBody(File file, String contentType) throws IOException {
26 | super(new FileInputStream(file));
27 | this.file = file;
28 | this.contentType = contentType;
29 | }
30 |
31 | @Override
32 | public void setContentType(String contentType) {
33 | this.contentType = contentType;
34 | }
35 |
36 | @Override
37 | public String getContentType() {
38 | if (TextUtils.isEmpty(contentType)) {
39 | contentType = getFileContentType(file);
40 | }
41 | return contentType;
42 | }
43 |
44 | public static String getFileContentType(File file) {
45 | String filename = file.getName();
46 | String contentType = null;
47 | try {
48 | filename = Uri.encode(filename, "-![.:/,?&=]");
49 | contentType = HttpURLConnection.guessContentTypeFromName(filename);
50 | } catch (Exception e) {
51 | LogUtil.e(e.toString());
52 | }
53 | if (TextUtils.isEmpty(contentType)) {
54 | contentType = "application/octet-stream";
55 | } else {
56 | contentType = contentType.replaceFirst("\\/jpg$", "/jpeg");
57 | }
58 | return contentType;
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/xutils/src/main/java/org/xutils/http/body/InputStreamBody.java:
--------------------------------------------------------------------------------
1 | package org.xutils.http.body;
2 |
3 | import android.text.TextUtils;
4 |
5 | import org.xutils.common.Callback;
6 | import org.xutils.common.util.IOUtil;
7 | import org.xutils.common.util.LogUtil;
8 | import org.xutils.http.ProgressHandler;
9 |
10 | import java.io.ByteArrayInputStream;
11 | import java.io.FileInputStream;
12 | import java.io.IOException;
13 | import java.io.InputStream;
14 | import java.io.OutputStream;
15 |
16 |
17 | /**
18 | * Author: wyouflf
19 | * Time: 2014/05/30
20 | */
21 | public class InputStreamBody implements ProgressBody {
22 |
23 | private InputStream content;
24 | private String contentType;
25 |
26 | private final long total;
27 | private long current = 0;
28 |
29 | private ProgressHandler callBackHandler;
30 |
31 | public InputStreamBody(InputStream inputStream) {
32 | this(inputStream, null);
33 | }
34 |
35 | public InputStreamBody(InputStream inputStream, String contentType) {
36 | this.content = inputStream;
37 | this.contentType = contentType;
38 | this.total = getInputStreamLength(inputStream);
39 | }
40 |
41 | @Override
42 | public void setProgressHandler(ProgressHandler progressHandler) {
43 | this.callBackHandler = progressHandler;
44 | }
45 |
46 | @Override
47 | public long getContentLength() {
48 | return total;
49 | }
50 |
51 | @Override
52 | public void setContentType(String contentType) {
53 | this.contentType = contentType;
54 | }
55 |
56 | @Override
57 | public String getContentType() {
58 | return TextUtils.isEmpty(contentType) ? "application/octet-stream" : contentType;
59 | }
60 |
61 | @Override
62 | public void writeTo(OutputStream out) throws IOException {
63 | if (callBackHandler != null && !callBackHandler.updateProgress(total, current, true)) {
64 | throw new Callback.CancelledException("upload stopped!");
65 | }
66 |
67 | byte[] buffer = new byte[4096];
68 | try {
69 | int len = 0;
70 | while ((len = content.read(buffer)) != -1) {
71 | out.write(buffer, 0, len);
72 | current += len;
73 | if (callBackHandler != null && !callBackHandler.updateProgress(total, current, false)) {
74 | throw new Callback.CancelledException("upload stopped!");
75 | }
76 | }
77 | out.flush();
78 |
79 | if (callBackHandler != null) {
80 | callBackHandler.updateProgress(total, current, true);
81 | }
82 | } finally {
83 | IOUtil.closeQuietly(content);
84 | }
85 | }
86 |
87 | public static long getInputStreamLength(InputStream inputStream) {
88 | try {
89 | if (inputStream instanceof FileInputStream ||
90 | inputStream instanceof ByteArrayInputStream) {
91 | return inputStream.available();
92 | }
93 | } catch (Throwable ex) {
94 | LogUtil.w(ex.getMessage(), ex);
95 | }
96 | return -1L;
97 | }
98 | }
99 |
--------------------------------------------------------------------------------
/xutils/src/main/java/org/xutils/http/body/ProgressBody.java:
--------------------------------------------------------------------------------
1 | package org.xutils.http.body;
2 |
3 |
4 | import org.xutils.http.ProgressHandler;
5 |
6 | /**
7 | * Created by wyouflf on 15/8/13.
8 | */
9 | public interface ProgressBody extends RequestBody {
10 | void setProgressHandler(ProgressHandler progressHandler);
11 | }
12 |
--------------------------------------------------------------------------------
/xutils/src/main/java/org/xutils/http/body/RequestBody.java:
--------------------------------------------------------------------------------
1 | package org.xutils.http.body;
2 |
3 | import java.io.IOException;
4 | import java.io.OutputStream;
5 |
6 | /**
7 | * Created by wyouflf on 15/10/29.
8 | */
9 | public interface RequestBody {
10 |
11 | long getContentLength();
12 |
13 | void setContentType(String contentType);
14 |
15 | String getContentType();
16 |
17 | void writeTo(OutputStream out) throws IOException;
18 | }
19 |
--------------------------------------------------------------------------------
/xutils/src/main/java/org/xutils/http/body/StringBody.java:
--------------------------------------------------------------------------------
1 | package org.xutils.http.body;
2 |
3 | import android.text.TextUtils;
4 |
5 | import java.io.IOException;
6 | import java.io.OutputStream;
7 | import java.io.UnsupportedEncodingException;
8 |
9 | /**
10 | * Author: wyouflf
11 | * Time: 2014/05/30
12 | */
13 | public class StringBody implements RequestBody {
14 |
15 | private byte[] content;
16 | private String contentType;
17 | private String charset = "UTF-8";
18 |
19 | public StringBody(String str, String charset) throws UnsupportedEncodingException {
20 | if (!TextUtils.isEmpty(charset)) {
21 | this.charset = charset;
22 | }
23 | this.content = str.getBytes(this.charset);
24 | }
25 |
26 | @Override
27 | public long getContentLength() {
28 | return content.length;
29 | }
30 |
31 | @Override
32 | public void setContentType(String contentType) {
33 | this.contentType = contentType;
34 | }
35 |
36 | @Override
37 | public String getContentType() {
38 | return TextUtils.isEmpty(contentType) ? "application/json;charset=" + charset : contentType;
39 | }
40 |
41 | @Override
42 | public void writeTo(OutputStream out) throws IOException {
43 | out.write(content);
44 | out.flush();
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/xutils/src/main/java/org/xutils/http/body/UrlEncodedBody.java:
--------------------------------------------------------------------------------
1 | package org.xutils.http.body;
2 |
3 | import android.text.TextUtils;
4 |
5 | import org.xutils.common.util.KeyValue;
6 | import org.xutils.common.util.LogUtil;
7 |
8 | import java.io.IOException;
9 | import java.io.OutputStream;
10 | import java.net.URLEncoder;
11 | import java.util.List;
12 |
13 | /**
14 | * Author: wyouflf
15 | * Time: 2014/05/30
16 | */
17 | public class UrlEncodedBody implements RequestBody {
18 |
19 | private byte[] content;
20 | private String charset = "UTF-8";
21 |
22 | public UrlEncodedBody(List params, String charset) throws IOException {
23 | if (!TextUtils.isEmpty(charset)) {
24 | this.charset = charset;
25 | }
26 | StringBuilder contentSb = new StringBuilder();
27 | if (params != null) {
28 | for (KeyValue kv : params) {
29 | String name = kv.key;
30 | String value = kv.getValueStrOrNull();
31 | if (!TextUtils.isEmpty(name) && value != null) {
32 | if (contentSb.length() > 0) {
33 | contentSb.append("&");
34 | }
35 | contentSb.append(URLEncoder.encode(name, this.charset).replaceAll("\\+", "%20"))
36 | .append("=")
37 | .append(URLEncoder.encode(value, this.charset).replaceAll("\\+", "%20"));
38 | }
39 | }
40 | }
41 |
42 | this.content = contentSb.toString().getBytes(this.charset);
43 | }
44 |
45 | @Override
46 | public long getContentLength() {
47 | return content.length;
48 | }
49 |
50 | @Override
51 | public void setContentType(String contentType) {
52 | if (!TextUtils.isEmpty(contentType)) {
53 | LogUtil.w("ignored Content-Type: " + contentType);
54 | }
55 | }
56 |
57 | @Override
58 | public String getContentType() {
59 | return "application/x-www-form-urlencoded;charset=" + charset;
60 | }
61 |
62 | @Override
63 | public void writeTo(OutputStream sink) throws IOException {
64 | sink.write(this.content);
65 | sink.flush();
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/xutils/src/main/java/org/xutils/http/cookie/CookieEntity.java:
--------------------------------------------------------------------------------
1 | package org.xutils.http.cookie;
2 |
3 | import android.text.TextUtils;
4 |
5 | import org.xutils.db.annotation.Column;
6 | import org.xutils.db.annotation.Table;
7 |
8 | import java.net.HttpCookie;
9 | import java.net.URI;
10 |
11 | /**
12 | * Created by wyouflf on 15/8/20.
13 | * 数据库中的cookie实体
14 | */
15 | @Table(name = "cookie",
16 | onCreated = "CREATE UNIQUE INDEX index_cookie_unique ON cookie(\"name\",\"domain\",\"path\")")
17 | /*package*/ final class CookieEntity {
18 |
19 | // ~ 100 year
20 | private static final long MAX_EXPIRY = System.currentTimeMillis() + 1000L * 60L * 60L * 24L * 30L * 12L * 100L;
21 |
22 | @Column(name = "id", isId = true)
23 | private long id;
24 |
25 | @Column(name = "uri")
26 | private String uri; // cookie add by this uri.
27 |
28 | @Column(name = "name")
29 | private String name;
30 | @Column(name = "value")
31 | private String value;
32 | @Column(name = "comment")
33 | private String comment;
34 | @Column(name = "commentURL")
35 | private String commentURL;
36 | @Column(name = "discard")
37 | private boolean discard;
38 | @Column(name = "domain")
39 | private String domain;
40 | @Column(name = "expiry")
41 | private long expiry = MAX_EXPIRY;
42 | @Column(name = "path")
43 | private String path;
44 | @Column(name = "portList")
45 | private String portList;
46 | @Column(name = "secure")
47 | private boolean secure;
48 | @Column(name = "version")
49 | private int version = 1;
50 |
51 | public CookieEntity() {
52 | }
53 |
54 | public CookieEntity(URI uri, HttpCookie cookie) {
55 | this.uri = uri == null ? null : uri.toString();
56 | this.name = cookie.getName();
57 | this.value = cookie.getValue();
58 | this.comment = cookie.getComment();
59 | this.commentURL = cookie.getCommentURL();
60 | this.discard = cookie.getDiscard();
61 | this.domain = cookie.getDomain();
62 | long maxAge = cookie.getMaxAge();
63 | if (maxAge > 0) {
64 | this.expiry = (maxAge * 1000L) + System.currentTimeMillis();
65 | if (this.expiry < 0L) { // 计算溢出?
66 | this.expiry = MAX_EXPIRY;
67 | }
68 | } else {
69 | this.expiry = -1L;
70 | }
71 | this.path = cookie.getPath();
72 | if (!TextUtils.isEmpty(path) && path.length() > 1 && path.endsWith("/")) {
73 | this.path = path.substring(0, path.length() - 1);
74 | }
75 | this.portList = cookie.getPortlist();
76 | this.secure = cookie.getSecure();
77 | this.version = cookie.getVersion();
78 | }
79 |
80 | public HttpCookie toHttpCookie() {
81 | HttpCookie cookie = new HttpCookie(name, value);
82 | cookie.setComment(comment);
83 | cookie.setCommentURL(commentURL);
84 | cookie.setDiscard(discard);
85 | cookie.setDomain(domain);
86 | if (expiry == -1L) {
87 | cookie.setMaxAge(-1L);
88 | } else {
89 | cookie.setMaxAge((expiry - System.currentTimeMillis()) / 1000L);
90 | }
91 | cookie.setPath(path);
92 | cookie.setPortlist(portList);
93 | cookie.setSecure(secure);
94 | cookie.setVersion(version);
95 | return cookie;
96 | }
97 |
98 | public long getId() {
99 | return id;
100 | }
101 |
102 | public void setId(long id) {
103 | this.id = id;
104 | }
105 |
106 | public String getUri() {
107 | return uri;
108 | }
109 |
110 | public void setUri(String uri) {
111 | this.uri = uri;
112 | }
113 |
114 | public boolean isExpired() {
115 | return expiry != -1L && expiry < System.currentTimeMillis();
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/xutils/src/main/java/org/xutils/http/loader/BooleanLoader.java:
--------------------------------------------------------------------------------
1 | package org.xutils.http.loader;
2 |
3 | import org.xutils.cache.DiskCacheEntity;
4 | import org.xutils.http.request.UriRequest;
5 |
6 | /**
7 | * Author: wyouflf
8 | * Time: 2014/05/30
9 | */
10 | /*package*/ class BooleanLoader extends Loader {
11 |
12 | @Override
13 | public Loader newInstance() {
14 | return new BooleanLoader();
15 | }
16 |
17 | @Override
18 | public Boolean load(final UriRequest request) throws Throwable {
19 | request.sendRequest();
20 | return request.getResponseCode() < 300;
21 | }
22 |
23 | @Override
24 | public Boolean loadFromCache(final DiskCacheEntity cacheEntity) throws Throwable {
25 | return null;
26 | }
27 |
28 | @Override
29 | public void save2Cache(final UriRequest request) {
30 |
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/xutils/src/main/java/org/xutils/http/loader/ByteArrayLoader.java:
--------------------------------------------------------------------------------
1 | package org.xutils.http.loader;
2 |
3 | import org.xutils.cache.DiskCacheEntity;
4 | import org.xutils.common.util.IOUtil;
5 | import org.xutils.http.request.UriRequest;
6 |
7 | /**
8 | * Author: wyouflf
9 | * Time: 2014/05/30
10 | */
11 | /*package*/ class ByteArrayLoader extends Loader {
12 |
13 | private byte[] resultData;
14 |
15 | @Override
16 | public Loader newInstance() {
17 | return new ByteArrayLoader();
18 | }
19 |
20 | @Override
21 | public byte[] load(final UriRequest request) throws Throwable {
22 | request.sendRequest();
23 | resultData = IOUtil.readBytes(request.getInputStream());
24 | return resultData;
25 | }
26 |
27 | @Override
28 | public byte[] loadFromCache(final DiskCacheEntity cacheEntity) throws Throwable {
29 | if (cacheEntity != null) {
30 | byte[] data = cacheEntity.getBytesContent();
31 | if (data != null && data.length > 0) {
32 | return data;
33 | }
34 | }
35 | return null;
36 | }
37 |
38 | @Override
39 | public void save2Cache(final UriRequest request) {
40 | saveByteArrayCache(request, resultData);
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/xutils/src/main/java/org/xutils/http/loader/InputStreamLoader.java:
--------------------------------------------------------------------------------
1 | package org.xutils.http.loader;
2 |
3 | import org.xutils.cache.DiskCacheEntity;
4 | import org.xutils.http.request.UriRequest;
5 |
6 | import java.io.InputStream;
7 |
8 | /**
9 | * 建议配合 {@link org.xutils.common.Callback.PrepareCallback} 使用,
10 | * 将PrepareType设置为InputStream, 以便在PrepareCallback#prepare中做耗时的数据任务处理.
11 | *
12 | * Author: wyouflf
13 | * Time: 2014/05/30
14 | */
15 | /*package*/ class InputStreamLoader extends Loader {
16 |
17 | @Override
18 | public Loader newInstance() {
19 | return new InputStreamLoader();
20 | }
21 |
22 | @Override
23 | public InputStream load(final UriRequest request) throws Throwable {
24 | request.sendRequest();
25 | return request.getInputStream();
26 | }
27 |
28 | @Override
29 | public InputStream loadFromCache(final DiskCacheEntity cacheEntity) throws Throwable {
30 | return null;
31 | }
32 |
33 | @Override
34 | public void save2Cache(final UriRequest request) {
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/xutils/src/main/java/org/xutils/http/loader/IntegerLoader.java:
--------------------------------------------------------------------------------
1 | package org.xutils.http.loader;
2 |
3 | import org.xutils.cache.DiskCacheEntity;
4 | import org.xutils.http.request.UriRequest;
5 |
6 | /**
7 | * Author: wyouflf
8 | * Time: 2014/10/17
9 | */
10 | /*package*/ class IntegerLoader extends Loader {
11 | @Override
12 | public Loader newInstance() {
13 | return new IntegerLoader();
14 | }
15 |
16 | @Override
17 | public Integer load(UriRequest request) throws Throwable {
18 | request.sendRequest();
19 | return request.getResponseCode();
20 | }
21 |
22 | @Override
23 | public Integer loadFromCache(final DiskCacheEntity cacheEntity) throws Throwable {
24 | return null;
25 | }
26 |
27 | @Override
28 | public void save2Cache(UriRequest request) {
29 |
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/xutils/src/main/java/org/xutils/http/loader/JSONArrayLoader.java:
--------------------------------------------------------------------------------
1 | package org.xutils.http.loader;
2 |
3 | import android.text.TextUtils;
4 |
5 | import org.json.JSONArray;
6 | import org.xutils.cache.DiskCacheEntity;
7 | import org.xutils.common.util.IOUtil;
8 | import org.xutils.http.RequestParams;
9 | import org.xutils.http.request.UriRequest;
10 |
11 | /**
12 | * Author: wyouflf
13 | * Time: 2014/06/16
14 | */
15 | /*package*/ class JSONArrayLoader extends Loader {
16 |
17 | private String charset = "UTF-8";
18 | private String resultStr = null;
19 |
20 | @Override
21 | public Loader newInstance() {
22 | return new JSONArrayLoader();
23 | }
24 |
25 | @Override
26 | public void setParams(final RequestParams params) {
27 | if (params != null) {
28 | String charset = params.getCharset();
29 | if (!TextUtils.isEmpty(charset)) {
30 | this.charset = charset;
31 | }
32 | }
33 | }
34 |
35 | @Override
36 | public JSONArray load(final UriRequest request) throws Throwable {
37 | request.sendRequest();
38 | resultStr = IOUtil.readStr(request.getInputStream(), charset);
39 | return new JSONArray(resultStr);
40 | }
41 |
42 | @Override
43 | public JSONArray loadFromCache(final DiskCacheEntity cacheEntity) throws Throwable {
44 | if (cacheEntity != null) {
45 | String text = cacheEntity.getTextContent();
46 | if (!TextUtils.isEmpty(text)) {
47 | return new JSONArray(text);
48 | }
49 | }
50 |
51 | return null;
52 | }
53 |
54 | @Override
55 | public void save2Cache(UriRequest request) {
56 | saveStringCache(request, resultStr);
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/xutils/src/main/java/org/xutils/http/loader/JSONObjectLoader.java:
--------------------------------------------------------------------------------
1 | package org.xutils.http.loader;
2 |
3 | import android.text.TextUtils;
4 |
5 | import org.json.JSONObject;
6 | import org.xutils.cache.DiskCacheEntity;
7 | import org.xutils.common.util.IOUtil;
8 | import org.xutils.http.RequestParams;
9 | import org.xutils.http.request.UriRequest;
10 |
11 | /**
12 | * Author: wyouflf
13 | * Time: 2014/06/16
14 | */
15 | /*package*/ class JSONObjectLoader extends Loader {
16 |
17 | private String charset = "UTF-8";
18 | private String resultStr = null;
19 |
20 | @Override
21 | public Loader newInstance() {
22 | return new JSONObjectLoader();
23 | }
24 |
25 | @Override
26 | public void setParams(final RequestParams params) {
27 | if (params != null) {
28 | String charset = params.getCharset();
29 | if (!TextUtils.isEmpty(charset)) {
30 | this.charset = charset;
31 | }
32 | }
33 | }
34 |
35 | @Override
36 | public JSONObject load(final UriRequest request) throws Throwable {
37 | request.sendRequest();
38 | resultStr = IOUtil.readStr(request.getInputStream(), charset);
39 | return new JSONObject(resultStr);
40 | }
41 |
42 | @Override
43 | public JSONObject loadFromCache(final DiskCacheEntity cacheEntity) throws Throwable {
44 | if (cacheEntity != null) {
45 | String text = cacheEntity.getTextContent();
46 | if (!TextUtils.isEmpty(text)) {
47 | return new JSONObject(text);
48 | }
49 | }
50 |
51 | return null;
52 | }
53 |
54 | @Override
55 | public void save2Cache(UriRequest request) {
56 | saveStringCache(request, resultStr);
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/xutils/src/main/java/org/xutils/http/loader/Loader.java:
--------------------------------------------------------------------------------
1 | package org.xutils.http.loader;
2 |
3 |
4 | import android.text.TextUtils;
5 |
6 | import org.xutils.cache.DiskCacheEntity;
7 | import org.xutils.cache.LruDiskCache;
8 | import org.xutils.http.ProgressHandler;
9 | import org.xutils.http.RequestParams;
10 | import org.xutils.http.request.UriRequest;
11 |
12 | import java.util.Date;
13 |
14 | /**
15 | * Author: wyouflf
16 | * Time: 2014/05/26
17 | */
18 | public abstract class Loader {
19 |
20 | protected ProgressHandler progressHandler;
21 |
22 | public void setParams(final RequestParams params) {
23 | }
24 |
25 | public void setProgressHandler(final ProgressHandler callbackHandler) {
26 | this.progressHandler = callbackHandler;
27 | }
28 |
29 | protected void saveStringCache(UriRequest request, String resultStr) {
30 | saveCacheInternal(request, resultStr, null);
31 | }
32 |
33 | protected void saveByteArrayCache(UriRequest request, byte[] resultData) {
34 | saveCacheInternal(request, null, resultData);
35 | }
36 |
37 | public abstract Loader newInstance();
38 |
39 | public abstract T load(final UriRequest request) throws Throwable;
40 |
41 | public abstract T loadFromCache(final DiskCacheEntity cacheEntity) throws Throwable;
42 |
43 | public abstract void save2Cache(final UriRequest request);
44 |
45 | private void saveCacheInternal(UriRequest request, String resultStr, byte[] resultData) {
46 | if (!TextUtils.isEmpty(resultStr) || (resultData != null && resultData.length > 0)) {
47 | DiskCacheEntity entity = new DiskCacheEntity();
48 | entity.setKey(request.getCacheKey());
49 | entity.setLastAccess(System.currentTimeMillis());
50 | entity.setEtag(request.getETag());
51 | entity.setExpires(request.getExpiration());
52 | entity.setLastModify(new Date(request.getLastModified()));
53 | entity.setTextContent(resultStr);
54 | entity.setBytesContent(resultData);
55 | LruDiskCache.getDiskCache(request.getParams().getCacheDirName()).put(entity);
56 | }
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/xutils/src/main/java/org/xutils/http/loader/LoaderFactory.java:
--------------------------------------------------------------------------------
1 | package org.xutils.http.loader;
2 |
3 |
4 | import org.json.JSONArray;
5 | import org.json.JSONObject;
6 |
7 | import java.io.File;
8 | import java.io.InputStream;
9 | import java.lang.reflect.Type;
10 | import java.util.HashMap;
11 |
12 | /**
13 | * Author: wyouflf
14 | * Time: 2014/05/26
15 | */
16 | public final class LoaderFactory {
17 |
18 | private LoaderFactory() {
19 | }
20 |
21 | /**
22 | * key: loadType
23 | */
24 | private static final HashMap converterHashMap = new HashMap();
25 |
26 | static {
27 | converterHashMap.put(JSONObject.class, new JSONObjectLoader());
28 | converterHashMap.put(JSONArray.class, new JSONArrayLoader());
29 | converterHashMap.put(String.class, new StringLoader());
30 | converterHashMap.put(File.class, new FileLoader());
31 | converterHashMap.put(byte[].class, new ByteArrayLoader());
32 | converterHashMap.put(InputStream.class, new InputStreamLoader());
33 |
34 | BooleanLoader booleanLoader = new BooleanLoader();
35 | converterHashMap.put(boolean.class, booleanLoader);
36 | converterHashMap.put(Boolean.class, booleanLoader);
37 |
38 | IntegerLoader integerLoader = new IntegerLoader();
39 | converterHashMap.put(int.class, integerLoader);
40 | converterHashMap.put(Integer.class, integerLoader);
41 | }
42 |
43 | public static Loader> getLoader(Type type) {
44 | Loader> result = converterHashMap.get(type);
45 | if (result == null) {
46 | result = new ObjectLoader(type);
47 | } else {
48 | result = result.newInstance();
49 | }
50 | return result;
51 | }
52 |
53 | public static void registerLoader(Type type, Loader loader) {
54 | converterHashMap.put(type, loader);
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/xutils/src/main/java/org/xutils/http/loader/ObjectLoader.java:
--------------------------------------------------------------------------------
1 | package org.xutils.http.loader;
2 |
3 | import org.xutils.cache.DiskCacheEntity;
4 | import org.xutils.common.util.ParameterizedTypeUtil;
5 | import org.xutils.http.RequestParams;
6 | import org.xutils.http.annotation.HttpResponse;
7 | import org.xutils.http.app.ResponseParser;
8 | import org.xutils.http.request.UriRequest;
9 |
10 | import java.lang.reflect.ParameterizedType;
11 | import java.lang.reflect.Type;
12 | import java.lang.reflect.TypeVariable;
13 | import java.util.List;
14 |
15 | /**
16 | * Created by lei.jiao on 2014/6/27.
17 | * 其他对象的下载转换.
18 | * 使用类型上的@HttpResponse注解信息进行数据转换.
19 | */
20 | /*package*/ class ObjectLoader extends Loader