How to Log, the URL I am accessing retrofit - android

I am using retrofit to communicate with the BE and I want to the log the URL i am hitting and the body I am sending and the response I am getting (like 401, 404).
This is to know if I am hitting the right url what is the header, body and the actual URL to verify I am sending all the right info required
I implemented HttpLoggingInterceptor but that does not log it, or I am logging it the right way
can you suggest please.
object RetrofitBuilder {
private const val BASE_URL = "https://xxxx.xxxxxx.com/"
private fun getRetrofit(): Retrofit {
var interceptor: HttpLoggingInterceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
val client = OkHttpClient.Builder().addInterceptor(interceptor).build()
return Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build() //Doesn't require the adapter
}
val apiService: ApiService = getRetrofit().create(ApiService::class.java)
}
AppService
#POST
suspend fun postData(#Url url: String, #Body Data: Data, #Header("Autzn") authHeader: String)
ApiHelper
//NOTE: here i am using the different URL as but not the baseUrl defined on the top
apiService.postData("https://abcd.com/msg/oauth2/123456", Data,
"token")
//Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.6.0'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.squareup.okhttp3:logging-interceptor:4.2.1'
Please suggest how to log the url info
thanks
R

just give HttpLoggingInterceptor.Logger interface.
logging interceptor should be like this.
val loggingInterceptor = HttpLoggingInterceptor(object : HttpLoggingInterceptor.Logger {
override fun log(message: String) {
Timber.tag("retrofit").d(message)
}
})

This is How I'm logging url in logs
val retrofitApi: RetrofitInterface
get() {
if (retrofitInterface == null) {
val logger = HttpLoggingInterceptor()
logger.level = HttpLoggingInterceptor.Level.BODY
val okHttpClient = OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.addInterceptor(logger).build()
val retrofit = Retrofit.Builder()
.baseUrl(REST_HOST)
.addConverterFactory(GsonConverterFactory.create(GsonBuilder()
.setLenient()
.create()))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build()
retrofitInterface = retrofit.create(RetrofitInterface::class.java)
}
return this.retrofitInterface!!
}

I suggest you to use HttpLoggingInterceptor with log level, logging can be heavy task especially with images(printing raw bytes), and with big images(40mb+) can throw OOM exception.
So use logging for debug purpose.
public HttpLoggingInterceptor loggingInterceptor() {
HttpLoggingInterceptor loggingInterceptor
= new HttpLoggingInterceptor(message -> Log.d(TAG, message));
loggingInterceptor.setLevel(BuildConfig.DEBUG ? HttpLoggingInterceptor.Level.BODY : HttpLoggingInterceptor.Level.NONE);
return loggingInterceptor;
}

Related

OkHttp client generates diffrent URL for retrofit

I am developing a simple app to fetch now playing movie details from https://www.themoviedb.org/ API.
This is the URL to which I need to perform the API call.
https://api.themoviedb.org/3/movie/now_playing?api_key=<<api_key>>
I am using retrofit to make the API call like this.
#GET("/movie/now_playing")
fun getNowPlayingMovies(): Single<List<MovieData>>
and I am using Base Url and API key as variables in the client class.
private val BASE_URL = "https://api.themoviedb.org/3/"
private val API_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
Since the best way to add the api key as a query parameter to the URL is by using a request Interceptor , I created an intercepted to intercept the request and add the api key as a query parameter.
private fun getInterceptor() : Interceptor{
if (requestInterceptor == null){
requestInterceptor = Interceptor{
val url = it.request()
.url
.newBuilder()
.addQueryParameter("api_key" , API_KEY)
.build()
val request = it.request()
.newBuilder()
.url(url)
.build()
return#Interceptor it.proceed(request)
}
}
return requestInterceptor
}
Then added this interceptor along with logging interceptor to the OkHttp Client.
private fun getOkHttpClient() : OkHttpClient{
var httLog : HttpLoggingInterceptor = HttpLoggingInterceptor()
httLog.setLevel(HttpLoggingInterceptor.Level.BODY)
val okHttpClient = OkHttpClient.Builder()
.addInterceptor(getInterceptor()).addInterceptor(httLog)
.connectTimeout(60 , TimeUnit.SECONDS)
.build()
return okHttpClient
}
And then build the retrofit client. In which I add the base URL.
var retrofit : Retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.client(getOkHttpClient())
.addConverterFactory(getGsonConverterFactory())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
The problem is ,
the entire URL made using the BASE_URL and the api as a Query Paramter , Should be like this
https://api.themoviedb.org/3/movie/now_playing?api_key=**********
But in the logging interceptor I am getting this as the url
https://api.themoviedb.org/movie/now_playing?api_key=**********
which has a different BASE_URL than the one I provided in retrofit. It is giving me a 404 error.
I cannot seem to figure out why this is causing.
I logged on different places and I believe the request Interceptor is intercepting a different URL than the BASE_URL.
This is my entire codebase, it would be very helpful if there are any other improvements in the code , I am new to android development and Kotlin. Thank You.
interface MoviesApiServiceRx {
#GET("/movie/now_playing")
fun getNowPlayingMovies(): Single<List<MovieData>>
}
class MoviesRetrofitClient() {
private val BASE_URL = "https://api.themoviedb.org/3/"
private val API_KEY = "9a976526fce8c29aaa35eb4a1e654d3c"
private var moviesApiServiceRx : MoviesApiServiceRx
private var gsonConverterFactory : GsonConverterFactory
private var requestInterceptor : Interceptor
init {
moviesApiServiceRx = getMoviesApiServiceRx()
gsonConverterFactory = getGsonConverterFactory()
requestInterceptor = getInterceptor()
}
private fun getInterceptor() : Interceptor{
if (requestInterceptor == null){
requestInterceptor = Interceptor{
val url = it.request()
.url
.newBuilder()
.addQueryParameter("api_key" , API_KEY)
.build()
val request = it.request()
.newBuilder()
.url(url)
.build()
return#Interceptor it.proceed(request)
}
}
return requestInterceptor
}
private fun getGsonConverterFactory() : GsonConverterFactory{
if (gsonConverterFactory == null){
gsonConverterFactory = GsonConverterFactory.create();
}
return gsonConverterFactory
}
private fun getOkHttpClient() : OkHttpClient{
var httLog : HttpLoggingInterceptor = HttpLoggingInterceptor()
httLog.setLevel(HttpLoggingInterceptor.Level.BODY)
val okHttpClient = OkHttpClient.Builder()
.addInterceptor(getInterceptor()).addInterceptor(httLog)
.connectTimeout(60 , TimeUnit.SECONDS)
.build()
return okHttpClient
}
private fun getMoviesApiServiceRx() : MoviesApiServiceRx{
if (moviesApiServiceRx == null){
var retrofit : Retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.client(getOkHttpClient())
.addConverterFactory(getGsonConverterFactory())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
moviesApiServiceRx = retrofit.create(MoviesApiServiceRx::class.java)
}
return moviesApiServiceRx
}
fun getNowPlayingMovies(): Single<List<MovieData>> {
return getMoviesApiServiceRx().
getNowPlayingMovies()
}
}
Change
#GET("/movie/now_playing")
to
#GET("movie/now_playing")
The / at the beginning refers to the root so it will replace whatever there is after the service name.
two options
interface MoviesApiServiceRx {
#GET("/3/movie/now_playing")
fun getNowPlayingMovies(): Single<List<MovieData>>
}
private val BASE_URL = "https://api.themoviedb.org/"
or
interface MoviesApiServiceRx {
#GET("movie/now_playing")
fun getNowPlayingMovies(): Single<List<MovieData>>
}
private val BASE_URL = "https://api.themoviedb.org/3/"

I am trying to create a retrofit instance inside companion object I am writing this code using using by lazy{}

class RetrofitInstance {
companion object {
private val retrofit by lazy {
val logging = HttpLoggingInterceptor()
logging.setLevel(HttpLoggingInterceptor.Level.BODY)
val client = OkHttpClient.Builder()
.addInterceptor(logging)
.build()
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build()
}
val api by lazy {
retrofit.create(RickAndMortyServiceApi::class.java)
}
} }
Error message
"Type 'Lazy<TypeVariable(T)>' has no method 'getValue(RetrofitInstance.Companion, KProperty<*>)' and thus it cannot serve as a delegate"
Kotlin version 1.6
I have tried this code snippet on my machine, and it works fine:
companion object {
private val retrofit by lazy {
val logging = HttpLoggingInterceptor()
logging.setLevel(HttpLoggingInterceptor.Level.BODY)
val client = OkHttpClient.Builder()
.addInterceptor(logging)
.build()
Retrofit.Builder()
.baseUrl("https://www.google.com")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build()
}
val api by lazy {
retrofit.create(DummyClass::class.java)
}
}
So i suggest you double-check your code for typos, or you can check for dependency versions. Versions that i'm using are:
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation("com.squareup.okhttp3:logging-interceptor:4.9.3")

android connecting restful api with mvvm pattern

I am implementing user login and signup with my custom backend in which i get a token when i signup or login.Then i save that token inside SharedPreferences.
So when i want to call an api, i call it inside Repository which is an kotlin object so that it becomes singleton.
In this app when i call api, i should add a authorization header,get token from SharedPreferences and assign to this header.
object MainRepository {
private var sharedPreferencesRepository: SharedPreferencesRepository
private var retrofit: Retrofit
private lateinit var mainApi:MainApi
init {
sharedPreferencesRepository = SharedPreferencesRepository(MyApplication.context)
val user = sharedPreferencesRepository.getUser()
val httpClient = OkHttpClient.Builder()
.addInterceptor(MyOkHttpClientInterceptor(user.token))
.build()
retrofit =
Retrofit.Builder().baseUrl(API_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient)
.build()
mainApi = retrofit.create(MainApi::class.java)
}
fun getSomethingFromApi() = myApi.getSomething()
}
The problem is, when i launch app and login it works just fine. But when i logout, and login again, because MainRepository class is only initialized once application launched, it doesn't get new token from SharedPreferences. I mean retrofit instance is only build once.
So how can i solve this problem? Should i add header dynamically for each api call? (There are so many calls that require Authorization header, that is why i did not add header at each request,instead i added an interceptor)
You can add an interceptor and add your authorization header in the OkhttpClient.Builder().
class RetrofitPrivateService {
var token = SharedPreferencesHelper().getToken()
companion object {
private val interceptor: HttpLoggingInterceptor = HttpLoggingInterceptor(ApiLogger())
.setLevel(HttpLoggingInterceptor.Level.BODY)
private val client = OkHttpClient.Builder()
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.addInterceptor(object : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request().newBuilder()
.addHeader("Authorization", "Bearer " + RetrofitPrivateService().token)
.build()
return chain.proceed(request)
}
})
.addInterceptor(interceptor)
.build()
private val retrofit = Retrofit.Builder()
.baseUrl(API_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build()
// function to access your API here
}

Retrofit doesn't show server response

I use retrofit for async server requests.
Below is declaration of API client:
object NasaApiClient {
private const val NASA_BASE_URL="https://images-api.nasa.gov/"
fun getClient(): NasaApiService {
val logging = HttpLoggingInterceptor()
logging.level = HttpLoggingInterceptor.Level.BODY
val okHttpClient = OkHttpClient.Builder()
.addInterceptor(logging)
.readTimeout(20, TimeUnit.SECONDS)
.build()
val gson = GsonBuilder().create()
val retrofit = Retrofit.Builder()
.baseUrl(NASA_BASE_URL)
.client(okHttpClient)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
return retrofit.create(NasaApiService::class.java)
}
}
Also i use Rx method for this GET request:
interface NasaApiService {
#GET("search")
fun mediaInfo(#Query("nasa_id")nasa_id:String): Single<MediaDetail>
}
fun fetchMediaDetails(nasaId:String){
_networkState.postValue(NetworkState.LOADING)
try {
compositeDisposable.add(
apiService.mediaInfo(nasaId)
.observeOn(Schedulers.io())
.subscribeOn(Schedulers.io())
.subscribe ({
Log.e("MediaDetail",it.toString())
_downloadedMediaDetailsResponse.postValue(it)
_networkState.postValue(NetworkState.LOADED)
},{
_networkState.postValue(NetworkState.ERROR)
Log.e("MovieDetailsDataSource", it.message.toString())
})
)
}
catch (e: Exception){
Log.e("MediaDetailsDataSource", e.message.toString())
}
}
Retrofit doesn't show server HTTP response in log. Why does it happen?
Logging interceptor enabled!
After updating version of retrofit and okHttp it works!
//Retrofit
def retrofit_version = "2.9.0"
implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
implementation "com.squareup.retrofit2:adapter-rxjava2:$retrofit_version"
implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"
implementation 'com.squareup.okhttp3:logging-interceptor:4.9.0'

Call an activity from Koin module

I'm trying to logout user in case of a 401 response from any API call in the project.
I'm using Koin Module. Here's the code snippet of Koin Module:
single {
retrofit(get())
}
single {
client()
}
private fun retrofit(okHttpClient: OkHttpClient): Retrofit {
val gson = GsonBuilder()
.setLenient()
.create()
return Retrofit.Builder()
.baseUrl(Config.BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build()
}
private fun client(): OkHttpClient {
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
return OkHttpClient.Builder()
.connectTimeout(1, TimeUnit.MINUTES)
.writeTimeout(1, TimeUnit.MINUTES)
.readTimeout(1, TimeUnit.MINUTES)
.addInterceptor(object : Interceptor {
#Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): Response {
val request: Request = chain.request()
val response: Response = chain.proceed(request)
// todo deal with the issues the way you need to
if (response.code == 200) {
//Open LogIn Activity
}
return response
}
}).build()
}
How can I call activity from within the interceptor? Or is there any other preferred way to do this?
Any help is appreciated.

Categories

Resources