Pārlūkot izejas kodu

+ Convert增加Request参数便于在转换器中定位接口和判断
+ LogCat中所有异常都能追踪到位置
+ onError中可以通过异常对象拿到request对象, 用于收集网络错误信息时定位请求信息

drake 5 gadi atpakaļ
vecāks
revīzija
561968b2f3
37 mainītis faili ar 278 papildinājumiem un 376 dzēšanām
  1. 1 0
      README.md
  2. 2 1
      build.gradle
  3. 9 0
      kalle/build.gradle
  4. 9 9
      kalle/src/main/java/com/yanzhenjie/kalle/connect/http/ConnectInterceptor.java
  5. 1 1
      kalle/src/main/java/com/yanzhenjie/kalle/download/BasicWorker.java
  6. 0 36
      kalle/src/main/java/com/yanzhenjie/kalle/exception/ConnectException.java
  7. 27 0
      kalle/src/main/java/com/yanzhenjie/kalle/exception/ConnectException.kt
  8. 8 10
      kalle/src/main/java/com/yanzhenjie/kalle/exception/ConnectTimeoutError.kt
  9. 0 47
      kalle/src/main/java/com/yanzhenjie/kalle/exception/DownloadError.java
  10. 29 0
      kalle/src/main/java/com/yanzhenjie/kalle/exception/DownloadError.kt
  11. 8 10
      kalle/src/main/java/com/yanzhenjie/kalle/exception/HostError.kt
  12. 18 0
      kalle/src/main/java/com/yanzhenjie/kalle/exception/NetException.kt
  13. 7 10
      kalle/src/main/java/com/yanzhenjie/kalle/exception/NetworkError.kt
  14. 7 10
      kalle/src/main/java/com/yanzhenjie/kalle/exception/NoCacheError.kt
  15. 8 10
      kalle/src/main/java/com/yanzhenjie/kalle/exception/ParseError.kt
  16. 0 36
      kalle/src/main/java/com/yanzhenjie/kalle/exception/ReadException.java
  17. 27 0
      kalle/src/main/java/com/yanzhenjie/kalle/exception/ReadException.kt
  18. 8 10
      kalle/src/main/java/com/yanzhenjie/kalle/exception/ReadTimeoutError.kt
  19. 8 10
      kalle/src/main/java/com/yanzhenjie/kalle/exception/URLError.kt
  20. 0 35
      kalle/src/main/java/com/yanzhenjie/kalle/exception/WriteException.java
  21. 27 0
      kalle/src/main/java/com/yanzhenjie/kalle/exception/WriteException.kt
  22. 2 2
      kalle/src/main/java/com/yanzhenjie/kalle/simple/BasicWorker.java
  23. 2 2
      net/build.gradle
  24. 16 24
      net/src/main/java/com/drake/net/Net.kt
  25. 4 3
      net/src/main/java/com/drake/net/convert/DefaultConvert.kt
  26. 5 3
      net/src/main/java/com/drake/net/error/RequestParamsException.kt
  27. 6 3
      net/src/main/java/com/drake/net/error/ResponseException.kt
  28. 5 3
      net/src/main/java/com/drake/net/error/ServerResponseException.kt
  29. 0 5
      net/src/main/java/com/drake/net/scope/AndroidScope.kt
  30. 1 0
      net/src/main/java/com/drake/net/scope/NetCoroutineScope.kt
  31. 4 47
      net/src/main/java/com/drake/net/scope/PageCoroutineScope.kt
  32. 1 2
      net/src/main/java/com/drake/net/scope/RefreshCoroutineScope.kt
  33. 3 7
      net/src/main/java/com/drake/net/scope/StateCoroutineScope.kt
  34. 12 12
      net/src/main/java/com/drake/net/time/Interval.kt
  35. 10 10
      net/src/main/java/com/drake/net/utils/ScopeUtils.kt
  36. 1 1
      sample/build.gradle
  37. 2 17
      sample/src/main/java/com/drake/net/sample/MainActivity.kt

+ 1 - 0
README.md

