Skip to content

Commit

Permalink
2019-03-29 14:20
Browse files Browse the repository at this point in the history
Request cache added
  • Loading branch information
Aryan-mor committed Mar 29, 2019
1 parent ba52afd commit d76dbbd
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 56 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/com/gcorp/retrofithelperexample/BaseApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ class BaseApp : Application() {
super.onCreate()

retrofitClient =
RetrofitClient().setBaseUrl("http://192.168.1.2/test/")
RetrofitClient(this).setBaseUrl("http://192.168.1.2/test/")
.setUrl("mili","http://7468e347.ngrok.io/api/")
.setConnectionTimeout(15)
.enableCaching()
.setReadingTimeout(15)
.addHeader("Accept", "application/json")
.addHeader("client", "android")
Expand Down
28 changes: 18 additions & 10 deletions app/src/main/java/com/gcorp/retrofithelperexample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import com.gcorp.retrofithelper.RequestHandler
import com.gcorp.retrofithelper.Response
import com.gcorp.retrofithelperexample.BaseApp.Companion.retrofitClient
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_main.*
import java.io.Serializable
import java.io.StringReader
import kotlin.math.log


class MainActivity : AppCompatActivity() {

var text = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Expand Down Expand Up @@ -59,12 +60,17 @@ class MainActivity : AppCompatActivity() {
// aa.items.forEach {
// Log.e("Ary", "${it.name} -> ${it.url}")
// }
request()
requestBtn.setOnClickListener {
request()
}
}

retrofitClient.Get<T2>()
.setBaseUrlKey("mili")
.setPath("todo/1")
.setRequestHandler(T2::class.java, object : RequestHandler<T2>() {
override fun onSuccess(response: Response<T2>) {
fun request() {
retrofitClient.Get<T3>()
.setPath("items.json")
.setRequestHandler(T3::class.java, object : RequestHandler<T3>() {
override fun onSuccess(response: Response<T3>) {
super.onSuccess(response)

// EEEERRRRRRRRRORRRRRRRRRR is hear
Expand All @@ -74,12 +80,14 @@ class MainActivity : AppCompatActivity() {
Log.e("Ary", "raw -> " + response.raw!!.toString())
Log.e("Ary", "raw.body -> " + response.raw!!.body())

Log.e("Ary", "body -> " + response.body.name)


text += "\n\n"
response.body.items?.forEach {
text += it.name + ", "
}
textTv.text = text
}

override fun onError(response: Response<T2>?) {
override fun onError(response: Response<T3>?) {
super.onError(response)
}

Expand Down
23 changes: 14 additions & 9 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
<Button
android:id="@+id/requestBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
android:text="Request"
android:layout_margin="15dp"/>

</android.support.constraint.ConstraintLayout>
<TextView
android:id="@+id/textTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="Req test"
android:padding="5dp"/>
</LinearLayout>
4 changes: 2 additions & 2 deletions retrofithelper/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
defaultConfig {
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0.2"
versionCode 2
versionName "1.1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand Down
3 changes: 2 additions & 1 deletion retrofithelper/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gcorp.retrofithelper">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>

</manifest>
Loading

0 comments on commit d76dbbd

Please sign in to comment.