Web Service response delayed with Retrofit by approx 10 secs

Viewed 259

Problem Statement: We are using Retrofit for calling web services. For some reason web service response is getting delayed by approx 10 seconds. If we use Volley instead, We are getting response within 2-3 seconds.

Below is the code.

MainActivity.kt

    class MainActivity : AppCompatActivity() {

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
        }
        
        fun loadRetrofit(view: View)  {
        
            Log.e("lol", "Start: ${Date()}")
        
            val apiResponse = APIClient.api.getFavouriteLocationsAsync(dateAndTime = "2021-02-10T16:27:03.921Z")
            
            apiResponse.enqueue(object : Callback<ResponseBody> {
                
                override fun onResponse(call: Call<ResponseBody>, response: retrofit2.Response<ResponseBody>) {
                    Log.e("lol", "onResponse: $response")
                    Toast.makeText(this@MainActivity, "Success", Toast.LENGTH_SHORT).show()
                }

                override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
                    Log.e("lol", "onFailure: ${t.localizedMessage}");
                }
            })
        }
        
        fun loadVolley(view: View) {
        
        Log.e("lol", "Start: ${Date()}")

        val queue = Volley.newRequestQueue(this)
        val url = "full URL"

            val stringRequest =
                object : StringRequest(Request.Method.GET, url, Response.Listener { response ->
                    Log.e("lol", "End: ${Date()}")
                    Log.e("lol", "onResponse: $response")
                    Toast.makeText(this@MainActivity, "Success", Toast.LENGTH_SHORT).show()
                },
                    Response.ErrorListener { volleyError ->
                        Log.e("lol", "End: ${Date()}")
                        Log.e("lol", "onResponse: $volleyError")
                    }) {
                    override fun getHeaders(): MutableMap<String, String> {
                        val headers = HashMap<String, String>()
                        headers["Authorization"] = "Token here"
                        return headers
                    }
                }
            queue.add(stringRequest)
        }
}

activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_fav_parking_lot"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_volley"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:onClick="loadVolley"
        android:text="Hit Volley"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/btn_retrofit"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/btn_retrofit"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:onClick="loadRetrofit"
        android:text="Hit Retrofit"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/btn_volley" />
</androidx.constraintlayout.widget.ConstraintLayout>

APIClient.kt

    class APIClient {

    companion object {

        private val retrofit by lazy {

            val logging = HttpLoggingInterceptor()
            logging.setLevel(HttpLoggingInterceptor.Level.BODY)

            val client = OkHttpClient.Builder()
                .addInterceptor(logging)
                .readTimeout(30, TimeUnit.SECONDS)
                .build()

            Retrofit.Builder()
                .baseUrl("base url here")
                /*.baseUrl("https://randomuser.me/")*/
                .client(client)
                .build()
        }

        val api by lazy {
            retrofit.create(ApiService::class.java)
        }
    }
}

ApiService.kt

interface ApiService {

    @GET("end point here")
    fun getFavouriteLocationsAsync(
        @Header("Authorization") toke: String = "Token here",
        @Query("currentDateTime") dateAndTime: String
    ): Call<ResponseBody>
}

We have already tried

  • Trying to call Open source web service with same above code of Retrofit, working fast
  • The same web service is working fine/fast in iOS app.
  • Tried lowering the Retrofit version, no luck!

Any guidance in right direction would be very much appreciated

0 Answers
Related