@@ -34,6 +34,7 @@ The project is supported by [JetBrains](https://www.jetbrains.com/), Best IDE to
 - 自动处理加载对话框
 - 协程作用域支持错误和结束回调
 - 支持先强制读取缓存后网络请求二次刷新
+- 附带超强轮循器
 
 
 

+ 2 - 1
build.gradle

@@ -1,10 +1,11 @@
 // Top-level build file where you can add configuration options common to all sub-projects/modules.
 
 buildscript {
+    ext.kotlin_version = '1.3.61'
 
     ext {
         kotlin_version = '1.3.61'
-        brv_version = '1.2.5'
+        brv_version = '1.2.7'
         coroutine_version = '1.3.0'
         glide_version = '4.9.0'
     }

+ 9 - 0
kalle/build.gradle

@@ -1,4 +1,6 @@
 apply plugin: 'com.android.library'
+apply plugin: 'kotlin-android-extensions'
+apply plugin: 'kotlin-android'
 
 apply plugin: 'com.github.dcendents.android-maven'
 group='com.github.liangjingkanji'
@@ -11,4 +13,11 @@ android {
         minSdkVersion 9
         targetSdkVersion 28
     }
+}
+dependencies {
+    implementation "androidx.core:core-ktx:+"
+    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
+}
+repositories {
+    mavenCentral()
 }

+ 9 - 9
kalle/src/main/java/com/yanzhenjie/kalle/connect/http/ConnectInterceptor.java

@@ -112,7 +112,7 @@ class ConnectInterceptor implements Interceptor {
      */
     private Connection connect(Request request) throws ConnectException {
         if (!mNetwork.isAvailable())
-            throw new NetworkError(String.format("Network Unavailable: %1$s.", request.location()));
+            throw new NetworkError(request, "Network Unavailable: ");
 
         try {
             Headers headers = request.headers();
@@ -123,15 +123,15 @@ class ConnectInterceptor implements Interceptor {
             headers.set(KEY_HOST, uri.getHost());
             return mFactory.connect(request);
         } catch (URISyntaxException e) {
-            throw new URLError(String.format("The url syntax error: %1$s.", request.location()), e);
+            throw new URLError(request, "The url syntax error: ", e);
         } catch (MalformedURLException e) {
-            throw new URLError(String.format("The url is malformed: %1$s.", request.location()), e);
+            throw new URLError(request, "The url is malformed: ", e);
         } catch (UnknownHostException e) {
-            throw new HostError(String.format("Hostname can not be resolved: %1$s.", request.location()), e);
+            throw new HostError(request, "Hostname can not be resolved: ", e);
         } catch (SocketTimeoutException e) {
-            throw new ConnectTimeoutError(String.format("Connect time out: %1$s.", request.location()), e);
+            throw new ConnectTimeoutError(request, "Connect time out: ", e);
         } catch (Exception e) {
-            throw new ConnectException(String.format("An unknown exception: %1$s.", request.location()), e);
+            throw new ConnectException(request, "An unknown exception: ", e);
         }
     }
 
@@ -141,7 +141,7 @@ class ConnectInterceptor implements Interceptor {
             request.body().writeTo(IOUtils.toBufferedOutputStream(stream));
             IOUtils.closeQuietly(stream);
         } catch (Exception e) {
-            throw new WriteException(request.location(), e);
+            throw new WriteException(request, null, e);
         }
     }
 
@@ -158,9 +158,9 @@ class ConnectInterceptor implements Interceptor {
             ResponseBody body = new StreamBody(contentType, mConnection.getInputStream());
             return Response.newBuilder().code(code).headers(headers).body(body).build();
         } catch (SocketTimeoutException e) {
-            throw new ReadTimeoutError(String.format("Read data time out: %1$s.", request.location()), e);
+            throw new ReadTimeoutError(request, "Read data time out: ", e);
         } catch (Exception e) {
-            throw new ReadException(request.location(), e);
+            throw new ReadException(request, null, e);
         }
     }
 

+ 1 - 1
kalle/src/main/java/com/yanzhenjie/kalle/download/BasicWorker.java

@@ -90,7 +90,7 @@ public abstract class BasicWorker<T extends Download> implements Callable<String
             }
 
             if (!mPolicy.allowDownload(code, comeHeaders)) {
-                throw new DownloadError(code, comeHeaders, "The download policy prohibits the program from continuing to download: " + mDownload.request().location());
+                throw new DownloadError(code, comeHeaders, mDownload.request(), "The download policy prohibits the program from continuing to download: ");
             }
 
             File file = new File(mDirectory, mFileName);

+ 0 - 36
kalle/src/main/java/com/yanzhenjie/kalle/exception/ConnectException.java

@@ -1,36 +0,0 @@
-/*
- * Copyright © 2018 Zhenjie Yan.
- *
- * 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.exception;
-
-import java.io.IOException;
-
-/**
- * Created by Zhenjie Yan on 2018/2/13.
- */
-public class ConnectException extends IOException {
-
-    public ConnectException(String message) {
-        super(message);
-    }
-
-    public ConnectException(String message, Throwable cause) {
-        super(message, cause);
-    }
-
-    public ConnectException(Throwable cause) {
-        super(cause);
-    }
-}

+ 27 - 0
kalle/src/main/java/com/yanzhenjie/kalle/exception/ConnectException.kt

@@ -0,0 +1,27 @@
+/*
+ * Copyright © 2018 Zhenjie Yan.
+ *
+ * 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.exception
+
+import com.yanzhenjie.kalle.Request
+
+/**
+ * Created by Zhenjie Yan on 2018/2/13.
+ */
+open class ConnectException(
+    request: Request,
+    message: String? = null,
+    cause: Throwable? = null
+) : NetException(request, message, cause)

+ 8 - 10
kalle/src/main/java/com/yanzhenjie/kalle/exception/URLError.java → kalle/src/main/java/com/yanzhenjie/kalle/exception/ConnectTimeoutError.kt

@@ -13,17 +13,15 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package com.yanzhenjie.kalle.exception;
+package com.yanzhenjie.kalle.exception
+
+import com.yanzhenjie.kalle.Request
 
 /**
  * Created by Zhenjie Yan on 2018/2/22.
  */
