Search code examples
androidapikotlinretrofitandroid-architecture-components

Retrofit response.body is null while using league of legends API


I try to recover the data of a player with the league of legends API however the response to my request is always null and those without an error message in my logcat.

here is my retrofit call:

 public interface LolApiService {
        @GET("summoners/by-name/")
        Call<SummonerData> getSummonerData (@Query("summonerName")String summonerName, @Query("key") String key);
    }

here is my repository:

class LolApiRepository(val application: Application) {

    val response = MutableLiveData<SummonerData>()

    fun getSummonerID(summonerName: String, key: String): MutableLiveData<SummonerData> {
     //  val responseData = MutableLiveData<SummonerData>()



        val retrofit = Retrofit.Builder()
            .baseUrl("https://euw1.api.riotgames.com/lol/summoner/v4/")
            .addConverterFactory(GsonConverterFactory.create())
            .build()

        val service = retrofit.create(LolApiService::class.java)



        service.getSummonerData(summonerName, key).enqueue(object : Callback<SummonerData> {
            override fun onFailure(call: Call<SummonerData>, t: Throwable) {
                Toast.makeText(application, "Error wile accessing the API", Toast.LENGTH_SHORT)
                    .show()


            }

            override fun onResponse(call: Call<SummonerData>, resp: Response<SummonerData>) {
                Log.d("LolApiRepository", "LolApiRepository:" + resp.body().toString())
                if (resp.body() != null) {
                    Toast.makeText(application, "Success accessing the API", Toast.LENGTH_SHORT)
                        .show()
                    response.value = (resp.body() as SummonerData)
                } else {
                    Log.d("LolApiRepository", "LolApiRepository:" + resp.errorBody().toString())
                    Toast.makeText(application, "Error wile accessing the API", Toast.LENGTH_SHORT)
                        .show()
                }
            }

        })
        return response
    }
}

my data model in which I retrieve the result of my query:

class SummonerData {
    @SerializedName("id")
    @Expose
    var id: String? = null

    @SerializedName("accountId")
    @Expose
    var accountId: String? = null

    @SerializedName("puuid")
    @Expose
    var puuid: String? = null

    @SerializedName("name")
    @Expose
    var name: String? = null

    @SerializedName("profileIconId")
    @Expose
    var profileIconId: Int? = null

    @SerializedName("revisionDate")
    @Expose
    var revisionDate: Int? = null

    @SerializedName("summonerLevel")
    @Expose
    var summonerLevel: Int? = null
}

the fragment in which I want to display the data:

class LolStatFragment : Fragment() {

    private lateinit var mViewModel: LolApiViewModel
    private val apiKey = "api_key=RGAPI-bb27988b-cbb1-4767-b18b-icar8e90c308"


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_lol_stat, container, false)
        mViewModel = ViewModelProviders.of(this).get(LolApiViewModel::class.java)
        return view
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        summoner_search.setOnClickListener {
            val summonerName = summoner_name.text.toString()
            mViewModel.summonerIds(summonerName,apiKey).observe(viewLifecycleOwner,Observer<SummonerData>{
                summoner_ID.text = it.id
                Log.d("LolStatFragment", "LolStatFragment:" + it.id)
                Toast.makeText(context, "zzzzzzzzz ${it.id}", Toast.LENGTH_SHORT).show()
            })

        }
    }
}

here is the result of my retrofit request on a web browser:

{"id":"OR5-q4c9Mw3jKXcPZw2lXul0tT7eLf4dYNadYrGhQ9mG8-w","accountId":"gOb2ZjN51iRLnRmDJuR5GmfILqP3x-T3qfbKWaTZ9k3dYw","puuid":"9TgzR6qdI_X9Z6xFzV0nFndITN0LSGKKeJ5fol2Ii1a01l4duKvFwpYGJQvBeYkBLkvJc96Sr7DZMg","name":"Practice","profileIconId":4353,"revisionDate":1619525378251,"summonerLevel":209}

thank you to all those who will take the time to answer me !

PS:this is my first question on the forum, I hope to have been clear and to have asked my question correctly,If there's any detail that I left out for this question, feel free to ask.


Solution

  • I finally found the answer to my problem, the url of the network call was not formatted well. here is the code used to retrieve my call url api in my OnResponse method and compare it to that of the browser:

    Log.d("LolApiRepository", "LolApiRepository:" + resp.toString())
    

    this is what I had to change in my LolApiService interface :

    public interface LolApiService {
        @GET("summoners/by-name/{summonerName}?")
        Call<SummonerData> getSummonerData (@Path("summonerName") String summonerName, @Query("api_key") String key);
    }