【问题标题】:how to fix IllegalArgumentException retrofit in my app如何在我的应用程序中修复 IllegalArgumentException 改造
【发布时间】:2019-09-13 13:31:59
【问题描述】:

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

进程: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)

    }
}

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
)

功能是Main Activity:-

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


    }
}

}

【问题讨论】:

    标签: android kotlin retrofit


    【解决方案1】:
    interface ApiInterface {
    
    @GET("getHistory.php")
    fun getHistory(
        @query("toemail") toemail:String,
        @query("toname") toname:String
    ): Call<List<DataModel>>
    
    }
    

    【讨论】:

    • 抱歉,在 kotlincodes.com.retrofitwithkotlin.activity.MainActivity.getDat1a(MainActivity.kt:41) 出现同样的错误,在 kotlincodes.com.retrofitwithkotlin.activity.MainActivity.onCreate(MainActivity.kt:36)
    • 它是黄色的
    • 检查 github/retrofir 中的问题
    • 使用了 @query 的 Field 实例
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-05
      • 2019-05-27
      • 2016-02-16
      • 2021-08-18
      • 1970-01-01
      • 1970-01-01
      • 2011-12-11
      • 2014-09-04
      相关资源
      最近更新 更多