-public class URLError extends ConnectException {
-    public URLError(String message) {
-        super(message);
-    }
-
-    public URLError(String message, Throwable cause) {
-        super(message, cause);
-    }
-}
+class ConnectTimeoutError(
+    request: Request,
+    message: String? = null,
+    cause: Throwable? = null
+) : ConnectException(request, message, cause)

+ 0 - 47
kalle/src/main/java/com/yanzhenjie/kalle/exception/DownloadError.java

@@ -1,47 +0,0 @@
-/*
- * Copyright © 2018 Zhenjie Yan.
- *
- * 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.exception;
-
-import com.yanzhenjie.kalle.Headers;
-
-/**
- * Created by Zhenjie Yan on 2018/3/18.
- */
-public class DownloadError extends ReadException {
-
-    private int mCode;
-    private Headers mHeaders;
-
-    public DownloadError(int code, Headers headers, String message) {
-        super(message);
-        this.mCode = code;
-        this.mHeaders = headers;
-    }
-
-    public DownloadError(int code, Headers headers, Throwable cause) {
-        super(cause);
-        this.mCode = code;
-        this.mHeaders = headers;
-    }
-
-    public int getCode() {
-        return mCode;
-    }
-
-    public Headers getHeaders() {
-        return mHeaders;
-    }
-}

+ 29 - 0
kalle/src/main/java/com/yanzhenjie/kalle/exception/DownloadError.kt

@@ -0,0 +1,29 @@
+/*
+ * Copyright © 2018 Zhenjie Yan.
+ *
+ * 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.exception
+
+import com.yanzhenjie.kalle.Headers
+import com.yanzhenjie.kalle.Request
+
+/**
+ * Created by Zhenjie Yan on 2018/3/18.
+ */
+class DownloadError(
+    code: Int,
+    headers: Headers,
+    request: Request,
+    message: String? = null
+) : ReadException(request, message)

+ 8 - 10
kalle/src/main/java/com/yanzhenjie/kalle/exception/HostError.java → kalle/src/main/java/com/yanzhenjie/kalle/exception/HostError.kt

@@ -13,17 +13,15 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package com.yanzhenjie.kalle.exception;
+package com.yanzhenjie.kalle.exception
+
+import com.yanzhenjie.kalle.Request
 
 /**
  * Created by Zhenjie Yan on 2018/2/22.
  */
-public class HostError extends ConnectException {
-    public HostError(String message) {
-        super(message);
-    }
-
-    public HostError(String message, Throwable cause) {
-        super(message, cause);
-    }
-}
+class HostError(
+    request: Request,
+    message: String? = null,
+    cause: Throwable? = null
+) : ConnectException(request, message, cause)

+ 18 - 0
kalle/src/main/java/com/yanzhenjie/kalle/exception/NetException.kt

@@ -0,0 +1,18 @@
+/*
+ * Copyright (C) 2018, Umbrella CompanyLimited All rights reserved.
+ * Project:Net
+ * Author:Drake
+ * Date:1/13/20 1:23 PM
+ */
+
+package com.yanzhenjie.kalle.exception
+
+import com.yanzhenjie.kalle.Request
+import java.io.IOException
+
+
+open class NetException(
+    val request: Request,
+    message: String? = null,
+    cause: Throwable? = null
+) : IOException(message + request.location(), cause)

+ 7 - 10
kalle/src/main/java/com/yanzhenjie/kalle/exception/NetworkError.java → kalle/src/main/java/com/yanzhenjie/kalle/exception/NetworkError.kt

@@ -13,17 +13,14 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package com.yanzhenjie.kalle.exception;
+package com.yanzhenjie.kalle.exception
+
+import com.yanzhenjie.kalle.Request
 
 /**
  * Created by Zhenjie Yan on 2018/2/22.
  */
-public class NetworkError extends ConnectException {
-    public NetworkError(String message) {
-        super(message);
-    }
-
-    public NetworkError(String message, Throwable cause) {
-        super(message, cause);
-    }
-}
+class NetworkError(
+    request: Request,
+    message: String? = null
+) : ConnectException(request, message)

+ 7 - 10
kalle/src/main/java/com/yanzhenjie/kalle/exception/NoCacheError.java → kalle/src/main/java/com/yanzhenjie/kalle/exception/NoCacheError.kt

@@ -13,17 +13,14 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package com.yanzhenjie.kalle.exception;
+package com.yanzhenjie.kalle.exception
+
+import com.yanzhenjie.kalle.Request
 
 /**
  * Created by Zhenjie Yan on 2018/2/26.
  */
-public class NoCacheError extends ReadException {
-    public NoCacheError(String message) {
-        super(message);
-    }
-
-    public NoCacheError(String message, Throwable cause) {
-        super(message, cause);
-    }
-}
+class NoCacheError(
+    request: Request,
+    message: String? = null
+) : ReadException(request, message)

+ 8 - 10
kalle/src/main/java/com/yanzhenjie/kalle/exception/ParseError.java → kalle/src/main/java/com/yanzhenjie/kalle/exception/ParseError.kt

