Skip to content

Commit

Permalink
update parse logic for GSON response
Browse files Browse the repository at this point in the history
  • Loading branch information
utsavdotpro committed Mar 5, 2022
1 parent 287b6c7 commit a01d64a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.os.Handler
import android.os.Looper
import android.util.Log
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.isolpro.custom.Callback
import com.isolpro.library.connection.helpers.Utils
import org.json.JSONException
Expand All @@ -16,7 +17,6 @@ import java.nio.charset.StandardCharsets
import java.util.concurrent.Executor
import java.util.concurrent.Executors


abstract class Connection<T>() {
private val REQUST_MODE_POST = "POST";
private val REQUST_MODE_GET = "GET";
Expand Down Expand Up @@ -230,7 +230,7 @@ abstract class Connection<T>() {
onResponseReceived(mutatedResponseString)

try {
val res: T = Gson().fromJson(mutatedResponseString, getClassType());
val res = Gson().fromJson<T>(mutatedResponseString, object : TypeToken<T>() {}.type);

onSuccess(res);

Expand Down Expand Up @@ -275,8 +275,6 @@ abstract class Connection<T>() {

abstract fun handleOnError(e: Exception)

abstract fun getClassType(): Class<T>

class Config(val baseEndpoint: String) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.isolpro.library.connection.Connection

const val BASE_ENDPOINT = "https://jsonplaceholder.typicode.com";

class ConnectionHelper<T>(private val ctx: Context, private val typeClass: Class<T>) :
class ConnectionHelper<T>(private val ctx: Context) :
Connection<T>() {
override var config: Config = Config(BASE_ENDPOINT)

Expand Down Expand Up @@ -58,8 +58,4 @@ class ConnectionHelper<T>(private val ctx: Context, private val typeClass: Class
override fun handleOnNoResponseError() {
Log.e("---------", "handleOnNoResponseError");
}

override fun getClassType(): Class<T> {
return typeClass;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import com.isolpro.library.connection.models.Post

object PostService {

fun getPosts(ctx: Context): Connection<Post> {
return ConnectionHelper(ctx, Post::class.java)
fun getPosts(ctx: Context): Connection<List<Post>> {
return ConnectionHelper<List<Post>>(ctx)
.endpoint("/posts")
.loader(false)
}

fun createPost(ctx: Context, post: Post): Connection<Post> {
return ConnectionHelper(ctx, Post::class.java)
return ConnectionHelper<Post>(ctx)
.payload(post)
.endpoint("/posts")
.loader(false)
Expand Down

0 comments on commit a01d64a

Please sign in to comment.