你好,我正在研究改造,我遇到了这个问题
这是我的密码
ApiClient:-
object ApiClient {
var BASE_URL:String="http://192.168.1.6/Matloob/"
val getClient: ApiInterface
get() {
val gson = GsonBuilder()
.setLenient()
.create()
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
val client = OkHttpClient.Builder().addInterceptor(interceptor).build()
val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
return retrofit.create(ApiInterface::class.java)
}
}
API接口:-
interface ApiInterface {
@GET("getHistory.php")
fun getHistory(
@Field("toemail") toemail:String,
@Field("toname") toname:String
): Call<List<DataModel>>
}
数据模型:-
data class DataModel(
@SerializedName("name")
var name: String,
@SerializedName("email")
var email: String,
@SerializedName("phone")
val number: String,
@SerializedName("data")
val data: String,
@SerializedName("time")
val time: String,
@SerializedName("image")
var image: String,
@SerializedName("lat")
var lat: Double,
@SerializedName("lng")
val lng: Double,
@SerializedName("rate")
val rate: Double,
@SerializedName("ratecount")
val ratecount: Int
)
功能是主活动:-
private fun getDat1a() {
val call: Call<List<DataModel>> = ApiClient.getClient.getHistory("khairo.humsi@mail.ru", "khairo humsi")
call.enqueue(object : Callback<List<DataModel>> {
override fun onResponse(call: Call<List<DataModel>>?, response: Response<List<DataModel>>?) {
pd.dismiss()
list.addAll(response!!.body()!!)
recyclerView.adapter?.notifyDataSetChanged()
}
override fun onFailure(call: Call<List<DataModel>>?, t: Throwable?) {
pd.dismiss()
}
})
}
最后这是我的适配器
class DataAdpter(private var list: List<DataModel>, private val context: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun onBindViewHolder(p0: RecyclerView.ViewHolder, p1: Int) {
(p0 as ItemView).bind(list[p1].name, list[p1].email, list[p1].number, list[p1].data, list[p1].time, list[p1].image, list[p1].lat, list[p1].lng, list[p1].rate, list[p1].ratecount)
}
override fun getItemCount(): Int {
return list.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return ItemView(LayoutInflater.from(context).inflate(R.layout.row_history, parent, false))
}
class ItemView(itemVeiw: View) : RecyclerView.ViewHolder(itemVeiw) {
fun bind(name: String, email: String, number: String
, data: String, time: String, image: String, lat: Double, lng: Double, rate: Double, ratecount: Int) {
itemView.name.text= name
itemView.historyRate.isEnabled= false
itemView.emailtext.text= email
itemView.phonetext.text= number
itemView.datatext.text= data
itemView.timetext.text= time
Picasso.with(itemView.context).load(image).into(itemView.ivRowCategoryImage)
itemView.lat.text= lat.toString()
itemView.lng.text= lng.toString()
itemView.historyRate.rating = ((rate/ratecount).toFloat())
}
}
}
interface ApiInterface {
@GET("getHistory.php")
fun getHistory(
@query("toemail") toemail:String,
@query("toname") toname:String
): Call<List<DataModel>>
}
@Field()
用于带有@FormUrlEncoded
的@POST
。您应该改用@Query()
!