@@ -13,17 +13,15 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package com.yanzhenjie.kalle.exception;
+package com.yanzhenjie.kalle.exception
+
+import com.yanzhenjie.kalle.Request
 
 /**
  * Created by Zhenjie Yan on 2018/2/27.
  */
-public class ParseError extends ReadException {
-    public ParseError(String message) {
-        super(message);
-    }
-
-    public ParseError(String message, Throwable cause) {
-        super(message, cause);
-    }
-}
+class ParseError(
+    request: Request,
+    message: String? = null,
+    cause: Throwable? = null
+) : ReadException(request, message, cause)

+ 0 - 36
kalle/src/main/java/com/yanzhenjie/kalle/exception/ReadException.java

@@ -1,36 +0,0 @@
-/*
- * Copyright © 2018 Zhenjie Yan.
- *
- * 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.exception;
-
-import java.io.IOException;
-
-/**
- * Created by Zhenjie Yan on 2018/2/13.
- */
-public class ReadException extends IOException {
-
-    public ReadException(String message) {
-        super(message);
-    }
-
-    public ReadException(String message, Throwable cause) {
-        super(message, cause);
-    }
-
-    public ReadException(Throwable cause) {
-        super(cause);
-    }
-}

+ 27 - 0
kalle/src/main/java/com/yanzhenjie/kalle/exception/ReadException.kt

@@ -0,0 +1,27 @@
+/*
+ * Copyright © 2018 Zhenjie Yan.
+ *
+ * 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.exception
+
+import com.yanzhenjie.kalle.Request
+
+/**
+ * Created by Zhenjie Yan on 2018/2/13.
+ */
+open class ReadException(
+    request: Request,
+    message: String? = null,
+    cause: Throwable? = null
+) : NetException(request, message, cause)

+ 8 - 10
kalle/src/main/java/com/yanzhenjie/kalle/exception/ReadTimeoutError.java → kalle/src/main/java/com/yanzhenjie/kalle/exception/ReadTimeoutError.kt

@@ -13,17 +13,15 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package com.yanzhenjie.kalle.exception;
+package com.yanzhenjie.kalle.exception
+
+import com.yanzhenjie.kalle.Request
 
 /**
  * Created by Zhenjie Yan on 2018/2/24.
  */
-public class ReadTimeoutError extends ReadException {
-    public ReadTimeoutError(String message) {
-        super(message);
-    }
-
-    public ReadTimeoutError(String message, Throwable cause) {
-        super(message, cause);
-    }
-}
+class ReadTimeoutError(
+    request: Request,
+    message: String? = null,
+    cause: Throwable? = null
+) : ReadException(request, message, cause)

+ 8 - 10
kalle/src/main/java/com/yanzhenjie/kalle/exception/ConnectTimeoutError.java → kalle/src/main/java/com/yanzhenjie/kalle/exception/URLError.kt

@@ -13,17 +13,15 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package com.yanzhenjie.kalle.exception;
+package com.yanzhenjie.kalle.exception
+
+import com.yanzhenjie.kalle.Request
 
 /**
  * Created by Zhenjie Yan on 2018/2/22.
  */
-public class ConnectTimeoutError extends ConnectException {
-    public ConnectTimeoutError(String message) {
-        super(message);
-    }
-
-    public ConnectTimeoutError(String message, Throwable cause) {
-        super(message, cause);
-    }
-}
+class URLError(
+    request: Request,
+    message: String? = null,
+    cause: Throwable? = null
+) : ConnectException(request, message, cause)

+ 0 - 35
kalle/src/main/java/com/yanzhenjie/kalle/exception/WriteException.java

@@ -1,35 +0,0 @@
-/*
- * Copyright © 2018 Zhenjie Yan.
- *
- * 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.exception;
-
-import java.io.IOException;
-
-/**
- * Created by Zhenjie Yan on 2018/2/24.
- */
-public class WriteException extends IOException {
-    public WriteException(String message) {
-        super(message);
-    }
-
-    public WriteException(String message, Throwable cause) {
-        super(message, cause);
-    }
-
-    public WriteException(Throwable cause) {
-        super(cause);
-    }
-}

+ 27 - 0
kalle/src/main/java/com/yanzhenjie/kalle/exception/WriteException.kt

@@ -0,0 +1,27 @@
+/*
+ * Copyright © 2018 Zhenjie Yan.
+ *
+ * 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.exception
+
+import com.yanzhenjie.kalle.Request
+
+/**
+ * Created by Zhenjie Yan on 2018/2/24.
+ */
+class WriteException constructor(
+    request: Request,
+    message: String? = null,
+    cause: Throwable? = null
+) : NetException(request, message, cause)

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

@@ -133,7 +133,7 @@ abstract class BasicWorker<T extends SimpleRequest, Succeed, Failed>
                 if (cache != null) {
                     return buildResponse(cache.getCode(), cache.getHeaders(), cache.getBody());
                 }
