繁体   English   中英

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

[英]how to fix IllegalArgumentException retrofit in my app

你好,我正在尝试学习改造,我遇到了这个问题

进程:kotlincodes.com.retrofitwithkotlin,PID:14957 java.lang.RuntimeException:无法启动活动ComponentInfo {kotlincodes.com.retrofitwithkotlin / kotlincodes.com.retrofitwithkotlin.activity.MainActivity}:java.lang.IllegalArgumentException:@Field参数可以仅与表单编码一起使用。 (参数#1)适用于android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)处的方法ApiInterface.getHistory(适用于android.app.ActivityThread.-wrap11(android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)) android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344)上的ActivityThread.java)android.os.Looper.loop(Looper.java:148)上android.os.Handler.dispatchMessage(Handler.java:102)上的ActivityThread.java) )的android.app.ActivityThread.main(ActivityThread.java:5417)的com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726的java.lang.reflect.Method.invoke(本机方法) ),位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616),原因是:java.lang.IllegalArgumentException:@Field参数只能与表单编码一起使用。 (retrofit2.ServiceMethod $ Builder.methodError(ServiceMethod.java:752)处方法ApiInterface.getHistory的(#1参数)at retrofit2.ServiceMethod $ Builder.methodError(ServiceMethod.java:743)处的Retrofit2.ServiceMethod $ Builder.parameterError(ServiceMethod) .java:761)at retrofit2.ServiceMethod $ Builder.parseParameterAnnotation(ServiceMethod.java:533)at retrofit2.ServiceMethod $ Builder.parseParameter(ServiceMethod.java:336)at 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.Instrumentation.callActivit上的android.app.Activity.performCreate(Activity.java:6237) android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)上的yOnCreate(Instrumentation.java:1107)

这是我的代码

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

}

数据模型:

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()是用于@POST@FormUrlEncoded 您应该使用@Query()代替!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM