Pixelherz
Pixelherz

Reputation: 21

java.lang.IllegalArgumentException: No Retrofit annotation found. (parameter #2

this is my API:

import okhttp3.RequestBody
import okhttp3.ResponseBody
import retrofit2.Response
import retrofit2.http.*

interface BowBuddyAPI {
    
        
    
        @POST("/api/v1/create")
        suspend fun createEmployee(@Body requestBody: RequestBody): Response<RequestBody>
    
    
    
    }

And this is my function where I make the request:

fun sendEmp() {
        Log.i("Click", "Function call")
        val retrofit = Retrofit.Builder()
            .baseUrl("https://dummy.restapiexample.com")
            .build()
        val service = retrofit.create(BowBuddyAPI::class.java)
   
        val jsonObj = JSONObject()
        jsonObj.put("name", "Jack")
        jsonObj.put("salary", "3540")
        jsonObj.put("age", "23")

        val testbody = jsonObj.toString().toRequestBody(("application/json".toMediaTypeOrNull()))

        GlobalScope.launch(Dispatchers.Main) {
            val response = service.createEmployee(testbody)
            if (response.isSuccessful) {
                withContext(Dispatchers.Main) {
                    if (response.isSuccessful) {
                        Log.i("Success", response.toString())
                    } else {
                        Log.i("ERROR", response.code().toString())
                    }

                }
            }


        }
    }

Calling it via:

override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item.itemId){
            R.id.menu_item_save -> sendEmp()
        }
        return true
    }
}

I get this error:

2022-05-02 15:11:41.758 10006-10006/com.example.bowbuddyapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.bowbuddyapp, PID: 10006
    java.lang.IllegalArgumentException: No Retrofit annotation found. (parameter #2)
        for method BowBuddyAPI.createEmployee
        at retrofit2.Utils.methodError(Utils.java:52)
        at retrofit2.Utils.methodError(Utils.java:42)
        at retrofit2.Utils.parameterError(Utils.java:61)
        at retrofit2.RequestFactory$Builder.parseParameter(RequestFactory.java:311)
        at retrofit2.RequestFactory$Builder.build(RequestFactory.java:182)
        at retrofit2.RequestFactory.parseAnnotations(RequestFactory.java:65)
        at retrofit2.ServiceMethod.parseAnnotations(ServiceMethod.java:25)
        at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:168)
        at retrofit2.Retrofit$1.invoke(Retrofit.java:147)
        at java.lang.reflect.Proxy.invoke(Proxy.java:1006)
        at $Proxy0.createEmployee(Unknown Source)
        at com.example.bowbuddyapp.CreateParcoursActivity$sendEmp$1.invokeSuspend(CreateParcoursActivity.kt:48)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

I cant find any solution. The closest one I found is this: No Retrofit annotation found. (parameter #2)

But this is not a solution for my problem since my params in the API functions have their annotations right?

EDIT: Ok I found a solution: My retrofit was outdated. Changed it from 2.5.0 to 2.9.0

OffTopic: In case someone wants to use the code above change Response<RequestBody> to Response<ResponseBody>

The code above is based on this project.

Upvotes: 1

Views: 1005

Answers (1)

Navas pk
Navas pk

Reputation: 349

This could be because of first reason, mismatch of annotation like @Field instead of @Body or @Query instead of @Field etc.

And second reason as Pixelherz mentioned, if we are using Retrofit with RxJava as CallAdapterFactory then version com.squareup.retrofit2:retrofit:2.5.0 is fair enough, where as if we are using Coroutine with Retrofit then always we need to use com.squareup.retrofit2:retrofit:2.9.0 or above

Upvotes: 1

Related Questions