-                throw new NoCacheError("No cache found: " + mRequest.request().location());
+                throw new NoCacheError(mRequest.request(), "No cache found: ");
             }
             case READ_CACHE_NO_THEN_NETWORK:
             case READ_CACHE_NO_THEN_HTTP: {
@@ -290,7 +290,7 @@ abstract class BasicWorker<T extends SimpleRequest, Succeed, Failed>
         } catch (IOException e) {
             throw e;
         } catch (Exception e) {
-            throw new ParseError("An exception occurred while parsing the data: " + mRequest.request().location(), e);
+            throw new ParseError(mRequest.request(), "An exception occurred while parsing the data: ", e);
         }
     }
 }

+ 2 - 2
net/build.gradle

@@ -35,7 +35,7 @@ dependencies {
     api fileTree(dir: "libs", include: ["*.jar"])
     implementation "androidx.appcompat:appcompat:1.1.0"
     implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
-    testImplementation "junit:junit:4.12"
+    testImplementation "junit:junit:4.13"
     androidTestImplementation "androidx.test:runner:1.2.0"
     androidTestImplementation "androidx.test.espresso:espresso-core:3.2.0"
 
@@ -43,7 +43,7 @@ dependencies {
     api project(path: ':kalle')
     api 'com.squareup.okhttp3:okhttp-urlconnection:3.12.3'
 
-    implementation "com.github.liangjingkanji:Tooltip:1.0.3"
+    implementation "com.github.liangjingkanji:Tooltip:1.0.4"
 
     compileOnly "com.github.liangjingkanji:BRV:$brv_version"
     compileOnly "com.github.bumptech.glide:glide:$glide_version"

+ 16 - 24
net/src/main/java/com/drake/net/Net.kt

@@ -46,19 +46,15 @@ inline fun <reified M> CoroutineScope.get(
         .cacheMode(cache)
 
     val response = if (block == null) {
-        request.perform(M::class.java, String::class.java)
+        request.perform(M::class.java, ResponseException::class.java)
     } else {
-        request.apply(block).perform<M, String>(M::class.java, String::class.java)
+        request.apply(block).perform<M, String>(M::class.java, ResponseException::class.java)
     }
 
-    if (isActive) {
-        if (response.isSucceed) {
-            response.succeed()
-        } else {
-            throw ResponseException(response.code(), response.failed())
-        }
+    if (response.isSucceed) {
+        response.succeed()
     } else {
-        throw CancellationException()
+        throw response.failed() as ResponseException
     }
 }
 
@@ -78,19 +74,15 @@ inline fun <reified M> CoroutineScope.post(
             .cacheMode(cache)
 
     val response = if (block == null) {
-        request.perform<M, String>(M::class.java, String::class.java)
+        request.perform<M, String>(M::class.java, ResponseException::class.java)
     } else {
-        request.apply(block).perform<M, String>(M::class.java, String::class.java)
+        request.apply(block).perform<M, String>(M::class.java, ResponseException::class.java)
     }
 
-    if (isActive) {
-        if (response.isSucceed) {
-            response.succeed()
-        } else {
-            throw ResponseException(response.code(), response.failed())
-        }
+    if (response.isSucceed) {
+        response.succeed()
     } else {
-        throw CancellationException()
+        throw response.failed() as ResponseException
     }
 }
 
@@ -173,15 +165,15 @@ inline fun <reified M> syncGet(
         Kalle.get(if (absolutePath) path else (NetConfig.host + path)).tag(tag).cacheKey(path)
             .cacheMode(cache)
     val response = if (block == null) {
-        request.perform(M::class.java, String::class.java)
+        request.perform(M::class.java, ResponseException::class.java)
     } else {
-        request.apply(block).perform<M, String>(M::class.java, String::class.java)
+        request.apply(block).perform<M, String>(M::class.java, ResponseException::class.java)
     }
 
     return if (response.isSucceed) {
         response.succeed()
     } else {
-        throw ResponseException(response.code(), response.failed())
+        throw response.failed() as ResponseException
     }
 }
 
@@ -197,15 +189,15 @@ inline fun <reified M> syncPost(
         Kalle.post(if (absolutePath) path else (NetConfig.host + path)).tag(tag).cacheKey(path)
             .cacheMode(cache)
     val response = if (block == null) {
-        request.perform<M, String>(M::class.java, String::class.java)
+        request.perform<M, String>(M::class.java, ResponseException::class.java)
     } else {
-        request.apply(block).perform<M, String>(M::class.java, String::class.java)
+        request.apply(block).perform<M, String>(M::class.java, ResponseException::class.java)
     }
 
     return if (response.isSucceed) {
         response.succeed()
     } else {
-        throw ResponseException(response.code(), response.failed())
+        throw response.failed() as ResponseException
     }
 }
 

+ 4 - 3
net/src/main/java/com/drake/net/convert/DefaultConvert.kt

@@ -10,6 +10,7 @@
 package com.drake.net.convert
 
 import com.drake.net.error.RequestParamsException
+import com.drake.net.error.ResponseException
 import com.drake.net.error.ServerResponseException
 import com.yanzhenjie.kalle.Request
 import com.yanzhenjie.kalle.Response
@@ -57,12 +58,12 @@ abstract class DefaultConvert(
                     succeedData = if (succeed === String::class.java) body as S
                     else convert(succeed, body)
                 } else {
-                    failedData = jsonObject.getString(msg) as F
+                    failedData = ResponseException(code, jsonObject.getString(msg), request) as F
                     code = responseCode.toInt()
                 }
             }
-            code in 400..499 -> throw RequestParamsException(code)
-            code >= 500 -> throw ServerResponseException(code)
+            code in 400..499 -> throw RequestParamsException(code, request)
+            code >= 500 -> throw ServerResponseException(code, request)
         }
 
         return SimpleResponse.newBuilder<S, F>().code(code)

+ 5 - 3
net/src/main/java/com/drake/net/error/RequestParamsException.kt

@@ -7,11 +7,13 @@
 
 package com.drake.net.error
 
+import com.yanzhenjie.kalle.Request
+import com.yanzhenjie.kalle.exception.NetException
+
 /**
  * 404
  */
 class RequestParamsException(
     val code: Int,
-    val msg: String = "",
-    val tag: Any? = null
-) : Throwable("$code: $msg")
+    request: Request
+) : NetException(request, code.toString())

+ 6 - 3
net/src/main/java/com/drake/net/error/ResponseException.kt

@@ -7,6 +7,9 @@
 
 package com.drake.net.error
 
+import com.yanzhenjie.kalle.Request
+import com.yanzhenjie.kalle.exception.NetException
+
 /**
  *  对应网络请求后台定义的错误信息
  * @param msg 网络请求错误信息
@@ -15,6 +18,6 @@ package com.drake.net.error
  */
 class ResponseException(
     val code: Int,
-    val msg: String = "",
-    val tag: Any? = null
-) : Throwable("$code: $msg")
+    val msg: String,
+    request: Request
+) : NetException(request, "$code $msg")

