如何在我的应用程序中修复IllegalArgumentException改造

时间:2019-09-13 13:31:59

标签: android kotlin retrofit

您好,我正在尝试研究改造,但遇到了这个问题

  

进程:kotlincodes.com.retrofitwithkotlin,PID:14957       java.lang.RuntimeException:无法启动活动ComponentInfo {kotlincodes.com.retrofitwithkotlin / kotlincodes.com.retrofitwithkotlin.activity.MainActivity}:java.lang.IllegalArgumentException:@Field参数只能与表单编码一起使用。 (参数1)           用于方法ApiInterface.getHistory           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)           在android.app.ActivityThread.-wrap11(ActivityThread.java)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344)           在android.os.Handler.dispatchMessage(Handler.java:102)           在android.os.Looper.loop(Looper.java:148)           在android.app.ActivityThread.main(ActivityThread.java:5417)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)        原因:java.lang.IllegalArgumentException:@Field参数只能与表单编码一起使用。 (参数1)           用于方法ApiInterface.getHistory           在retrofit2.ServiceMethod $ Builder.methodError(ServiceMethod.java:752)           在retrofit2.ServiceMethod $ Builder.methodError(ServiceMethod.java:743)           在retrofit2.ServiceMethod $ Builder.parameterError(ServiceMethod.java:761)           在retrofit2.ServiceMethod $ Builder.parseParameterAnnotation(ServiceMethod.java:533)           在retrofit2.ServiceMethod $ Builder.parseParameter(ServiceMethod.java:336)           在retrofit2.ServiceMethod $ Builder.build(ServiceMethod.java:204)           在retrofit2.Retrofit.loadServiceMethod(Retrofit.java:170)           在retrofit2.Retrofit $ 1.invoke(Retrofit.java:147)           在java.lang.reflect.Proxy.invoke(Proxy.java:393)           在$ Proxy0.getHistory(未知来源)           在kotlincodes.com.retrofitwithkotlin.activity.MainActivity.getDat1a(MainActivity.kt:41)//这是mainActivity中的错误           在kotlincodes.com.retrofitwithkotlin.activity.MainActivity.onCreate(MainActivity.kt:36)           在android.app.Activity.performCreate(Activity.java:6237)           在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)

这是我的密码

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)

    }
}

ApiInterface:-

interface ApiInterface {

@GET("getHistory.php")
fun getHistory(
    @Field("toemail") toemail:String,
    @Field("toname") toname:String
): Call<List<DataModel>>

}

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())


    }
}

}

2 个答案:

答案 0 :(得分:2)

interface ApiInterface {

@GET("getHistory.php")
fun getHistory(
    @query("toemail") toemail:String,
    @query("toname") toname:String
): Call<List<DataModel>>

}

答案 1 :(得分:0)

@Field()用于@POST@FormUrlEncoded。您应该改用@Query()