I am new to Android kotlin and are currently making a app that import JSON from a url. I am trying to display a image with info like title etc.
This error (look futher down) appear in Logcat when I tried to import image from https://jsonplaceholder.typicode.com/albums/1/photos
Got a tips to set "User-Agent" as the header, the URLs will not work. But all effort have not worked.
Any help or tips is great.
Thanks
0
albumId 1
id 1
title "accusamus beatae ad facilis cum similique qui sunt"
url "https://via.placeholder.com/600/92c952"
thumbnailUrl "https://via.placeholder.com/150/92c952"
Logcat:
2022-03-29 21:53:09.161 12423-12423/com.example.mvvmretrofit W/Glide: Load failed for https://via.placeholder.com/150/771796 with size [1036x550]
class com.bumptech.glide.load.engine.GlideException: Failed to load resource
There was 1 root cause:
com.bumptech.glide.load.HttpException(Failed to connect or obtain data, status code: 410)
call GlideException#logRootCauses(String) for more detail
Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Fetching data failed, class java.io.InputStream, REMOTE
There was 1 root cause:
com.bumptech.glide.load.HttpException(Failed to connect or obtain data, status code: 410)
call GlideException#logRootCauses(String) for more detail
Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Fetch failed
There was 1 root cause:
com.bumptech.glide.load.HttpException(Failed to connect or obtain data, status code: 410)
call GlideException#logRootCauses(String) for more detail
Cause (1 of 1): class com.bumptech.glide.load.HttpException: Failed to connect or obtain data, status code: 410
MainAdapter.kt
class MainAdapter: RecyclerView.Adapter<MainViewHolder>() {
var movies = mutableListOf<Model.Movie>()
fun setMovieList(movies: List<Model.Movie>) {
this.movies = movies.toMutableList()
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainViewHolder {
val inflater = LayoutInflater.from(parent.context)
val binding = AdapterMovieBinding.inflate(inflater, parent, false)
return MainViewHolder(binding)
}
override fun onBindViewHolder(holder: MainViewHolder, position: Int) {
val movie = movies[position]
holder.binding.name.text = movie.albumId.toString()
Glide.with(holder.itemView.context).load(movie.thumbnailUrl).into(holder.binding.imageview)
}
override fun getItemCount(): Int {
return movies.size
}
}
class MainViewHolder(val binding: AdapterMovieBinding) : RecyclerView.ViewHolder(binding.root) {
}
RetrofitService.kt
@GET("/albums/1/photos")
@Headers("User-Agent: android")
fun getAllMovies() : Call<List<Model.Movie>>
companion object {
var retrofitService: RetrofitService? = null
fun getInstance() : RetrofitService {
if (retrofitService == null) {
val retrofit = Retrofit.Builder()
.baseUrl("https://jsonplaceholder.typicode.com/")
.addConverterFactory(GsonConverterFactory.create())
.build()
retrofitService = retrofit.create(RetrofitService::class.java)
}
return retrofitService!!
}
}