+ 5 - 3
net/src/main/java/com/drake/net/error/ServerResponseException.kt

@@ -7,11 +7,13 @@
 
 package com.drake.net.error
 
+import com.yanzhenjie.kalle.Request
+import com.yanzhenjie.kalle.exception.NetException
+
 /**
  * 500
  */
 class ServerResponseException(
     val code: Int,
-    val msg: String = "",
-    val tag: Any? = null
-) : Throwable("$code: $msg")
+    request: Request
+) : NetException(request, code.toString())

+ 0 - 5
net/src/main/java/com/drake/net/scope/AndroidScope.kt

@@ -36,7 +36,6 @@ open class AndroidScope(
 
     protected var catch: (AndroidScope.(Throwable) -> Unit)? = null
     protected var finally: (AndroidScope.(Throwable?) -> Unit)? = null
-    protected var auto = true
 
     private val exceptionHandler = CoroutineExceptionHandler { _, throwable ->
         catch(throwable)
@@ -90,9 +89,5 @@ open class AndroidScope(
         e.printStackTrace()
     }
 
-    fun autoOff() {
-        auto = false
-    }
-
 }
 

+ 1 - 0
net/src/main/java/com/drake/net/scope/NetCoroutineScope.kt

@@ -93,6 +93,7 @@ open class NetCoroutineScope() : AndroidScope() {
      */
     fun cache(
         error: Boolean = false,
+        animate: Boolean = true,
         onCache: suspend CoroutineScope.() -> Unit
     ): AndroidScope {
         this.error = error

+ 4 - 47
net/src/main/java/com/drake/net/scope/PageCoroutineScope.kt

@@ -8,7 +8,6 @@
 package com.drake.net.scope
 
 import android.view.View
-import com.drake.brv.BindingAdapter
 import com.drake.brv.PageRefreshLayout
 import com.drake.net.NetConfig
 import com.drake.net.R
@@ -38,6 +37,7 @@ class PageCoroutineScope(
             readCache = false
             cacheSucceed = it
         }
+        page.trigger()
     }
 
     override fun readCache(succeed: Boolean) {
@@ -51,7 +51,7 @@ class PageCoroutineScope(
         super.catch(e)
 
         if (cacheSucceed) {
-            finish(false)
+            page.finish(false)
         } else {
             page.showError(e)
         }
@@ -59,8 +59,9 @@ class PageCoroutineScope(
 
     override fun finally(e: Throwable?) {
         super.finally(e)
-        if (e == null && auto) {
+        if (e == null) {
             page.showContent()
+            page.trigger()
         }
     }
 
@@ -72,48 +73,4 @@ class PageCoroutineScope(
         }
     }
 
-    /**
-     * 自动判断是添加数据还是覆盖数据, 以及数据为空或者NULL时[showEmpty]
-     *
-     * @param hasMore 如果不传数据, 默认已加载完全部 (建议此时可以关闭[PageRefreshLayout]的加载更多功能)
-     */
-    fun addData(
-        data: List<Any?>?,
-        bindingAdapter: BindingAdapter? = null,
-        hasMore: BindingAdapter.() -> Boolean = { false }
-    ) {
-        page.addData(data, bindingAdapter, hasMore)
-        autoOff()
-    }
-
-    /**
-     * 显示空缺省页
-     */
-    fun showEmpty(tag: Any? = null) {
-        page.showEmpty(tag)
-        autoOff()
-    }
-
-    /**
-     * 显示内容缺省页
-     *
-     * 一般情况会自动执行不需要手动调用
-     */
-    fun showContent() {
-        page.showContent()
-        autoOff()
-    }
-
-    /**
-     * 结束刷新或者加载
-     *
-     * 一般情况会自动执行不需要手动调用
-     *
-     * @param success 是否成功
-     */
-    fun finish(success: Boolean = true) {
-        page.finish(success)
-        autoOff()
-    }
-
 }

+ 1 - 2
net/src/main/java/com/drake/net/scope/RefreshCoroutineScope.kt

@@ -41,7 +41,6 @@ class RefreshCoroutineScope(
     override fun readCache(succeed: Boolean) {
         refresh.finishRefresh()
         refresh.setTag(R.id.cache_succeed, false)
-        autoOff()
     }
 
     override fun catch(e: Throwable) {
@@ -51,7 +50,7 @@ class RefreshCoroutineScope(
 
     override fun finally(e: Throwable?) {
         super.finally(e)
-        if (e == null && auto) {
+        if (e == null) {
             refresh.finishRefresh(true)
         }
     }

+ 3 - 7
net/src/main/java/com/drake/net/scope/StateCoroutineScope.kt

@@ -44,6 +44,7 @@ class StateCoroutineScope(val state: StateLayout) : NetCoroutineScope() {
             readCache = false
             cacheSucceed = it
         }
+        state.trigger()
     }
 
     override fun readCache(succeed: Boolean) {
@@ -70,15 +71,10 @@ class StateCoroutineScope(val state: StateLayout) : NetCoroutineScope() {
 
     override fun finally(e: Throwable?) {
         super.finally(e)
-        if (e == null && auto) {
+        if (e == null) {
             state.showContent()
+            state.trigger()
         }
     }
 
-
-    fun showEmpty(tag: Any? = null) {
-        state.showEmpty(tag)
-        autoOff()
-    }
-
 }

+ 12 - 12
net/src/main/java/com/drake/net/time/Interval.kt

@@ -60,14 +60,14 @@ class Interval(
 
     private val receiveList: MutableList<(Long) -> Unit> = mutableListOf()
     private val finishList: MutableList<(Long) -> Unit> = mutableListOf()
-    private var _state = IntervalState.STATE_IDLE
     private var countTime = 0L
     private var delay = 0L
     private var scope: AndroidScope? = null
     private lateinit var ticker: ReceiveChannel<Unit>
 
     var count = start
-    val state get() = _state
+    var state = IntervalState.STATE_IDLE
+        private set
 
     // <editor-fold desc="回调">
 
@@ -95,10 +95,10 @@ class Interval(
      * 开始
      */
     fun start() {
-        if (_state == IntervalState.STATE_ACTIVE || _state == IntervalState.STATE_PAUSE) {
+        if (state == IntervalState.STATE_ACTIVE || state == IntervalState.STATE_PAUSE) {
             return
         }
-        _state = IntervalState.STATE_ACTIVE
+        state = IntervalState.STATE_ACTIVE
         launch()
     }
 
@@ -107,8 +107,8 @@ class Interval(
      * 停止
      */
     fun stop() {
-        if (_state == IntervalState.STATE_IDLE) return
-        _state = IntervalState.STATE_IDLE
+        if (state == IntervalState.STATE_IDLE) return
+        state = IntervalState.STATE_IDLE
         scope?.cancel()
         finishList.forEach {
             it.invoke(count)
@@ -131,8 +131,8 @@ class Interval(
      * 继续
      */
     fun resume() {
-        if (_state != IntervalState.STATE_PAUSE) return
-        _state = IntervalState.STATE_ACTIVE
+        if (state != IntervalState.STATE_PAUSE) return
+        state = IntervalState.STATE_ACTIVE
         launch(delay)
     }
 
@@ -140,8 +140,8 @@ class Interval(
      * 暂停
      */
     fun pause() {
-        if (_state != IntervalState.STATE_ACTIVE) return
-        _state = IntervalState.STATE_PAUSE
+        if (state != IntervalState.STATE_ACTIVE) return
+        state = IntervalState.STATE_PAUSE
         delay = System.currentTimeMillis() - countTime
         scope?.cancel()
     }
@@ -150,11 +150,11 @@ class Interval(
      * 重置
      */
     fun reset() {
-        if (_state == IntervalState.STATE_IDLE) return
+        if (state == IntervalState.STATE_IDLE) return
         count = start
         scope?.cancel()
         delay = unit.toMillis(initialDelay)
-        if (_state == IntervalState.STATE_ACTIVE) launch()
+        if (state == IntervalState.STATE_ACTIVE) launch()
     }
 
     /**

+ 10 - 10
net/src/main/java/com/drake/net/utils/ScopeUtils.kt

@@ -40,14 +40,14 @@ fun FragmentActivity.scopeDialog(
     dialog: Dialog? = null,
     cancelable: Boolean = true, block: suspend CoroutineScope.() -> Unit
 ): NetCoroutineScope {
-    return DialogCoroutineScope(this, dialog, cancelable).launch(block = block)
+    return DialogCoroutineScope(this, dialog, cancelable).launch(block)
 }
 
 fun Fragment.scopeDialog(
     dialog: Dialog? = null,
     cancelable: Boolean = true, block: suspend CoroutineScope.() -> Unit
 ): NetCoroutineScope {
-    return DialogCoroutineScope(activity!!, dialog, cancelable).launch(block = block)
+    return DialogCoroutineScope(activity!!, dialog, cancelable).launch(block)
 }
 
 // </editor-fold>
@@ -64,9 +64,9 @@ fun Fragment.scopeDialog(
  *
  * 布局被销毁或者界面关闭作用域被取消
  */
-fun StateLayout.scope(block: suspend CoroutineScope.(StateCoroutineScope) -> Unit): NetCoroutineScope {
+fun StateLayout.scope(block: suspend CoroutineScope.() -> Unit): NetCoroutineScope {
     val scope = StateCoroutineScope(this)
-    scope.launch { block(scope) }
+    scope.launch(block)
     return scope
 }
 
@@ -100,9 +100,9 @@ fun SmartRefreshLayout.scopeRefresh(
  *
  * 布局被销毁或者界面关闭作用域被取消
  */
-fun PageRefreshLayout.scope(block: suspend CoroutineScope.(PageCoroutineScope) -> Unit): PageCoroutineScope {
+fun PageRefreshLayout.scope(block: suspend CoroutineScope.() -> Unit): PageCoroutineScope {
     val scope = PageCoroutineScope(this)
-    scope.launch { block(scope) }
+    scope.launch(block)
     return scope
 }
 
@@ -112,14 +112,14 @@ fun PageRefreshLayout.scope(block: suspend CoroutineScope.(PageCoroutineScope) -
  * 该作用域生命周期跟随整个应用, 注意内存泄漏
  */
 fun scope(block: suspend CoroutineScope.() -> Unit): AndroidScope {
-    return AndroidScope().launch(block = block)
+    return AndroidScope().launch(block)
 }
 
 fun LifecycleOwner.scopeLife(
     lifeEvent: Lifecycle.Event = Lifecycle.Event.ON_DESTROY,
     block: suspend CoroutineScope.() -> Unit
 ): AndroidScope {
-    return AndroidScope(this, lifeEvent).launch(block = block)
+    return AndroidScope(this, lifeEvent).launch(block)
 }
 
 /**
@@ -129,7 +129,7 @@ fun LifecycleOwner.scopeLife(
  * 该作用域生命周期跟随整个应用, 注意内存泄漏
  */
 fun scopeNet(block: suspend CoroutineScope.() -> Unit): NetCoroutineScope {
-    return NetCoroutineScope().launch(block = block)
+    return NetCoroutineScope().launch(block)
 }
 
 
@@ -137,7 +137,7 @@ fun LifecycleOwner.scopeNetLife(
     lifeEvent: Lifecycle.Event = Lifecycle.Event.ON_DESTROY,
     block: suspend CoroutineScope.() -> Unit
 ): NetCoroutineScope {
-    return NetCoroutineScope(this, lifeEvent).launch(block = block)
+    return NetCoroutineScope(this, lifeEvent).launch(block)
 }
 
 

+ 1 - 1
sample/build.gradle

@@ -32,7 +32,7 @@ dependencies {
     implementation "androidx.appcompat:appcompat:1.1.0"
     implementation "androidx.core:core-ktx:1.1.0"
     implementation "androidx.constraintlayout:constraintlayout:1.1.3"
-    testImplementation "junit:junit:4.12"
+    testImplementation "junit:junit:4.13"
     androidTestImplementation "androidx.test:runner:1.2.0"
     androidTestImplementation "androidx.test.espresso:espresso-core:3.2.0"
 

+ 2 - 17
sample/src/main/java/com/drake/net/sample/MainActivity.kt

@@ -1,7 +1,6 @@
 package com.drake.net.sample
 
 import android.os.Bundle
-import android.util.Log
 import androidx.appcompat.app.AppCompatActivity
 import com.drake.net.get
 import com.drake.net.utils.scope
@@ -20,30 +19,16 @@ class MainActivity : AppCompatActivity() {
 
             scope {
 
-                Log.d("日志", "(MainActivity.kt:50)    网络")
-
-                val data = get<String>(
-                    "https://raw.githusercontent.com/liangjingkanji/BRV/master/README.md",
-                    cache = CacheMode.NETWORK_YES_THEN_WRITE_CACHE,
-                    absolutePath = true
-                )
-
-                textView.text = data.await()
-
-            }.cache(true) {
-
-                Log.d("日志", "(MainActivity.kt:57)    缓存")
-
                 val data = get<String>(
                     "https://raw.githubusercontent.com/liangjingkanji/BRV/master/README.md",
-                    cache = CacheMode.READ_CACHE,
+                    cache = CacheMode.NETWORK_YES_THEN_WRITE_CACHE,
                     absolutePath = true
                 )
 
                 textView.text = data.await()
             }
 
-        }.autoRefresh()
+        }.showLoading()
     }
 }