使用英雄联盟 API 时改造 response.body 为空
Retrofit response.body is null while using league of legends API
我尝试恢复英雄联盟玩家的数据 API 但是对我的请求的响应始终为空,而那些在我的 logcat.[=17 中没有错误消息的响应总是空的=]
这是我的改装电话:
public interface LolApiService {
@GET("summoners/by-name/")
Call<SummonerData> getSummonerData (@Query("summonerName")String summonerName, @Query("key") String key);
}
这是我的存储库:
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
}
}
我在其中检索查询结果的数据模型:
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
}
我要显示数据的片段:
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()
})
}
}
}
这是我在网络浏览器上的改造请求的结果:
{"id":"OR5-q4c9Mw3jKXcPZw2lXul0tT7eLf4dYNadYrGhQ9mG8-w","accountId":"gOb2ZjN51iRLnRmDJuR5GmfILqP3x-T3qfbKWaTZ9k3dYw","puuid":"9TgzR6qdI_X9Z6xFzV0nFndITN0LSGKKeJ5fol2Ii1a01l4duKvFwpYGJQvBeYkBLkvJc96Sr7DZMg","name":"Practice","profileIconId":4353,"revisionDate":1619525378251,"summonerLevel":209}
感谢所有愿意花时间回答我的人!
PS:这是我在论坛上的第一个问题,我希望已经清楚并正确地提出了我的问题,如果我遗漏了这个问题的任何细节,请随时提问.
我终于找到了问题的答案,网络调用的url格式不正确。这是用于在我的 OnResponse 方法中检索我的调用 url api 并将其与浏览器的调用进行比较的代码:
Log.d("LolApiRepository", "LolApiRepository:" + resp.toString())
这是我必须在 LolApiService 接口中更改的内容:
public interface LolApiService {
@GET("summoners/by-name/{summonerName}?")
Call<SummonerData> getSummonerData (@Path("summonerName") String summonerName, @Query("api_key") String key);
}
我尝试恢复英雄联盟玩家的数据 API 但是对我的请求的响应始终为空,而那些在我的 logcat.[=17 中没有错误消息的响应总是空的=]
这是我的改装电话:
public interface LolApiService {
@GET("summoners/by-name/")
Call<SummonerData> getSummonerData (@Query("summonerName")String summonerName, @Query("key") String key);
}
这是我的存储库:
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
}
}
我在其中检索查询结果的数据模型:
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
}
我要显示数据的片段:
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()
})
}
}
}
这是我在网络浏览器上的改造请求的结果:
{"id":"OR5-q4c9Mw3jKXcPZw2lXul0tT7eLf4dYNadYrGhQ9mG8-w","accountId":"gOb2ZjN51iRLnRmDJuR5GmfILqP3x-T3qfbKWaTZ9k3dYw","puuid":"9TgzR6qdI_X9Z6xFzV0nFndITN0LSGKKeJ5fol2Ii1a01l4duKvFwpYGJQvBeYkBLkvJc96Sr7DZMg","name":"Practice","profileIconId":4353,"revisionDate":1619525378251,"summonerLevel":209}
感谢所有愿意花时间回答我的人!
PS:这是我在论坛上的第一个问题,我希望已经清楚并正确地提出了我的问题,如果我遗漏了这个问题的任何细节,请随时提问.
我终于找到了问题的答案,网络调用的url格式不正确。这是用于在我的 OnResponse 方法中检索我的调用 url api 并将其与浏览器的调用进行比较的代码:
Log.d("LolApiRepository", "LolApiRepository:" + resp.toString())
这是我必须在 LolApiService 接口中更改的内容:
public interface LolApiService {
@GET("summoners/by-name/{summonerName}?")
Call<SummonerData> getSummonerData (@Path("summonerName") String summonerName, @Query("api_key") String key);
}