From 47691d038a999c5a5c498d1b300fb9498400de8f Mon Sep 17 00:00:00 2001 From: Aryan Moradi Date: Thu, 28 Mar 2019 16:31:42 +0430 Subject: [PATCH] 2019-03-28 16:30 Bug fix --- .../gcorp/retrofithelperexample/BaseApp.kt | 3 +- .../retrofithelperexample/MainActivity.kt | 83 +++++++++++++------ .../com/gcorp/retrofithelperexample/T3.kt | 6 ++ retrofithelper/build.gradle | 2 +- .../com/gcorp/retrofithelper/ApiInterface.kt | 2 +- .../com/gcorp/retrofithelper/MySubscriber.kt | 44 ---------- .../java/com/gcorp/retrofithelper/Response.kt | 8 +- .../gcorp/retrofithelper/RetrofitClient.kt | 64 ++++++++------ 8 files changed, 112 insertions(+), 100 deletions(-) create mode 100644 app/src/main/java/com/gcorp/retrofithelperexample/T3.kt delete mode 100644 retrofithelper/src/main/java/com/gcorp/retrofithelper/MySubscriber.kt diff --git a/app/src/main/java/com/gcorp/retrofithelperexample/BaseApp.kt b/app/src/main/java/com/gcorp/retrofithelperexample/BaseApp.kt index e933640..8e7a978 100644 --- a/app/src/main/java/com/gcorp/retrofithelperexample/BaseApp.kt +++ b/app/src/main/java/com/gcorp/retrofithelperexample/BaseApp.kt @@ -8,7 +8,8 @@ class BaseApp : Application() { super.onCreate() retrofitClient = - RetrofitClient().setBaseUrl("http://192.168.1.97/") +// RetrofitClient().setBaseUrl("http://192.168.1.2/test/") + RetrofitClient().setBaseUrl("http://bef0de5a.ngrok.io/api/") .setUrl("salam","http://192.168.1.95:81/") .setConnectionTimeout(15) .setReadingTimeout(15) diff --git a/app/src/main/java/com/gcorp/retrofithelperexample/MainActivity.kt b/app/src/main/java/com/gcorp/retrofithelperexample/MainActivity.kt index 1ef8af1..00f7cec 100644 --- a/app/src/main/java/com/gcorp/retrofithelperexample/MainActivity.kt +++ b/app/src/main/java/com/gcorp/retrofithelperexample/MainActivity.kt @@ -7,6 +7,9 @@ import android.util.Log import com.gcorp.retrofithelper.RequestHandler import com.gcorp.retrofithelper.Response import com.gcorp.retrofithelperexample.BaseApp.Companion.retrofitClient +import com.google.gson.Gson +import java.io.Serializable +import kotlin.math.log class MainActivity : AppCompatActivity() { @@ -14,31 +17,59 @@ class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) +// +// retrofitClient.Put() +// .setBaseUrlKey("salam") +// .setRequestHeader("tcl", "salam2") +// .setRequestHeader("vcl", "vcl1") +// .setPath("test") +// .setUrlParams("param1", "Salam") +// .setRequest(T()) +// .setRequestHandler(object : RequestHandler(){ +// override fun onSuccess(response: Response) { +// super.onSuccess(response) +// retrofitClient.Put() +// .setBaseUrlKey("salam") +// .setRequestHeader("vcl2", "vcl2") +// .setPath("test") +// .setUrlParams("param1", "Salam") +// .setRequest(T()) +// .setRequestHandler(object : RequestHandler(){ +// override fun onSuccess(response: Response) { +// super.onSuccess(response) +// +// } +// +// }) +// .run() +// } +// }) +// .run() - retrofitClient.Put() - .setBaseUrlKey("salam") - .setRequestHeader("tcl", "salam2") - .setRequestHeader("vcl", "vcl1") - .setPath("test") - .setUrlParams("param1", "Salam") - .setRequest(T()) - .setRequestHandler(object : RequestHandler(){ - override fun onSuccess(response: Response) { + + retrofitClient.Post() + .setPath("todo/1") + .setRequestHandler(T3::class.java,object : RequestHandler() { + override fun onSuccess(response: Response) { super.onSuccess(response) - retrofitClient.Put() - .setBaseUrlKey("salam") - .setRequestHeader("vcl2", "vcl2") - .setPath("test") - .setUrlParams("param1", "Salam") - .setRequest(T()) - .setRequestHandler(object : RequestHandler(){ - override fun onSuccess(response: Response) { - super.onSuccess(response) - - } - - }) - .run() + +// EEEERRRRRRRRRORRRRRRRRRR is hear +// |||||||||||||||||| +// >>>>>>>>>>>>>>> + + Log.e("Ary", "raw -> " + response.raw!!.toString()) + Log.e("Ary", "raw.body -> " + response.raw!!.body()) + + Log.e("Ary", "body -> " + response.body.name) + } + + override fun onError(response: Response?) { + super.onError(response) + } + + override fun onFailed(e: Throwable?) { + super.onFailed(e) + Log.e("Error", ":DDD errrrror -> ${e!!.message}") } }) .run() @@ -48,7 +79,9 @@ class MainActivity : AppCompatActivity() { var bye: String = ":DDDDDDD" } - inner class T2 { - var min_version:String = "pashm" + open inner class T2 { + var id: String = "pashm" + var name: String = "pashm" + var isComplete: Boolean = false } } diff --git a/app/src/main/java/com/gcorp/retrofithelperexample/T3.kt b/app/src/main/java/com/gcorp/retrofithelperexample/T3.kt new file mode 100644 index 0000000..2467523 --- /dev/null +++ b/app/src/main/java/com/gcorp/retrofithelperexample/T3.kt @@ -0,0 +1,6 @@ +package com.gcorp.retrofithelperexample +open class T3 { + var id: String = "pashm" + var name: String = "pashm" + var isComplete: Boolean = false +} \ No newline at end of file diff --git a/retrofithelper/build.gradle b/retrofithelper/build.gradle index 92d5404..1e16e12 100644 --- a/retrofithelper/build.gradle +++ b/retrofithelper/build.gradle @@ -11,7 +11,7 @@ android { minSdkVersion 19 targetSdkVersion 28 versionCode 1 - versionName "1.0" + versionName "1.0.1" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" diff --git a/retrofithelper/src/main/java/com/gcorp/retrofithelper/ApiInterface.kt b/retrofithelper/src/main/java/com/gcorp/retrofithelper/ApiInterface.kt index ad5fd45..45a78d3 100644 --- a/retrofithelper/src/main/java/com/gcorp/retrofithelper/ApiInterface.kt +++ b/retrofithelper/src/main/java/com/gcorp/retrofithelper/ApiInterface.kt @@ -8,7 +8,7 @@ import rx.Observable interface ApiInterface { @GET - fun get(@Url url: String, @QueryMap urlParams: Any?): Observable> + fun get(@Url url: String, @QueryMap urlParams: HashMap?): Observable> @POST fun post(@Url url: String, @QueryMap urlParams: HashMap?, @Body request: Any?): Observable> diff --git a/retrofithelper/src/main/java/com/gcorp/retrofithelper/MySubscriber.kt b/retrofithelper/src/main/java/com/gcorp/retrofithelper/MySubscriber.kt deleted file mode 100644 index da9c004..0000000 --- a/retrofithelper/src/main/java/com/gcorp/retrofithelper/MySubscriber.kt +++ /dev/null @@ -1,44 +0,0 @@ -package com.gcorp.retrofithelper - -import android.util.Log -import retrofit2.Response -import rx.Subscriber - -abstract class MySubscriber(private val requestHandler: RequestHandler?): Subscriber>() { - override fun onNext(t: Response?) { - if (t != null) { - Log.e("myApp", "12") - Log.e("myApp", "code -> ${t.code()}") - Log.e("myApp", "body -> ${t.body()}") - Log.e("myApp", "header -> ${t.headers()}") - - } - - if (t == null) { - requestHandler?.onFailed(Throwable("Response in null")) - return - } - - val res = Response(t) - - if (t.code() != 200) { - Log.e("myApp", "13") - requestHandler?.onError(res) - return - } - Log.e("myApp", "14") - requestHandler?.onSuccess(res) - - } - - override fun onCompleted() { - Log.e("myApp", "onCompleted()") - requestHandler?.onComplete() - } - - override fun onError(e: Throwable?) { - Log.e("myApp", "onError()") - requestHandler?.onFailed(e) - e?.printStackTrace() - } -} \ No newline at end of file diff --git a/retrofithelper/src/main/java/com/gcorp/retrofithelper/Response.kt b/retrofithelper/src/main/java/com/gcorp/retrofithelper/Response.kt index ee7875d..9cb449a 100644 --- a/retrofithelper/src/main/java/com/gcorp/retrofithelper/Response.kt +++ b/retrofithelper/src/main/java/com/gcorp/retrofithelper/Response.kt @@ -1,11 +1,13 @@ package com.gcorp.retrofithelper import android.util.Log +import com.google.gson.Gson import retrofit2.Response -class Response(response: Response) { - var header = response.headers().toMultimap() - var body = response.body() +class Response(response: Response, classOfT: Class) { + var header: MutableMap>? = response.headers().toMultimap() + var body = Gson().fromJson(response.body().toString(), classOfT) var code = response.code() + var raw: okhttp3.Response? = response.raw() } \ No newline at end of file diff --git a/retrofithelper/src/main/java/com/gcorp/retrofithelper/RetrofitClient.kt b/retrofithelper/src/main/java/com/gcorp/retrofithelper/RetrofitClient.kt index eba7436..e59bcd9 100644 --- a/retrofithelper/src/main/java/com/gcorp/retrofithelper/RetrofitClient.kt +++ b/retrofithelper/src/main/java/com/gcorp/retrofithelper/RetrofitClient.kt @@ -93,8 +93,7 @@ class RetrofitClient { fun removeHeader(headers: HashMap): RetrofitClient { headers.forEach { - if (this.headers[it.key] != null){ - Log.e("myApp","remove ${it.key} -> ${it.value}") + if (this.headers[it.key] != null) { this.headers.remove(it.key) } } @@ -201,7 +200,7 @@ class RetrofitClient { ) as Observable>) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) - .subscribe(object : MySubscriber(requestHandler){ + .subscribe(object : MySubscriber(classOfT!!,requestHandler) { override fun onCompleted() { super.onCompleted() removeHeader(requestHeader) @@ -212,6 +211,7 @@ class RetrofitClient { inner class Post : PostBaseRequest() { override fun run() { + val retrofit = addHeader(requestHeader).getClient(baseUrlKey!!) requestHandler!!.onBeforeSend() @@ -223,7 +223,7 @@ class RetrofitClient { ) as Observable>) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) - .subscribe(object : MySubscriber(requestHandler){ + .subscribe(object : MySubscriber(classOfT!!,requestHandler) { override fun onCompleted() { super.onCompleted() removeHeader(requestHeader) @@ -231,7 +231,7 @@ class RetrofitClient { }) } } - + inner class Put : PostBaseRequest() { override fun run() { val retrofit = addHeader(requestHeader).getClient(baseUrlKey!!) @@ -245,7 +245,7 @@ class RetrofitClient { ) as Observable>) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) - .subscribe(object : MySubscriber(requestHandler){ + .subscribe(object : MySubscriber(classOfT!!,requestHandler) { override fun onCompleted() { super.onCompleted() removeHeader(requestHeader) @@ -267,7 +267,7 @@ class RetrofitClient { ) as Observable>) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) - .subscribe(object : MySubscriber(requestHandler){ + .subscribe(object : MySubscriber(classOfT!!,requestHandler) { override fun onCompleted() { super.onCompleted() removeHeader(requestHeader) @@ -288,7 +288,7 @@ class RetrofitClient { ) as Observable>) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) - .subscribe(object : MySubscriber(requestHandler){ + .subscribe(object : MySubscriber(classOfT!!,requestHandler) { override fun onCompleted() { super.onCompleted() removeHeader(requestHeader) @@ -310,7 +310,7 @@ class RetrofitClient { ) as Observable>) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) - .subscribe(object : MySubscriber(requestHandler){ + .subscribe(object : MySubscriber(classOfT!!,requestHandler) { override fun onCompleted() { super.onCompleted() removeHeader(requestHeader) @@ -332,7 +332,7 @@ class RetrofitClient { ) as Observable>) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) - .subscribe(object : MySubscriber(requestHandler){ + .subscribe(object : MySubscriber(classOfT!!,requestHandler) { override fun onCompleted() { super.onCompleted() removeHeader(requestHeader) @@ -344,8 +344,14 @@ class RetrofitClient { abstract inner class BaseRequest { var requestHeader: HashMap = HashMap() var baseUrlKey: String? = null + get() { + if (field != null) + return field + return BASE_URL + } var path: String? = null var urlParams: HashMap = HashMap() + var classOfT: Class? = null var requestHandler: RequestHandler? = null open fun setRequestHeader(key: String, value: String): BaseRequest { @@ -388,7 +394,11 @@ class RetrofitClient { return this } - open fun setRequestHandler(requestHandler: RequestHandler): BaseRequest { + open fun setRequestHandler( + classOfT: Class, + requestHandler: RequestHandler + ): BaseRequest { + this.classOfT = classOfT this.requestHandler = requestHandler return this } @@ -445,8 +455,11 @@ class RetrofitClient { return this } - override fun setRequestHandler(requestHandler: RequestHandler): PostBaseRequest { - super.setRequestHandler(requestHandler) + override fun setRequestHandler( + classOfT: Class, + requestHandler: RequestHandler + ): PostBaseRequest { + super.setRequestHandler(classOfT, requestHandler) return this } } @@ -457,6 +470,7 @@ class RetrofitClient { var path: String? = null var urlParams: HashMap = HashMap() var part: MultipartBody.Part? = null + var classOfT: Class? = null var requestHandler: RequestHandler? = null open fun setRequestHeader(key: String, value: String): MultiPartBaseRequest { this.requestHeader[key] = value @@ -511,7 +525,8 @@ class RetrofitClient { return setPart(FileUtils.bitmapToPart(activity, bitmap, name)) } - open fun setRequestHandler(requestHandler: RequestHandler): MultiPartBaseRequest { + open fun setRequestHandler(classOfT: Class,requestHandler: RequestHandler): MultiPartBaseRequest { + this.classOfT = classOfT this.requestHandler = requestHandler return this } @@ -520,14 +535,14 @@ class RetrofitClient { } } - open class MySubscriber(private val requestHandler: RequestHandler?):Subscriber>(){ + open class MySubscriber(private val classOfT: Class, private val requestHandler: RequestHandler?) : + Subscriber>() { override fun onNext(t: Response?) { if (t != null) { - Log.e("myApp", "12") - Log.e("myApp", "code -> ${t.code()}") - Log.e("myApp", "body -> ${t.body()}") - Log.e("myApp", "header -> ${t.headers()}") - + Log.e("Request", "code -> ${t.code()}") + Log.e("Request", "raw -> ${t.raw()}") + Log.e("Request", "body -> ${t.body()}") + Log.e("Request", "header -> ${t.headers()}") } if (t == null) { @@ -535,25 +550,24 @@ class RetrofitClient { return } - val res = Response(t) + Log.e("AryLib", "raw -> " + t.raw().toString()) + Log.e("AryLib", "raw.body -> " + t.body()) + + val res = Response(t, classOfT) if (t.code() != 200) { - Log.e("myApp", "13") requestHandler?.onError(res) return } - Log.e("myApp", "14") requestHandler?.onSuccess(res) } override fun onCompleted() { - Log.e("myApp", "onCompleted()") requestHandler?.onComplete() } override fun onError(e: Throwable?) { - Log.e("myApp", "onError()") requestHandler?.onFailed(e) e?.printStackTrace() }