Mohammad Khair
Mohammad Khair

Reputation: 476

how to fix IllegalArgumentException retrofit in my app

hello I'm trying to study retrofit and I faced this problem

Process: kotlincodes.com.retrofitwithkotlin, PID: 14957 java.lang.RuntimeException: Unable to start activity ComponentInfo{kotlincodes.com.retrofitwithkotlin/kotlincodes.com.retrofitwithkotlin.activity.MainActivity}: java.lang.IllegalArgumentException: @Field parameters can only be used with form encoding. (parameter #1) for method ApiInterface.getHistory at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.IllegalArgumentException: @Field parameters can only be used with form encoding. (parameter #1) for method ApiInterface.getHistory at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:752) at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:743) at 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) at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:170) at retrofit2.Retrofit$1.invoke(Retrofit.java:147) at java.lang.reflect.Proxy.invoke(Proxy.java:393) at $Proxy0.getHistory(Unknown Source) at kotlincodes.com.retrofitwithkotlin.activity.MainActivity.getDat1a(MainActivity.kt:41) //here is the error in mainActivity at kotlincodes.com.retrofitwithkotlin.activity.MainActivity.onCreate(MainActivity.kt:36) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)

and this is my codes

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
)

and the function is Main Activity:-

private fun getDat1a() {
    val call: Call<List<DataModel>> = ApiClient.getClient.getHistory("[email protected]", "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()
        }

    })
}

and finally this is my adapter

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


    }
}

}

Upvotes: 0

Views: 511

Answers (2)

Maulik Santoki
Maulik Santoki

Reputation: 532

interface ApiInterface {

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

}

Upvotes: 2

Agnaramon
Agnaramon

Reputation: 881

@Field() is for @POST with @FormUrlEncoded. You should use @Query() instead !

Upvotes: 0

Related Questions