drake 4 anni fa
parent
commit
3c691e1997
22 ha cambiato i file con 118 aggiunte e 860 eliminazioni
  1. 11 0
      kalle/src/main/java/com/yanzhenjie/kalle/Response.java
  2. 0 5
      kalle/src/main/java/com/yanzhenjie/kalle/download/BodyDownload.java
  3. 0 46
      kalle/src/main/java/com/yanzhenjie/kalle/download/Callback.java
  4. 0 112
      kalle/src/main/java/com/yanzhenjie/kalle/download/DownloadManager.java
  5. 0 41
      kalle/src/main/java/com/yanzhenjie/kalle/download/SimpleCallback.java
  6. 0 5
      kalle/src/main/java/com/yanzhenjie/kalle/download/UrlDownload.java
  7. 0 74
      kalle/src/main/java/com/yanzhenjie/kalle/download/Work.java
  8. 3 2
      kalle/src/main/java/com/yanzhenjie/kalle/exception/ParseError.kt
  9. 7 18
      kalle/src/main/java/com/yanzhenjie/kalle/simple/BasicWorker.java
  10. 3 3
      kalle/src/main/java/com/yanzhenjie/kalle/simple/BodyWorker.java
  11. 0 69
      kalle/src/main/java/com/yanzhenjie/kalle/simple/Callback.java
  12. 8 16
      kalle/src/main/java/com/yanzhenjie/kalle/simple/Converter.kt
  13. 4 133
      kalle/src/main/java/com/yanzhenjie/kalle/simple/RequestManager.java
  14. 0 27
      kalle/src/main/java/com/yanzhenjie/kalle/simple/Result.kt
  15. 2 7
      kalle/src/main/java/com/yanzhenjie/kalle/simple/SimpleBodyRequest.java
  16. 0 55
      kalle/src/main/java/com/yanzhenjie/kalle/simple/SimpleCallback.java
  17. 2 7
      kalle/src/main/java/com/yanzhenjie/kalle/simple/SimpleUrlRequest.java
  18. 3 3
      kalle/src/main/java/com/yanzhenjie/kalle/simple/UrlWorker.java
  19. 0 74
      kalle/src/main/java/com/yanzhenjie/kalle/simple/Work.java
  20. 62 143
      net/src/main/java/com/drake/net/Net.kt
  21. 12 19
      net/src/main/java/com/drake/net/convert/DefaultConvert.kt
  22. 1 1
      sample/src/main/java/com/drake/net/sample/callback/JsonConvert.kt

+ 11 - 0
kalle/src/main/java/com/yanzhenjie/kalle/Response.java

