Browse Source

| 修复异常提示顺序问题
| DefaultConvert
| BUG修复

drake 5 years ago
parent
commit
a6f553e3e7
26 changed files with 164 additions and 262 deletions
  1. 1 1
      README.md
  2. 3 2
      kalle/src/main/java/com/yanzhenjie/kalle/connect/http/ConnectInterceptor.java
  3. 1 1
      kalle/src/main/java/com/yanzhenjie/kalle/exception/ConnectTimeoutError.kt
  4. 1 1
      kalle/src/main/java/com/yanzhenjie/kalle/exception/DownloadError.kt
  5. 1 1
      kalle/src/main/java/com/yanzhenjie/kalle/exception/HostError.kt
  6. 1 1
      kalle/src/main/java/com/yanzhenjie/kalle/exception/NetworkError.kt
  7. 1 1
      kalle/src/main/java/com/yanzhenjie/kalle/exception/NoCacheError.kt
  8. 1 1
      kalle/src/main/java/com/yanzhenjie/kalle/exception/ParseError.kt
  9. 1 1
      kalle/src/main/java/com/yanzhenjie/kalle/exception/ReadTimeoutError.kt
  10. 1 1
      kalle/src/main/java/com/yanzhenjie/kalle/exception/URLError.kt
  11. 15 5
      kalle/src/main/java/com/yanzhenjie/kalle/simple/BasicWorker.java
  12. 1 1
      kalle/src/main/java/com/yanzhenjie/kalle/simple/Callback.java
  13. 0 60
      kalle/src/main/java/com/yanzhenjie/kalle/simple/Converter.java
  14. 46 0
      kalle/src/main/java/com/yanzhenjie/kalle/simple/Converter.kt
  15. 3 3
      kalle/src/main/java/com/yanzhenjie/kalle/simple/RequestManager.java
  16. 23 0
      kalle/src/main/java/com/yanzhenjie/kalle/simple/Result.kt
  17. 1 1
      kalle/src/main/java/com/yanzhenjie/kalle/simple/SimpleBodyRequest.java
  18. 0 127
      kalle/src/main/java/com/yanzhenjie/kalle/simple/SimpleResponse.java
  19. 1 1
      kalle/src/main/java/com/yanzhenjie/kalle/simple/SimpleUrlRequest.java
  20. 1 1
      kalle/src/main/java/com/yanzhenjie/kalle/simple/Work.java
  21. 8 8
      net/src/main/java/com/drake/net/Net.kt
  22. 5 5
      net/src/main/java/com/drake/net/NetConfig.kt
  23. 23 23
      net/src/main/java/com/drake/net/convert/DefaultConvert.kt
  24. 4 15
      sample/src/main/java/com/drake/net/sample/App.kt
  25. 19 0
      sample/src/main/java/com/drake/net/sample/JsonConvert.kt
  26. 2 1
      sample/src/main/java/com/drake/net/sample/MainActivity.kt

+ 1 - 1
README.md

@@ -83,7 +83,7 @@ implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0'
 // 支持自动下拉刷新和缺省页的, 可选, 刷新头和上拉加载参考SmartRefreshLayout
 implementation 'com.github.liangjingkanji:BRV:1.2.1'
 
