【发布时间】:2021-12-29 10:26:44
【问题描述】:
我正在为我的 android 应用程序使用 Retrofit,但很少有参数返回为 null,而其他参数有值,它在 POSTMAN 上运行良好。
我用于 Api 调用的函数:
private fun apiCall(search: String): List<SortedApiData> {
var recyclerDataList: List<SortedApiData> = ArrayList<SortedApiData>()
val consumer = OkHttpOAuthConsumer("9c6c751a78db41b9a8bec92ef28c7656", "3f302d7930a74c1db0304ce675d4d41b")
consumer.setTokenWithSecret("","")
val client1 = OkHttpClient.Builder()
.addInterceptor(SigningInterceptor(consumer))
.build()
var retrofit : Retrofit = Retrofit.Builder()
.baseUrl(BaseUrl)
.client(client1)
.addConverterFactory(GsonConverterFactory.create())
.build()
val api: Client = retrofit.create(Client::class.java)
val call: retrofit2.Call<ApiData?>? = api.getResponse(search, 5, 0, 0)
call?.enqueue(object : Callback<ApiData?> {
override fun onFailure(call: retrofit2.Call<ApiData?>, t: Throwable?) {
Log.v("retrofit", t.toString())
}
override fun onResponse(call: retrofit2.Call<ApiData?>, response: Response<ApiData?>) {
val data: ApiData? = response.body()
popUprecycler.visibility = View.VISIBLE
Log.v("retrofit", data.toString())
recyclerDataList = createList(data)
adapter = PopUpAdapter(this@PopPage, recyclerDataList)
popUprecycler.layoutManager = GridLayoutManager(applicationContext, 2)
popUprecycler.itemAnimator = DefaultItemAnimator()
popUprecycler.setHasFixedSize(true)
popUprecycler.adapter = adapter
Log.i("Shrey", "shit")
}
})
return recyclerDataList
}
@GET请求函数:
interface Client {
@GET("{id}/icons")
fun getResponse(@Path("id") id: String?,@Query("limit") limit: Int?,@Query("offset") offset: Int?,@Query("page") page: Int?): Call<ApiData?>?
}
模型类:
public class ApiData{
private int total;
private String generatedAt;
private Collection collection;
private List<IconsItem> icons;
public int getTotal(){
return total;
}
public String getGeneratedAt(){
return generatedAt;
}
public Collection getCollection(){
return collection;
}
public List<IconsItem> getIcons(){
return icons;
}
}
图标模型类:
public class IconsItem{
private Sponsor sponsor;
private String uploaderId;
private String isActive;
private String nounjiFree;
private int year;
private String dateUploaded;
private Object sponsorCampaignLink;
private int termId;
private String sponsorId;
private String termSlug;
private String licenseDescription;
private List<TagsItem> tags;
private String attributionPreviewUrl;
private String updatedAt;
private String previewUrl;
private Uploader uploader;
private String attribution;
private String previewUrl42;
private String freemium;
private String term;
private String id;
private String permalink;
private String isExplicit;
private String previewUrl84;
public Sponsor getSponsor(){
return sponsor;
}
public String getUploaderId(){
return uploaderId;
}
public String getIsActive(){
return isActive;
}
public String getNounjiFree(){
return nounjiFree;
}
public int getYear(){
return year;
}
public String getDateUploaded(){
return dateUploaded;
}
public Object getSponsorCampaignLink(){
return sponsorCampaignLink;
}
public int getTermId(){
return termId;
}
public String getSponsorId(){
return sponsorId;
}
public String getTermSlug(){
return termSlug;
}
public String getLicenseDescription(){
return licenseDescription;
}
public List<TagsItem> getTags(){
return tags;
}
public String getAttributionPreviewUrl(){
return attributionPreviewUrl;
}
public String getUpdatedAt(){
return updatedAt;
}
public String getPreviewUrl(){
return previewUrl;
}
public Uploader getUploader(){
return uploader;
}
public String getAttribution(){
return attribution;
}
public String getPreviewUrl42(){
return previewUrl42;
}
public String getFreemium(){
return freemium;
}
public String getTerm(){
return term;
}
public String getId(){
return id;
}
public String getPermalink(){
return permalink;
}
public String getIsExplicit(){
return isExplicit;
}
public String getPreviewUrl84(){
return previewUrl84;
}
}
API 响应注意突出显示的值不应为空:
正确的 API 响应:
【问题讨论】:
标签: android api kotlin retrofit retrofit2