@@ -28,6 +28,7 @@ public final class Response implements Closeable {
     private final int mCode;
     private final Headers mHeaders;
     private final ResponseBody mBody;
+    private String logBody;
 
     private Response(Builder builder) {
         this.mCode = builder.mCode;
@@ -39,6 +40,16 @@ public final class Response implements Closeable {
         return new Builder();
     }
 
+    /**
+     * 设置日志信息
+     */
+    public void setLogBody(String logBody) {
+        this.logBody = logBody;
+    }
+
+    public String getLogBody() {
+        return logBody;
+    }
 
     /**
      * Get the mCode of response.

+ 0 - 5
kalle/src/main/java/com/yanzhenjie/kalle/download/BodyDownload.java

@@ -16,7 +16,6 @@
 package com.yanzhenjie.kalle.download;
 
 import com.yanzhenjie.kalle.BodyRequest;
-import com.yanzhenjie.kalle.Canceller;
 import com.yanzhenjie.kalle.Request;
 import com.yanzhenjie.kalle.RequestMethod;
 import com.yanzhenjie.kalle.Url;
@@ -102,9 +101,5 @@ public class BodyDownload extends BodyRequest implements Download {
         public String perform() throws Exception {
             return DownloadManager.getInstance().perform(new BodyDownload(this));
         }
-
-        public Canceller perform(Callback callback) {
-            return DownloadManager.getInstance().perform(new BodyDownload(this), callback);
-        }
     }
 }

+ 0 - 46
kalle/src/main/java/com/yanzhenjie/kalle/download/Callback.java

@@ -1,46 +0,0 @@
-/*
- * Copyright (C) 2018 Drake, Inc.
- *
- * 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.
- */
-package com.yanzhenjie.kalle.download;
-
-/**
- * Created by Zhenjie Yan on 2018/3/18.
- */
-public interface Callback {
-    /**
-     * Time dimensions: The request started.
-     */
-    void onStart();
-
-    /**
-     * Result dimensions: File download completed.
-     */
-    void onFinish(String path);
-
-    /**
-     * Result dimensions: An abnormality has occurred.
-     */
-    void onException(Exception e);
-
-    /**
-     * Result dimensions: The request was cancelled.
-     */
-    void onCancel();
-
-    /**
-     * Time dimensions: The request ended.
-     */
-    void onEnd();
-}

+ 0 - 112
kalle/src/main/java/com/yanzhenjie/kalle/download/DownloadManager.java

@@ -15,22 +15,16 @@
  */
 package com.yanzhenjie.kalle.download;
 
-import com.yanzhenjie.kalle.Canceller;
-import com.yanzhenjie.kalle.Kalle;
 import com.yanzhenjie.kalle.NetCancel;
 
-import java.util.concurrent.Executor;
-
 /**
  * Created by Zhenjie Yan on 2018/3/18.
  */
 public class DownloadManager {
 
     private static DownloadManager sInstance;
-    private final Executor mExecutor;
 
     private DownloadManager() {
-        this.mExecutor = Kalle.getConfig().getWorkExecutor();
     }
 
     public static DownloadManager getInstance() {
@@ -44,26 +38,6 @@ public class DownloadManager {
         return sInstance;
     }
 
-    /**
-     * Submit a request to the queue.
-     *
-     * @param download download request.
-     * @param callback accept the result callback.
-     * @return this request corresponds to the task cancel handle.
-     */
-    public Canceller perform(final UrlDownload download, Callback callback) {
-        final Work<UrlDownload> work = new Work<>(new UrlWorker(download), new AsyncCallback(callback) {
-            @Override
-            public void onEnd() {
-                super.onEnd();
-                NetCancel.INSTANCE.remove(download.uid());
-            }
-        });
-        NetCancel.INSTANCE.add(download.uid(), work);
-        mExecutor.execute(work);
-        return work;
-    }
-
     /**
      * Execute a request.
      *
@@ -76,26 +50,6 @@ public class DownloadManager {
         return worker.call();
     }
 
-    /**
-     * Submit a request to the queue.
-     *
-     * @param download download request.
-     * @param callback accept the result callback.
-     * @return this request corresponds to the task cancel handle.
-     */
-    public Canceller perform(final BodyDownload download, Callback callback) {
-        final Work<BodyDownload> work = new Work<>(new BodyWorker(download), new AsyncCallback(callback) {
-            @Override
-            public void onEnd() {
-                super.onEnd();
-                NetCancel.INSTANCE.remove(download.uid());
-            }
-        });
-        NetCancel.INSTANCE.add(download.uid(), work);
-        mExecutor.execute(work);
-        return work;
-    }
-
     /**
      * Execute a request.
      *
@@ -107,70 +61,4 @@ public class DownloadManager {
         NetCancel.INSTANCE.add(download.uid(), worker);
         return worker.call();
     }
-
-    private static class AsyncCallback implements Callback {
-
-        private final Callback mCallback;
-        private final Executor mExecutor;
-
-        AsyncCallback(Callback callback) {
-            this.mCallback = callback;
-            this.mExecutor = Kalle.getConfig().getMainExecutor();
-        }
-
-        @Override
-        public void onStart() {
-            if (mCallback == null) return;
-            mExecutor.execute(new Runnable() {
-                @Override
-                public void run() {
-                    mCallback.onStart();
-                }
-            });
-        }
-
-        @Override
-        public void onFinish(final String path) {
-            if (mCallback == null) return;
-            mExecutor.execute(new Runnable() {
-                @Override
-                public void run() {
-                    mCallback.onFinish(path);
-                }
-            });
-        }
-
-        @Override
-        public void onException(final Exception e) {
-            if (mCallback == null) return;
-            mExecutor.execute(new Runnable() {
-                @Override
-                public void run() {
-                    mCallback.onException(e);
-                }
-            });
-        }
-
-        @Override
-        public void onCancel() {
-            if (mCallback == null) return;
-            mExecutor.execute(new Runnable() {
-                @Override
-                public void run() {
-                    mCallback.onCancel();
-                }
-            });
-        }
-
-        @Override
-        public void onEnd() {
-            if (mCallback == null) return;
-            mExecutor.execute(new Runnable() {
-                @Override
-                public void run() {
-                    mCallback.onEnd();
-                }
-            });
-        }
-    }
 }

+ 0 - 41
kalle/src/main/java/com/yanzhenjie/kalle/download/SimpleCallback.java

@@ -1,41 +0,0 @@
-/*
- * Copyright (C) 2018 Drake, Inc.
- *
- * 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.
- */
-package com.yanzhenjie.kalle.download;
-
-/**
- * Created by Zhenjie Yan on 2018/3/18.
- */
-public class SimpleCallback implements Callback {
-    @Override
-    public void onStart() {
-    }
-
-    @Override
-    public void onFinish(String path) {
-    }
-
-    @Override
-    public void onException(Exception e) {
-    }
-
-    @Override
-    public void onCancel() {
-    }
-
-    @Override
-    public void onEnd() {
-    }
-}

+ 0 - 5
kalle/src/main/java/com/yanzhenjie/kalle/download/UrlDownload.java

@@ -15,7 +15,6 @@
  */
 package com.yanzhenjie.kalle.download;
 
-import com.yanzhenjie.kalle.Canceller;
 import com.yanzhenjie.kalle.Request;
 import com.yanzhenjie.kalle.RequestMethod;
 import com.yanzhenjie.kalle.Url;
@@ -103,9 +102,5 @@ public class UrlDownload extends UrlRequest implements Download {
         public String perform() throws Exception {
             return DownloadManager.getInstance().perform(new UrlDownload(this));
         }
-
-        public Canceller perform(Callback callback) {
-            return DownloadManager.getInstance().perform(new UrlDownload(this), callback);
-        }
     }
 }

+ 0 - 74
kalle/src/main/java/com/yanzhenjie/kalle/download/Work.java

@@ -1,74 +0,0 @@
-/*
- * Copyright (C) 2018 Drake, Inc.
- *
- * 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.
- */
-package com.yanzhenjie.kalle.download;
-
-import com.yanzhenjie.kalle.Canceller;
-
-import java.util.concurrent.CancellationException;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.FutureTask;
-
-/**
- * Created by Zhenjie Yan on 2018/3/18.
- */
-public class Work<T extends Download> extends FutureTask<String> implements Canceller {
-
-    private final Callback mCallback;
-    private BasicWorker<T> mWorker;
-
-    Work(BasicWorker<T> work, Callback callback) {
-        super(work);
-        this.mWorker = work;
-        this.mCallback = callback;
-    }
-
-    @Override
-    public void run() {
-        mCallback.onStart();
-        super.run();
-    }
-
-    @Override
-    protected void done() {
-        try {
-            mCallback.onFinish(get());
-        } catch (CancellationException e) {
-            mCallback.onCancel();
-        } catch (ExecutionException e) {
-            Throwable cause = e.getCause();
-            if (isCancelled()) {
-                mCallback.onCancel();
-            } else if (cause != null && cause instanceof Exception) {
-                mCallback.onException((Exception) cause);
-            } else {
-                mCallback.onException(new Exception(cause));
-            }
-        } catch (Exception e) {
-            if (isCancelled()) {
-                mCallback.onCancel();
-            } else {
-                mCallback.onException(e);
-            }
-        }
-        mCallback.onEnd();
-    }
-
-    @Override
-    public void cancel() {
-        cancel(true);
-        mWorker.cancel();
-    }
-}

+ 3 - 2
kalle/src/main/java/com/yanzhenjie/kalle/exception/ParseError.kt

@@ -20,8 +20,9 @@ import com.yanzhenjie.kalle.Request
 /**
  * Created by Zhenjie Yan on 2018/2/27.
  */
+
 class ParseError(
     request: Request,
-    message: String = "",
+    message: String = "An exception occurred while parsing the data",
     cause: Throwable? = null
-) : NetException(request, message, cause)
+                ) : NetException(request, message, cause)

+ 7 - 18
kalle/src/main/java/com/yanzhenjie/kalle/simple/BasicWorker.java

@@ -36,11 +36,7 @@ import java.util.concurrent.Callable;
 import static com.yanzhenjie.kalle.Headers.KEY_IF_MODIFIED_SINCE;
 import static com.yanzhenjie.kalle.Headers.KEY_IF_NONE_MATCH;
 
-/**
- * Created by Zhenjie Yan on 2018/2/18.
- */
-abstract class BasicWorker<T extends SimpleRequest, Succeed, Failed>
-        implements Callable<Result<Succeed, Failed>>, Canceller {
+abstract class BasicWorker<T extends SimpleRequest, Succeed> implements Callable<Succeed>, Canceller {
 
     private static final long MAX_EXPIRES = System.currentTimeMillis() + 100L * 365L * 24L * 60L * 60L * 1000L;
 
@@ -48,18 +44,16 @@ abstract class BasicWorker<T extends SimpleRequest, Succeed, Failed>
     private final CacheStore mCacheStore;
     private final Converter mConverter;
     private final Type mSucceed;
-    private final Type mFailed;
 
-    BasicWorker(T request, Type succeed, Type failed) {
+    BasicWorker(T request, Type succeed) {
         this.mRequest = request;
         this.mSucceed = succeed;
-        this.mFailed = failed;
         this.mCacheStore = Kalle.getConfig().getCacheStore();
         this.mConverter = request.converter() == null ? Kalle.getConfig().getConverter() : request.converter();
     }
 
     @Override
-    public final Result<Succeed, Failed> call() throws Exception {
+    public final Succeed call() throws Exception {
         Response response = tryReadCacheBefore();
         if (response != null) return buildSimpleResponse(response, true);
 
@@ -292,19 +286,14 @@ abstract class BasicWorker<T extends SimpleRequest, Succeed, Failed>
                 .build();
     }
 
-    private Result<Succeed, Failed> buildSimpleResponse(Response response, boolean cache) throws IOException {
+    private Succeed buildSimpleResponse(Response response, boolean cache) throws IOException {
         Request request = mRequest.request();
         try {
-            Result<Succeed, Failed> result = new Result<>();
-
-            mConverter.convert(mSucceed, mFailed, request, response, result);
+            Succeed result = mConverter.convert(mSucceed, request, response, cache);
 
-            if (result.getSuccess() == null && result.getFailure() == null) {
-                throw new ParseError(request, mConverter.getClass().getName() + " does not process result", null);
-            }
-
-            LogRecorder.INSTANCE.recordResponse(request.logId(), String.valueOf(response.code()), response.headers().toMap(), result.getLogResponseBody());
+            LogRecorder.INSTANCE.recordResponse(request.logId(), String.valueOf(response.code()), response.headers().toMap(), response.getLogBody());
             LogRecorder.INSTANCE.recordDuration(request.logId(), System.currentTimeMillis() - request.getRequestStartTime());
+
             return result;
         } catch (NetException e) {
             LogRecorder.INSTANCE.recordException(request.logId(), e);

+ 3 - 3
kalle/src/main/java/com/yanzhenjie/kalle/simple/BodyWorker.java

@@ -24,12 +24,12 @@ import java.lang.reflect.Type;
 /**
  * Created by Zhenjie Yan on 2018/2/13.
  */
-final class BodyWorker<S, F> extends BasicWorker<SimpleBodyRequest, S, F> {
+final class BodyWorker<S> extends BasicWorker<SimpleBodyRequest, S> {
 
     private Call mCall;
 
-    BodyWorker(SimpleBodyRequest request, Type succeed, Type failed) {
-        super(request, succeed, failed);
+    BodyWorker(SimpleBodyRequest request, Type succeed) {
+        super(request, succeed);
     }
 
     @Override

+ 0 - 69
kalle/src/main/java/com/yanzhenjie/kalle/simple/Callback.java

@@ -1,69 +0,0 @@
-/*
- * Copyright (C) 2018 Drake, Inc.
- *
- * 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.
- */
-package com.yanzhenjie.kalle.simple;
-
-import java.lang.reflect.ParameterizedType;
-import java.lang.reflect.Type;
-
-/**
- * Created by Zhenjie Yan on 2018/2/17.
- */
-public abstract class Callback<Succeed, Failed> {
-
-    public Callback() {
-    }
-
-    /**
-     * Get the data type when the business was successful.
-     */
-    public Type getSucceed() {
-        Type superClass = getClass().getGenericSuperclass();
-        return ((ParameterizedType) superClass).getActualTypeArguments()[0];
-    }
-
-    /**
-     * Get the data type when the business failed.
-     */
-    public Type getFailed() {
-        Type superClass = getClass().getGenericSuperclass();
-        return ((ParameterizedType) superClass).getActualTypeArguments()[1];
-    }
-
-    /**
-     * Time dimensions: The request started.
-     */
-    public abstract void onStart();
-
-    /**
-     * Result dimensions: The request has responded.
-     */
-    public abstract void onResponse(Result<Succeed, Failed> response);
-
-    /**
-     * Result dimensions: An abnormality has occurred.
-     */
-    public abstract void onException(Exception e);
-
-    /**
-     * Result dimensions: The request was cancelled.
-     */
-    public abstract void onCancel();
-
-    /**
-     * Time dimensions: The request ended.
-     */
-    public abstract void onEnd();
-}

+ 8 - 16
kalle/src/main/java/com/yanzhenjie/kalle/simple/Converter.kt

@@ -23,31 +23,23 @@ import java.lang.reflect.Type
 interface Converter {
 
     @Throws(Exception::class)
-    fun <S, F> convert(
-        succeed: Type,
-        failed: Type,
-        request: Request,
-        response: Response,
-        result: Result<S, F>
-    )
+    fun <S> convert(succeed: Type, request: Request, response: Response, cache: Boolean): S?
 
     companion object {
 
         @JvmField
         val DEFAULT: Converter = object : Converter {
 
-            override fun <S, F> convert(
-                succeed: Type,
-                failed: Type,
-                request: Request,
-                response: Response,
-                result: Result<S, F>
-            ) {
+            override fun <S> convert(succeed: Type,
+                                     request: Request,
+                                     response: Response,
+                                     cache: Boolean): S? {
                 if (succeed === String::class.java) {
                     val string = response.body().string()
-                    result.logResponseBody = string
-                    result.success = string as S
+                    response.logBody = string
+                    return string as S
                 }
+                return null
             }
         }
     }

+ 4 - 133
kalle/src/main/java/com/yanzhenjie/kalle/simple/RequestManager.java

@@ -15,12 +15,9 @@
  */
 package com.yanzhenjie.kalle.simple;
 
-import com.yanzhenjie.kalle.Canceller;
-import com.yanzhenjie.kalle.Kalle;
 import com.yanzhenjie.kalle.NetCancel;
 
 import java.lang.reflect.Type;
-import java.util.concurrent.Executor;
 
 /**
  * Created by Zhenjie Yan on 2018/2/13.
@@ -28,10 +25,8 @@ import java.util.concurrent.Executor;
 public class RequestManager {
 
     private static RequestManager sInstance;
-    private final Executor mExecutor;
 
     private RequestManager() {
-        this.mExecutor = Kalle.getConfig().getWorkExecutor();
     }
 
     public static RequestManager getInstance() {
@@ -45,155 +40,31 @@ public class RequestManager {
         return sInstance;
     }
 
-    /**
-     * Submit a request to the queue.
-     *
-     * @param request  request.
-     * @param callback accept the result callback.
-     * @param <S>      target object parameter.
-     * @param <F>      target object parameter.
-     * @return this request corresponds to the task cancel handle.
-     */
-    public <S, F> Canceller perform(final SimpleUrlRequest request, Callback<S, F> callback) {
-        final Work<SimpleUrlRequest, S, F> work = new Work<>(new UrlWorker<S, F>(request, callback.getSucceed(), callback.getFailed()), new AsyncCallback<S, F>(callback) {
-            @Override
-            public void onEnd() {
-                super.onEnd();
-                NetCancel.INSTANCE.remove(request.uid());
-            }
-        });
-        NetCancel.INSTANCE.add(request.uid(), work);
-        mExecutor.execute(work);
-        return work;
-    }
-
     /**
      * Execute a request.
      *
      * @param request request.
      * @param succeed the data type when the business succeed.
-     * @param failed  the data type when the business failed.
      * @param <S>     target object parameter.
-     * @param <F>     target object parameter.
      * @return the response to this request.
      */
-    public <S, F> Result<S, F> perform(SimpleUrlRequest request, Type succeed, Type failed) throws Exception {
-        UrlWorker<S, F> worker = new UrlWorker<>(request, succeed, failed);
+    public <S> S perform(SimpleUrlRequest request, Type succeed) throws Exception {
+        UrlWorker<S> worker = new UrlWorker<>(request, succeed);
         NetCancel.INSTANCE.add(request.uid(), worker);
         return worker.call();
     }
 
-    /**
-     * Submit a request to the queue.
-     *
-     * @param request  request.
-     * @param callback accept the result callback.
-     * @param <S>      target object parameter.
-     * @param <F>      target object parameter.
-     * @return this request corresponds to the task cancel handle.
-     */
-    public <S, F> Canceller perform(final SimpleBodyRequest request, Callback<S, F> callback) {
-        Work<SimpleBodyRequest, S, F> work = new Work<>(new BodyWorker<S, F>(request, callback.getSucceed(), callback.getFailed()), new AsyncCallback<S, F>(callback) {
-            @Override
-            public void onEnd() {
-                super.onEnd();
-                NetCancel.INSTANCE.remove(request.uid());
-            }
-        });
-        NetCancel.INSTANCE.add(request.uid(), work);
-        mExecutor.execute(work);
-        return work;
-    }
-
     /**
      * Execute a request.
      *
      * @param request request.
      * @param succeed the data type when the business succeed.
-     * @param failed  the data type when the business failed.
      * @param <S>     target object parameter.
-     * @param <F>     target object parameter.
      * @return the response to this request.
      */
-    public <S, F> Result<S, F> perform(SimpleBodyRequest request, Type succeed, Type failed) throws Exception {
-        BodyWorker<S, F> worker = new BodyWorker<>(request, succeed, failed);
+    public <S> S perform(SimpleBodyRequest request, Type succeed) throws Exception {
+        BodyWorker<S> worker = new BodyWorker<>(request, succeed);
         NetCancel.INSTANCE.add(request.uid(), worker);
         return worker.call();
     }
-
-    private static class AsyncCallback<S, F> extends Callback<S, F> {
-
-        private final Callback<S, F> mCallback;
-        private final Executor mExecutor;
-
-        AsyncCallback(Callback<S, F> callback) {
-            this.mCallback = callback;
-            this.mExecutor = Kalle.getConfig().getMainExecutor();
-        }
-
-        @Override
-        public Type getSucceed() {
-            return mCallback.getSucceed();
-        }
-
-        @Override
-        public Type getFailed() {
-            return mCallback.getFailed();
-        }
-
-        @Override
-        public void onStart() {
-            if (mCallback == null) return;
-            mExecutor.execute(new Runnable() {
-                @Override
-                public void run() {
-                    mCallback.onStart();
-                }
-            });
-        }
-
-        @Override
-        public void onResponse(final Result<S, F> response) {
-            if (mCallback == null) return;
-            mExecutor.execute(new Runnable() {
-                @Override
-                public void run() {
-                    mCallback.onResponse(response);
-                }
-            });
-        }
-
-        @Override
-        public void onException(final Exception e) {
-            if (mCallback == null) return;
-            mExecutor.execute(new Runnable() {
-                @Override
-                public void run() {
-                    mCallback.onException(e);
-                }
-            });
-        }
-
-        @Override
-        public void onCancel() {
-            if (mCallback == null) return;
-            mExecutor.execute(new Runnable() {
-                @Override
-                public void run() {
-                    mCallback.onCancel();
-                }
-            });
-        }
-
-        @Override
-        public void onEnd() {
-            if (mCallback == null) return;
-            mExecutor.execute(new Runnable() {
-                @Override
-                public void run() {
-                    mCallback.onEnd();
-                }
-            });
-        }
-    }
 }

+ 0 - 27
kalle/src/main/java/com/yanzhenjie/kalle/simple/Result.kt

@@ -1,27 +0,0 @@
-/*
- * Copyright (C) 2018 Drake, Inc.
- *
- * 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.
- */
-
-package com.yanzhenjie.kalle.simple
-
-data class Result<S, F>(
-    var logResponseBody: String? = null,
-    var success: S? = null,
-    var failure: F? = null
-                       ) {
-
-    val isSucceed
-        get() = success != null || failure == null
-}

+ 2 - 7
kalle/src/main/java/com/yanzhenjie/kalle/simple/SimpleBodyRequest.java

@@ -18,7 +18,6 @@ package com.yanzhenjie.kalle.simple;
 import android.text.TextUtils;
 
 import com.yanzhenjie.kalle.BodyRequest;
-import com.yanzhenjie.kalle.Canceller;
 import com.yanzhenjie.kalle.Request;
 import com.yanzhenjie.kalle.RequestMethod;
 import com.yanzhenjie.kalle.Url;
@@ -95,12 +94,8 @@ public class SimpleBodyRequest extends BodyRequest implements SimpleRequest {
             return this;
         }
 
-        public <S, F> Result<S, F> perform(Type succeed, Type failed) throws Exception {
-            return RequestManager.getInstance().perform(new SimpleBodyRequest(this), succeed, failed);
-        }
-
-        public <S, F> Canceller perform(Callback<S, F> callback) {
-            return RequestManager.getInstance().perform(new SimpleBodyRequest(this), callback);
+        public <S> S perform(Type succeed) throws Exception {
+            return RequestManager.getInstance().perform(new SimpleBodyRequest(this), succeed);
         }
     }
 }

+ 0 - 55
kalle/src/main/java/com/yanzhenjie/kalle/simple/SimpleCallback.java

@@ -1,55 +0,0 @@
-/*
- * Copyright (C) 2018 Drake, Inc.
- *
- * 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.
- */
-package com.yanzhenjie.kalle.simple;
-
-import java.lang.reflect.ParameterizedType;
-import java.lang.reflect.Type;
-
-/**
- * Created by Zhenjie Yan on 2018/2/26.
- */
-public abstract class SimpleCallback<V> extends Callback<V, String> {
-
-    public SimpleCallback() {
-    }
-
-    @Override
-    public Type getSucceed() {
-        Type superClass = getClass().getGenericSuperclass();
-        return ((ParameterizedType) superClass).getActualTypeArguments()[0];
-    }
-
-    @Override
-    public Type getFailed() {
-        return String.class;
-    }
-
-    @Override
-    public void onStart() {
-    }
-
-    @Override
-    public void onException(Exception e) {
-    }
-
-    @Override
-    public void onCancel() {
-    }
-
-    @Override
-    public void onEnd() {
-    }
-}

+ 2 - 7
kalle/src/main/java/com/yanzhenjie/kalle/simple/SimpleUrlRequest.java

@@ -17,7 +17,6 @@ package com.yanzhenjie.kalle.simple;
 
 import android.text.TextUtils;
 
-import com.yanzhenjie.kalle.Canceller;
 import com.yanzhenjie.kalle.Request;
 import com.yanzhenjie.kalle.RequestMethod;
 import com.yanzhenjie.kalle.Url;
@@ -95,12 +94,8 @@ public class SimpleUrlRequest extends UrlRequest implements SimpleRequest {
             return this;
         }
 
-        public <S, F> Result<S, F> perform(Type succeed, Type failed) throws Exception {
-            return RequestManager.getInstance().perform(new SimpleUrlRequest(this), succeed, failed);
-        }
-
-        public <S, F> Canceller perform(Callback<S, F> callback) {
-            return RequestManager.getInstance().perform(new SimpleUrlRequest(this), callback);
+        public <S> S perform(Type succeed) throws Exception {
+            return RequestManager.getInstance().perform(new SimpleUrlRequest(this), succeed);
         }
     }
 }

+ 3 - 3
kalle/src/main/java/com/yanzhenjie/kalle/simple/UrlWorker.java

@@ -24,12 +24,12 @@ import java.lang.reflect.Type;
 /**
  * Created by Zhenjie Yan on 2018/2/13.
  */
-final class UrlWorker<S, F> extends BasicWorker<SimpleUrlRequest, S, F> {
+final class UrlWorker<S> extends BasicWorker<SimpleUrlRequest, S> {
 
     private Call mCall;
 
-    UrlWorker(SimpleUrlRequest request, Type succeed, Type failed) {
-        super(request, succeed, failed);
+    UrlWorker(SimpleUrlRequest request, Type succeed) {
+        super(request, succeed);
     }
 
     @Override

+ 0 - 74
kalle/src/main/java/com/yanzhenjie/kalle/simple/Work.java

@@ -1,74 +0,0 @@
-/*
- * Copyright (C) 2018 Drake, Inc.
- *
- * 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.
- */
-package com.yanzhenjie.kalle.simple;
-
-import com.yanzhenjie.kalle.Canceller;
-
-import java.util.concurrent.CancellationException;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.FutureTask;
-
-/**
- * Created by Zhenjie Yan on 2018/2/13.
- */
-final class Work<T extends SimpleRequest, S, F> extends FutureTask<Result<S, F>> implements Canceller {
-
-    private final Callback<S, F> mCallback;
-    private BasicWorker<T, S, F> mWorker;
-
-    Work(BasicWorker<T, S, F> work, Callback<S, F> callback) {
-        super(work);
-        this.mWorker = work;
-        this.mCallback = callback;
-    }
-
-    @Override
-    public void run() {
-        mCallback.onStart();
-        super.run();
-    }
-
-    @Override
-    protected void done() {
-        try {
-            mCallback.onResponse(get());
-        } catch (CancellationException e) {
-            mCallback.onCancel();
-        } catch (ExecutionException e) {
-            Throwable cause = e.getCause();
-            if (isCancelled()) {
-                mCallback.onCancel();
-            } else if (cause != null && cause instanceof Exception) {
-                mCallback.onException((Exception) cause);
-            } else {
-                mCallback.onException(new Exception(cause));
-            }
-        } catch (Exception e) {
-            if (isCancelled()) {
-                mCallback.onCancel();
-            } else {
-                mCallback.onException(e);
-            }
-        }
-        mCallback.onEnd();
-    }
-
-    @Override
-    public void cancel() {
-        cancel(true);
-        mWorker.cancel();
-    }
-}

+ 62 - 143
net/src/main/java/com/drake/net/Net.kt

@@ -20,7 +20,6 @@ package com.drake.net
 
 import android.content.Context
 import com.bumptech.glide.Glide
-import com.drake.net.error.ResponseException
 import com.yanzhenjie.kalle.NetCancel
 import com.yanzhenjie.kalle.RequestMethod
 import com.yanzhenjie.kalle.Url
@@ -58,19 +57,16 @@ inline fun <reified M> CoroutineScope.Get(
     val realPath = if (absolutePath) path else (NetConfig.host + path)
 
     val request = SimpleUrlRequest.newApi(Url.newBuilder(realPath).build(), RequestMethod.GET)
-        .tag(tag)
-        .uid(uid)
-        .cacheKey(path)
-        .cacheMode(cache)
+            .tag(tag)
+            .uid(uid)
+            .cacheKey(path)
+            .cacheMode(cache)
 
-    val response = try {
-        request.apply(block)
-            .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
+    try {
+        request.apply(block).perform<M>(M::class.java)
     } catch (e: SocketException) {
         throw CancellationException()
     }
-
-    if (response.isSucceed) response.success!! else throw response.failure!!
 }
 
 
@@ -96,20 +92,17 @@ inline fun <reified M> CoroutineScope.Post(
     val realPath = if (absolutePath) path else (NetConfig.host + path)
 
     val request = SimpleBodyRequest.newApi(Url.newBuilder(realPath).build(), RequestMethod.POST)
-        .tag(tag)
-        .uid(uid)
-        .cacheKey(path)
-        .cacheMode(cache)
-
-        val response = try {
-            request.apply(block)
-                .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
-        } catch (e: SocketException) {
-            throw CancellationException()
-        }
+            .tag(tag)
+            .uid(uid)
+            .cacheKey(path)
+            .cacheMode(cache)
 
-        if (response.isSucceed) response.success!! else throw response.failure!!
+    try {
+        request.apply(block).perform<M>(M::class.java)
+    } catch (e: SocketException) {
+        throw CancellationException()
     }
+}
 
 
 /**
@@ -133,19 +126,16 @@ inline fun <reified M> CoroutineScope.Head(
     val realPath = if (absolutePath) path else (NetConfig.host + path)
 
     val request = SimpleUrlRequest.newApi(Url.newBuilder(realPath).build(), RequestMethod.HEAD)
-        .tag(tag)
-        .uid(uid)
-        .cacheKey(path)
-        .cacheMode(cache)
+            .tag(tag)
+            .uid(uid)
+            .cacheKey(path)
+            .cacheMode(cache)
 
-    val response = try {
-        request.apply(block)
-            .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
+    try {
+        request.apply(block).perform<M>(M::class.java)
     } catch (e: SocketException) {
         throw CancellationException()
     }
-
-    if (response.isSucceed) response.success!! else throw response.failure!!
 }
 
 /**
@@ -169,19 +159,16 @@ inline fun <reified M> CoroutineScope.Options(
     val realPath = if (absolutePath) path else (NetConfig.host + path)
 
     val request = SimpleUrlRequest.newApi(Url.newBuilder(realPath).build(), RequestMethod.OPTIONS)
-        .tag(tag)
-        .uid(uid)
-        .cacheKey(path)
-        .cacheMode(cache)
+            .tag(tag)
+            .uid(uid)
+            .cacheKey(path)
+            .cacheMode(cache)
 
-    val response = try {
-        request.apply(block)
-            .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
+    try {
+        request.apply(block).perform<M>(M::class.java)
     } catch (e: SocketException) {
         throw CancellationException()
     }
-
-    if (response.isSucceed) response.success!! else throw response.failure!!
 }
 
 /**
@@ -205,19 +192,16 @@ inline fun <reified M> CoroutineScope.Trace(
     val realPath = if (absolutePath) path else (NetConfig.host + path)
 
     val request = SimpleUrlRequest.newApi(Url.newBuilder(realPath).build(), RequestMethod.TRACE)
-        .tag(tag)
-        .uid(uid)
-        .cacheKey(path)
-        .cacheMode(cache)
+            .tag(tag)
+            .uid(uid)
+            .cacheKey(path)
+            .cacheMode(cache)
 
-    val response = try {
-        request.apply(block)
-            .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
+    try {
+        request.apply(block).perform<M>(M::class.java)
     } catch (e: SocketException) {
         throw CancellationException()
     }
-
-    if (response.isSucceed) response.success!! else throw response.failure!!
 }
 
 /**
@@ -241,19 +225,16 @@ inline fun <reified M> CoroutineScope.Delete(
     val realPath = if (absolutePath) path else (NetConfig.host + path)
 
     val request = SimpleBodyRequest.newApi(Url.newBuilder(realPath).build(), RequestMethod.DELETE)
-        .tag(tag)
-        .uid(uid)
-        .cacheKey(path)
-        .cacheMode(cache)
+            .tag(tag)
+            .uid(uid)
+            .cacheKey(path)
+            .cacheMode(cache)
 
-    val response = try {
-        request.apply(block)
-            .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
+    try {
+        request.apply(block).perform<M>(M::class.java)
     } catch (e: SocketException) {
         throw CancellationException()
     }
-
-    if (response.isSucceed) response.success!! else throw response.failure!!
 }
 
 /**
@@ -277,22 +258,19 @@ inline fun <reified M> CoroutineScope.Put(
     val realPath = if (absolutePath) path else (NetConfig.host + path)
 
     val request = SimpleBodyRequest.newApi(
-        Url.newBuilder(realPath).build(),
-        RequestMethod.PUT
-    )
-        .tag(tag)
-        .uid(uid)
-        .cacheKey(path)
-        .cacheMode(cache)
+            Url.newBuilder(realPath).build(),
+            RequestMethod.PUT
+                                          )
+            .tag(tag)
+            .uid(uid)
+            .cacheKey(path)
+            .cacheMode(cache)
 
-    val response = try {
-        request.apply(block)
-            .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
+    try {
+        request.apply(block).perform<M>(M::class.java)
     } catch (e: SocketException) {
         throw CancellationException()
     }
-
-    if (response.isSucceed) response.success!! else throw response.failure!!
 }
 
 /**
@@ -316,19 +294,16 @@ inline fun <reified M> CoroutineScope.Patch(
     val realPath = if (absolutePath) path else (NetConfig.host + path)
 
     val request = SimpleBodyRequest.newApi(Url.newBuilder(realPath).build(), RequestMethod.PATCH)
-        .tag(tag)
-        .uid(uid)
-        .cacheKey(path)
-        .cacheMode(cache)
+            .tag(tag)
+            .uid(uid)
+            .cacheKey(path)
+            .cacheMode(cache)
 
-    val response = try {
-        request.apply(block)
-            .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
+    try {
+        request.apply(block).perform<M>(M::class.java)
     } catch (e: SocketException) {
         throw CancellationException()
     }
-
-    if (response.isSucceed) response.success!! else throw response.failure!!
 }
 
 /**
@@ -461,14 +436,7 @@ inline fun <reified M> syncGet(
         .cacheKey(path)
         .cacheMode(cache)
 
-    val response = request.apply(block)
-        .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
-
-    return if (response.isSucceed) {
-        response.success!!
-    } else {
-        throw response.failure!!
-    }
+    return request.apply(block).perform(M::class.java)
 }
 
 /**
@@ -496,14 +464,7 @@ inline fun <reified M> syncPost(
         .cacheKey(path)
         .cacheMode(cache)
 
-    val response = request.apply(block)
-        .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
-
-    return if (response.isSucceed) {
-        response.success!!
-    } else {
-        throw response.failure!!
-    }
+    return request.apply(block).perform(M::class.java)
 }
 
 /**
@@ -531,14 +492,7 @@ inline fun <reified M> syncHead(
         .cacheKey(path)
         .cacheMode(cache)
 
-    val response = request.apply(block)
-        .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
-
-    return if (response.isSucceed) {
-        response.success!!
-    } else {
-        throw response.failure!!
-    }
+    return request.apply(block).perform(M::class.java)
 }
 
 /**
@@ -566,14 +520,7 @@ inline fun <reified M> syncOptions(
         .cacheKey(path)
         .cacheMode(cache)
 
-    val response = request.apply(block)
-        .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
-
-    return if (response.isSucceed) {
-        response.success!!
-    } else {
-        throw response.failure!!
-    }
+    return request.apply(block).perform(M::class.java)
 }
 
 /**
@@ -601,14 +548,7 @@ inline fun <reified M> syncTrace(
         .cacheKey(path)
         .cacheMode(cache)
 
-    val response = request.apply(block)
-        .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
-
-    return if (response.isSucceed) {
-        response.success!!
-    } else {
-        throw response.failure!!
-    }
+    return request.apply(block).perform(M::class.java)
 }
 
 /**
@@ -636,14 +576,7 @@ inline fun <reified M> syncDelete(
         .cacheKey(path)
         .cacheMode(cache)
 
-    val response = request.apply(block)
-        .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
-
-    return if (response.isSucceed) {
-        response.success!!
-    } else {
-        throw response.failure!!
-    }
+    return request.apply(block).perform(M::class.java)
 }
 
 /**
@@ -671,14 +604,7 @@ inline fun <reified M> syncPut(
         .cacheKey(path)
         .cacheMode(cache)
 
-    val response = request.apply(block)
-        .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
-
-    return if (response.isSucceed) {
-        response.success!!
-    } else {
-        throw response.failure!!
-    }
+    return request.apply(block).perform(M::class.java)
 }
 
 /**
@@ -706,14 +632,7 @@ inline fun <reified M> syncPatch(
         .cacheKey(path)
         .cacheMode(cache)
 
-    val response = request.apply(block)
-        .perform<M, ResponseException>(M::class.java, ResponseException::class.java)
-
-    return if (response.isSucceed) {
-        response.success!!
-    } else {
-        throw response.failure!!
-    }
+    return request.apply(block).perform(M::class.java)
 }
 
 /**

+ 12 - 19
net/src/main/java/com/drake/net/convert/DefaultConvert.kt

@@ -23,8 +23,8 @@ import com.drake.net.error.ResponseException
 import com.drake.net.error.ServerResponseException
 import com.yanzhenjie.kalle.Request
 import com.yanzhenjie.kalle.Response
+import com.yanzhenjie.kalle.exception.ParseError
 import com.yanzhenjie.kalle.simple.Converter
-import com.yanzhenjie.kalle.simple.Result
 import org.json.JSONObject
 import java.lang.reflect.Type
 
@@ -37,36 +37,29 @@ import java.lang.reflect.Type
  * @param message  错误信息在JSON中的字段名
  */
 @Suppress("UNCHECKED_CAST")
-abstract class DefaultConvert(
-    val success: String = "0",
-    val code: String = "code",
-    val message: String = "msg"
-) : Converter {
+abstract class DefaultConvert(val success: String = "0",
+                              val code: String = "code",
+                              val message: String = "msg") : Converter {
 
-    override fun <S, F> convert(
-        succeed: Type,
-        failed: Type,
-        request: Request,
-        response: Response,
-        result: Result<S, F>
-    ) {
+    override fun <S> convert(succeed: Type,
+                             request: Request,
+                             response: Response,
+                             cache: Boolean): S? {
         val body = response.body().string()
-        result.logResponseBody = body // 日志记录响应信息
+        response.logBody = body  // 日志记录响应信息
         val code = response.code()
-
         when {
             code in 200..299 -> { // 请求成功
                 val jsonObject = JSONObject(body) // 获取JSON中后端定义的错误码和错误信息
                 if (jsonObject.getString(this.code) == success) { // 对比后端自定义错误码
-                    result.success =
-                            if (succeed === String::class.java) body as S else body.parseBody(succeed)
+                    return if (succeed === String::class.java) body as S else body.parseBody(succeed)
                 } else { // 错误码匹配失败, 开始写入错误异常
-                    result.failure =
-                        ResponseException(code, jsonObject.getString(message), request) as F
+                    throw ResponseException(code, jsonObject.getString(message), request)
                 }
             }
             code in 400..499 -> throw RequestParamsException(code, request) // 请求参数错误
             code >= 500 -> throw ServerResponseException(code, request) // 服务器异常错误
+            else -> throw ParseError(request)
         }
     }
 

+ 1 - 1
sample/src/main/java/com/drake/net/sample/callback/JsonConvert.kt

@@ -20,7 +20,7 @@ import com.drake.net.convert.DefaultConvert
 import com.squareup.moshi.Moshi
 import java.lang.reflect.Type
 
-class JsonConvert : DefaultConvert(code = "code", message = "msg", success = "200") {
+class JsonConvert : DefaultConvert(code = "code", message = "msg", success = "400") {
 
     override fun <S> String.parseBody(succeed: Type): S? {
         return Moshi.Builder().build().adapter<S>(succeed).fromJson(this)