-implementation 'com.github.liangjingkanji:Net:2.1.0'
+implementation 'com.github.liangjingkanji:Net:2.1.1'
 ```
 
 

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

@@ -31,6 +31,7 @@ import com.yanzhenjie.kalle.cookie.CookieManager;
 import com.yanzhenjie.kalle.exception.ConnectException;
 import com.yanzhenjie.kalle.exception.ConnectTimeoutError;
 import com.yanzhenjie.kalle.exception.HostError;
+import com.yanzhenjie.kalle.exception.NetException;
 import com.yanzhenjie.kalle.exception.NetworkError;
 import com.yanzhenjie.kalle.exception.ReadException;
 import com.yanzhenjie.kalle.exception.ReadTimeoutError;
@@ -110,7 +111,7 @@ class ConnectInterceptor implements Interceptor {
      * @return connection between client and server.
      * @throws ConnectException anomalies that occurred during the connection.
      */
-    private Connection connect(Request request) throws ConnectException {
+    private Connection connect(Request request) throws NetException {
         if (!mNetwork.isAvailable())
             throw new NetworkError(request, "Network Unavailable: ");
 
@@ -145,7 +146,7 @@ class ConnectInterceptor implements Interceptor {
         }
     }
 
-    private Response readResponse(Request request) throws ReadException {
+    private Response readResponse(Request request) throws NetException {
         try {
             int code = mConnection.getCode();
             Headers headers = parseResponseHeaders(mConnection.getHeaders());

+ 1 - 1
kalle/src/main/java/com/yanzhenjie/kalle/exception/ConnectTimeoutError.kt

@@ -24,4 +24,4 @@ class ConnectTimeoutError(
     request: Request,
     message: String? = null,
     cause: Throwable? = null
-) : ConnectException(request, message, cause)
+) : NetException(request, message, cause)

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

@@ -26,4 +26,4 @@ class DownloadError(
     headers: Headers,
     request: Request,
     message: String? = null
-) : ReadException(request, message)
+) : NetException(request, message)

+ 1 - 1
kalle/src/main/java/com/yanzhenjie/kalle/exception/HostError.kt

@@ -24,4 +24,4 @@ class HostError(
     request: Request,
     message: String? = null,
     cause: Throwable? = null
-) : ConnectException(request, message, cause)
+) : NetException(request, message, cause)

+ 1 - 1
kalle/src/main/java/com/yanzhenjie/kalle/exception/NetworkError.kt

@@ -23,4 +23,4 @@ import com.yanzhenjie.kalle.Request
 class NetworkError(
     request: Request,
     message: String? = null
-) : ConnectException(request, message)
+) : NetException(request, message)

+ 1 - 1
kalle/src/main/java/com/yanzhenjie/kalle/exception/NoCacheError.kt

@@ -23,4 +23,4 @@ import com.yanzhenjie.kalle.Request
 class NoCacheError(
     request: Request,
     message: String? = null
-) : ReadException(request, message)
+) : NetException(request, message)

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

@@ -24,4 +24,4 @@ class ParseError(
     request: Request,
     message: String? = null,
     cause: Throwable? = null
-) : ReadException(request, message, cause)
+) : NetException(request, message, cause)

+ 1 - 1
kalle/src/main/java/com/yanzhenjie/kalle/exception/ReadTimeoutError.kt

@@ -24,4 +24,4 @@ class ReadTimeoutError(
     request: Request,
     message: String? = null,
     cause: Throwable? = null
-) : ReadException(request, message, cause)
+) : NetException(request, message, cause)

+ 1 - 1
kalle/src/main/java/com/yanzhenjie/kalle/exception/URLError.kt

@@ -24,4 +24,4 @@ class URLError(
     request: Request,
     message: String? = null,
     cause: Throwable? = null
-) : ConnectException(request, message, cause)
+) : NetException(request, message, cause)

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

@@ -18,6 +18,7 @@ package com.yanzhenjie.kalle.simple;
 import com.yanzhenjie.kalle.Headers;
 import com.yanzhenjie.kalle.Kalle;
 import com.yanzhenjie.kalle.Response;
+import com.yanzhenjie.kalle.exception.NetException;
 import com.yanzhenjie.kalle.exception.NoCacheError;
 import com.yanzhenjie.kalle.exception.ParseError;
 import com.yanzhenjie.kalle.simple.cache.Cache;
@@ -36,7 +37,7 @@ 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<SimpleResponse<Succeed, Failed>> {
+        implements Callable<Result<Succeed, Failed>> {
 
     private static final long MAX_EXPIRES = System.currentTimeMillis() + 100L * 365L * 24L * 60L * 60L * 1000L;
 
@@ -55,7 +56,7 @@ abstract class BasicWorker<T extends SimpleRequest, Succeed, Failed>
     }
 
     @Override
-    public final SimpleResponse<Succeed, Failed> call() throws Exception {
+    public final Result<Succeed, Failed> call() throws Exception {
         Response response = tryReadCacheBefore();
         if (response != null) return buildSimpleResponse(response, true);
 
@@ -284,10 +285,19 @@ abstract class BasicWorker<T extends SimpleRequest, Succeed, Failed>
                 .build();
     }
 
-    private SimpleResponse<Succeed, Failed> buildSimpleResponse(Response response, boolean cache) throws IOException {
+    private Result<Succeed, Failed> buildSimpleResponse(Response response, boolean cache) throws IOException {
         try {
-            return mConverter.convert(mSucceed, mFailed, mRequest.request(), response, cache);
-        } catch (IOException e) {
+
+            Result<Succeed, Failed> result = new Result<>(response.code(), response.headers(), cache, null, null);
+
+            mConverter.convert(mSucceed, mFailed, mRequest.request(), response, result);
+
+            if (result.getSuccess() == null && result.getFailure() == null) {
+                throw new ParseError(mRequest.request(), mConverter.getClass().getName() + " does not process result: ", null);
+            }
+
+            return result;
+        } catch (NetException e) {
             throw e;
         } catch (Exception e) {
             throw new ParseError(mRequest.request(), "An exception occurred while parsing the data: ", e);

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

@@ -50,7 +50,7 @@ public abstract class Callback<Succeed, Failed> {
     /**
      * Result dimensions: The request has responded.
      */
-    public abstract void onResponse(SimpleResponse<Succeed, Failed> response);
+    public abstract void onResponse(Result<Succeed, Failed> response);
 
     /**
      * Result dimensions: An abnormality has occurred.

+ 0 - 60
kalle/src/main/java/com/yanzhenjie/kalle/simple/Converter.java

@@ -1,60 +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.simple;
-
-import com.yanzhenjie.kalle.Request;
-import com.yanzhenjie.kalle.Response;
-
-import java.lang.reflect.Type;
-
-/**
- * Created by Zhenjie Yan on 2018/2/12.
- */
-public interface Converter {
-
-    /**
-     * Default converter.
-     */
-    Converter DEFAULT = new Converter() {
-        @Override
-        public <S, F> SimpleResponse<S, F> convert(Type succeed, Type failed, Request request, Response response, boolean fromCache) throws Exception {
-            S succeedData = null;
-
-            if (succeed == String.class) succeedData = (S) response.body().string();
-
-            return SimpleResponse.<S, F>newBuilder()
-                    .code(response.code())
-                    .headers(response.headers())
-                    .fromCache(fromCache)
-                    .succeed(succeedData)
-                    .build();
-        }
-    };
-
-    /**
-     * Convert data to the result of the target type.
-     *
-     * @param succeed   the data type when the business succeed.
-     * @param failed    the data type when the business failed.
-     * @param response  response of request.
-     * @param fromCache the response is from the cache.
-     * @param <S>       the data type.
-     * @param <F>       the data type.
-     * @return {@link SimpleResponse}
-     * @throws Exception to prevent accidents.
-     */
-    <S, F> SimpleResponse<S, F> convert(Type succeed, Type failed, Request request, Response response, boolean fromCache) throws Exception;
-}

+ 46 - 0
kalle/src/main/java/com/yanzhenjie/kalle/simple/Converter.kt

@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2018, Umbrella CompanyLimited All rights reserved.
+ * Project:Net
+ * Author:Drake
+ * Date:1/15/20 3:15 PM
+ */
+package com.yanzhenjie.kalle.simple
+
+import com.yanzhenjie.kalle.Request
+import com.yanzhenjie.kalle.Response
+import java.lang.reflect.Type
+
+/**
+ * Created by Zhenjie Yan on 2018/2/12.
+ */
+@Suppress("UNCHECKED_CAST")
+interface Converter {
+
+    @Throws(Exception::class)
+    fun <S, F> convert(
+        succeed: Type,
+        failed: Type,
+        request: Request,
+        response: Response,
+        result: Result<S, F>
+    )
+
+    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>
+            ) {
+                if (succeed === String::class.java) {
+                    result.success = response.body().string() as S
+                }
+            }
+        }
+    }
+}

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

@@ -78,7 +78,7 @@ public class RequestManager {
      * @param <F>     target object parameter.
      * @return the response to this request.
      */
-    public <S, F> SimpleResponse<S, F> perform(SimpleUrlRequest request, Type succeed, Type failed) throws Exception {
+    public <S, F> Result<S, F> perform(SimpleUrlRequest request, Type succeed, Type failed) throws Exception {
         return new UrlWorker<S, F>(request, succeed, failed).call();
     }
 
@@ -114,7 +114,7 @@ public class RequestManager {
      * @param <F>     target object parameter.
      * @return the response to this request.
      */
-    public <S, F> SimpleResponse<S, F> perform(SimpleBodyRequest request, Type succeed, Type failed) throws Exception {
+    public <S, F> Result<S, F> perform(SimpleBodyRequest request, Type succeed, Type failed) throws Exception {
         return new BodyWorker<S, F>(request, succeed, failed).call();
     }
 
@@ -159,7 +159,7 @@ public class RequestManager {
         }
 
         @Override
-        public void onResponse(final SimpleResponse<S, F> response) {
+        public void onResponse(final Result<S, F> response) {
             if (mCallback == null) return;
             mExecutor.execute(new Runnable() {
                 @Override

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

@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) 2018, Umbrella CompanyLimited All rights reserved.
+ * Project:Net
+ * Author:Drake
+ * Date:1/15/20 3:15 PM
+ */
+
+package com.yanzhenjie.kalle.simple
+
+import com.yanzhenjie.kalle.Headers
+
+data class Result<S, F>(
+    var code: Int,
+    var headers: Headers,
+    var fromCache: Boolean,
+    var success: S? = null,
+    var failure: F? = null
+) {
+
+    val isSucceed
+        get() = success != null || failure == null
+
+}

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

@@ -95,7 +95,7 @@ public class SimpleBodyRequest extends BodyRequest implements SimpleRequest {
             return this;
         }
 
-        public <S, F> SimpleResponse<S, F> perform(Type succeed, Type failed) throws Exception {
+        public <S, F> Result<S, F> perform(Type succeed, Type failed) throws Exception {
             return RequestManager.getInstance().perform(new SimpleBodyRequest(this), succeed, failed);
         }
 

+ 0 - 127
kalle/src/main/java/com/yanzhenjie/kalle/simple/SimpleResponse.java

@@ -1,127 +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.simple;
-
-import com.yanzhenjie.kalle.Headers;
-
-/**
- * Created in Oct 15, 2015 8:55:37 PM.
- */
-public final class SimpleResponse<Succeed, Failed> {
-
-    private final int mCode;
-    private final Headers mHeaders;
-    private final boolean mFromCache;
-    private final Succeed mSucceed;
-    private final Failed mFailed;
-    private SimpleResponse(Builder<Succeed, Failed> builder) {
-        this.mCode = builder.mCode;
-        this.mHeaders = builder.mHeaders;
-        this.mFromCache = builder.mFromCache;
-
-        this.mSucceed = builder.mSucceed;
-        this.mFailed = builder.mFailed;
-    }
-
-    public static <Succeed, Failed> Builder<Succeed, Failed> newBuilder() {
-        return new Builder<>();
-    }
-
-    /**
-     * Get the headers code of handle.
-     */
-    public int code() {
-        return mCode;
-    }
-
-    /**
-     * Get http headers headers.
-     */
-    public Headers headers() {
-        return mHeaders;
-    }
-
-    /**
-     * Whether the data returned from the cache.
-     *
-     * @return true: the data from cache, false: the data from network.
-     */
-    public boolean fromCache() {
-        return mFromCache;
-    }
-
-    /**
-     * Business successful.
-     */
-    public boolean isSucceed() {
-        return mFailed == null || mSucceed != null;
-    }
-
-    /**
-     * Get business success data.
-     */
-    public Succeed succeed() {
-        return mSucceed;
-    }
-
-    /**
-     * Get business failure data.
-     */
-    public Failed failed() {
-        return mFailed;
-    }
-
-    public static final class Builder<Succeed, Failed> {
-        private int mCode;
-        private Headers mHeaders;
-        private boolean mFromCache;
-
-        private Failed mFailed;
-        private Succeed mSucceed;
-
-        private Builder() {
-        }
-
-        public Builder<Succeed, Failed> code(int code) {
-            this.mCode = code;
-            return this;
-        }
-
-        public Builder<Succeed, Failed> headers(Headers headers) {
-            this.mHeaders = headers;
-            return this;
-        }
-
-        public Builder<Succeed, Failed> fromCache(boolean fromCache) {
-            this.mFromCache = fromCache;
-            return this;
-        }
-
-        public Builder<Succeed, Failed> succeed(Succeed succeed) {
-            this.mSucceed = succeed;
-            return this;
-        }
-
-        public Builder<Succeed, Failed> failed(Failed failed) {
-            this.mFailed = failed;
-            return this;
-        }
-
-        public SimpleResponse<Succeed, Failed> build() {
-            return new SimpleResponse<>(this);
-        }
-    }
-}

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

@@ -95,7 +95,7 @@ public class SimpleUrlRequest extends UrlRequest implements SimpleRequest {
             return this;
         }
 
-        public <S, F> SimpleResponse<S, F> perform(Type succeed, Type failed) throws Exception {
+        public <S, F> Result<S, F> perform(Type succeed, Type failed) throws Exception {
             return RequestManager.getInstance().perform(new SimpleUrlRequest(this), succeed, failed);
         }
 

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

@@ -24,7 +24,7 @@ import java.util.concurrent.FutureTask;
 /**
  * Created by Zhenjie Yan on 2018/2/13.
  */
-final class Work<T extends SimpleRequest, S, F> extends FutureTask<SimpleResponse<S, F>> implements Canceller {
+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;

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

@@ -52,9 +52,9 @@ inline fun <reified M> CoroutineScope.get(
     }
 
     if (response.isSucceed) {
-        response.succeed()
+        response.success!!
     } else {
-        throw response.failed() as ResponseException
+        throw response.failure as ResponseException
     }
 }
 
@@ -80,9 +80,9 @@ inline fun <reified M> CoroutineScope.post(
     }
 
     if (response.isSucceed) {
-        response.succeed()
+        response.success!!
     } else {
-        throw response.failed() as ResponseException
+        throw response.failure as ResponseException
     }
 }
 
@@ -171,9 +171,9 @@ inline fun <reified M> syncGet(
     }
 
     return if (response.isSucceed) {
-        response.succeed()
+        response.success!!
     } else {
-        throw response.failed() as ResponseException
+        throw response.failure as ResponseException
     }
 }
 
@@ -195,9 +195,9 @@ inline fun <reified M> syncPost(
     }
 
     return if (response.isSucceed) {
-        response.succeed()
+        response.success!!
     } else {
-        throw response.failed() as ResponseException
+        throw response.failure as ResponseException
     }
 }
 

+ 5 - 5
net/src/main/java/com/drake/net/NetConfig.kt

@@ -31,24 +31,24 @@ object NetConfig {
     lateinit var app: Application
 
     internal var defaultDialog: (DialogCoroutineScope.(FragmentActivity) -> Dialog)? = null
-    internal var onError: Throwable.() -> Unit = {
+    var onError: Throwable.() -> Unit = {
 
         val message = when (this) {
             is NetworkError -> app.getString(R.string.net_network_error)
             is URLError -> app.getString(R.string.net_url_error)
             is HostError -> app.getString(R.string.net_host_error)
             is ConnectTimeoutError -> app.getString(R.string.net_connect_timeout_error)
-            is ConnectException -> app.getString(R.string.net_connect_exception)
-            is WriteException -> app.getString(R.string.net_write_exception)
             is ReadTimeoutError -> app.getString(R.string.net_read_timeout_error)
             is DownloadError -> app.getString(R.string.net_download_error)
             is NoCacheError -> app.getString(R.string.net_no_cache_error)
-            is ReadException -> app.getString(R.string.net_read_exception)
             is ParseError -> app.getString(R.string.net_parse_error)
             is RequestParamsException -> app.getString(R.string.net_request_error)
             is ServerResponseException -> app.getString(R.string.net_server_error)
             is ExecutionException -> app.getString(R.string.net_image_error)
             is NullPointerException -> app.getString(R.string.net_null_error)
+            is ConnectException -> app.getString(R.string.net_connect_exception)
+            is WriteException -> app.getString(R.string.net_write_exception)
+            is ReadException -> app.getString(R.string.net_read_exception)
             is ResponseException -> msg
             else -> app.getString(R.string.net_other_error)
         }
@@ -57,7 +57,7 @@ object NetConfig {
         app.toast(message)
     }
 
-    internal var onStateError: Throwable.(view: View) -> Unit = {
+    var onStateError: Throwable.(view: View) -> Unit = {
 
         when (this) {
             is ParseError,

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

@@ -15,7 +15,7 @@ import com.drake.net.error.ServerResponseException
 import com.yanzhenjie.kalle.Request
 import com.yanzhenjie.kalle.Response
 import com.yanzhenjie.kalle.simple.Converter
-import com.yanzhenjie.kalle.simple.SimpleResponse
+import com.yanzhenjie.kalle.simple.Result
 import org.json.JSONObject
 import java.lang.reflect.Type
 
@@ -41,48 +41,48 @@ abstract class DefaultConvert(
         failed: Type,
         request: Request,
         response: Response,
-        fromCache: Boolean
-    ): SimpleResponse<S, F> {
-        var succeedData: S? = null
-        var failedData: F? = null
-
+        result: Result<S, F>
+    ) {
         val body = response.body().string()
-        var code = response.code()
+        val code = response.code()
 
         when {
             code in 200..299 -> {
-                val jsonObject = JSONObject(body)
+
+                if (succeed === String::class.java) {
+                    result.success = body as S
+                    return
+                }
+
+                val jsonObject = JSONObject(body.parseBody())
                 val responseCode = jsonObject.getString(this.code)
 
                 if (responseCode == success) {
-                    succeedData = if (succeed === String::class.java) body as S
-                    else convert(succeed, body)
+                    result.success = body.parseJson(succeed)
                 } else {
-                    failedData = ResponseException(code, jsonObject.getString(msg), request) as F
-                    code = responseCode.toInt()
+                    result.failure =
+                        ResponseException(code, jsonObject.getString(msg), request) as F
                 }
+
             }
             code in 400..499 -> throw RequestParamsException(code, request)
             code >= 500 -> throw ServerResponseException(code, request)
         }
-
-        return SimpleResponse.newBuilder<S, F>().code(code)
-            .headers(response.headers())
-            .fromCache(fromCache)
-            .succeed(succeedData)
-            .failed(failedData)
-            .build()
     }
 
-    fun encrypt(body: String) {
-
+    /**
+     * 解析数据用于获取基本接口信息
+     */
+    fun String.parseBody(): String {
+        return this
     }
 
     /**
      * 解析JSON数据
+     *
      * @param succeed Type 请求函数传过来的字节码类型
-     * @param body String 一般为返回JSON
      * @return S? 解析后的数据实体
      */
-    abstract fun <S> convert(succeed: Type, body: String): S?
+    abstract fun <S> String.parseJson(succeed: Type): S?
+
 }

+ 4 - 15
sample/src/main/java/com/drake/net/sample/App.kt

@@ -20,27 +20,16 @@ class App : Application() {
             errorLayout = R.layout.layout_error
         }
 
-        initNet("http://localhost.com") {
-
-            /*            converter(object : DefaultConvert() {
-                            override fun <S> convert(succeed: Type, body: String): S? {
-                                return Moshi.Builder().build().adapter<S>(succeed).fromJson(body)
-                            }
-
-                        })*/
-
+        initNet("") {
+            converter(JsonConvert())
             cacheEnabled()
         }
 
         SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, layout ->
-            ClassicsHeader(
-                context
-            )
+            ClassicsHeader(context)
         }
         SmartRefreshLayout.setDefaultRefreshFooterCreator { context, layout ->
-            ClassicsFooter(
-                context
-            )
+            ClassicsFooter(context)
         }
 
     }

+ 19 - 0
sample/src/main/java/com/drake/net/sample/JsonConvert.kt

@@ -0,0 +1,19 @@
+/*
+ * Copyright (C) 2018, Umbrella CompanyLimited All rights reserved.
+ * Project:Net
+ * Author:Drake
+ * Date:1/15/20 4:22 PM
+ */
+
+package com.drake.net.sample
+
+import com.drake.net.convert.DefaultConvert
+import com.squareup.moshi.Moshi
+import java.lang.reflect.Type
+
+class JsonConvert : DefaultConvert(code = "errno", msg = "errstr") {
+
+    override fun <S> String.parseJson(succeed: Type): S? {
+        return Moshi.Builder().build().adapter<S>(succeed).fromJson(this)
+    }
+}

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

@@ -19,7 +19,8 @@ class MainActivity : AppCompatActivity() {
 
             scope {
                 val data =
-                    get<String>("https://raw.githubusercontent.com/liangjingkanji/BRV/master/README.md")
+                    get<String>("https://github.com/liangjingkanji")
+
                 textView.text = data.await()
             }