无法使用 Retrofit2 @GET 和 @Path 发送参数

Unable to send parameters using Retrofit2 @GET and @Path

我想使用以下代码将带有 ID 参数的获取请求发送到我的 asp.net rest api。我要实现的格式是 https://randomwebapi.net/api/Tblathletes/id but instead the end result it always ends up being https://randomWebApi.net/api/Tblathletes/,其中 returns 所有运动员。它总是在末尾省略 ID。还有其他方法可以实现这种格式吗?

@GET("TblAthletes/{id}")
fun getAthlete(@Path("id")id:String?): Call<List<tblAthlete>>

调用代码如下

    private fun getAthletesdata(athleteID: String?) {
//        val filter = HashMap<String?,String?>()
//        filter.put("Id",athleteID)
        val call = mAPIService?.getAthlete(athleteID)
        call?.enqueue(object : Callback<List<tblAthlete>> {
            override fun onResponse(call: Call<List<tblAthlete>>, response: Response<List<tblAthlete>>) {
                val athletes:List<tblAthlete> = response.body()!!
                txtName?.setText((athletes[0].name))
                txtSurname?.setText((athletes[0].surname))
                txtDOB?.setText((athletes[0].dob))
                txtAthleteNumber?.setText((athletes[0].athletenumber))
                txtID?.setText((athletes[0].athleteidno))
            }

            override fun onFailure(call: Call<List<tblAthlete>>, t: Throwable) {
                Toast.makeText(this@EditAthleteActivity.context, t.message, Toast.LENGTH_SHORT).show()
            }
        })

    }

下面是我的asp.net代码

        [HttpGet]
        public async Task<ActionResult<IEnumerable<Tblathletegroup>>> GetTblathletegroup()
        {
            return await _context.Tblathletegroup.ToListAsync();
        }

        // GET: api/Tblathletegroups/5
        [HttpGet("{id}")]
        public async Task<ActionResult<Tblathletegroup>> GetTblathletegroup(string id)
        {
            var tblathletegroup = await _context.Tblathletegroup.FindAsync(id);

            if (tblathletegroup == null)
            {
                return NotFound();
            }

            return tblathletegroup;
        }

下面是 OKHttp 日志

2020-06-25 22:30:25.358 13749-13811/com.sprint_coach.shaun.sprint_coach D/OkHttp: --> GET https://RandomWebApi.net/api/TblAthletes/82ce9446-2776-41bd-bde8-3df6f924930a
2020-06-25 22:30:25.358 13749-13811/com.sprint_coach.shaun.sprint_coach D/OkHttp: --> END GET
2020-06-25 22:30:25.383 13749-13822/com.sprint_coach.shaun.sprint_coach D/OkHttp: --> GET https://RandomWebApi.net/api/TblAthletes/
2020-06-25 22:30:25.383 13749-13822/com.sprint_coach.shaun.sprint_coach D/OkHttp: --> END GET
2020-06-25 22:30:26.033 13749-13811/com.sprint_coach.shaun.sprint_coach D/OkHttp: <-- 200 OK https://RandomWebApi.net/api/TblAthletes/82ce9446-2776-41bd-bde8-3df6f924930a (674ms)
2020-06-25 22:30:26.033 13749-13811/com.sprint_coach.shaun.sprint_coach D/OkHttp: Content-Type: application/json; charset=utf-8
2020-06-25 22:30:26.033 13749-13811/com.sprint_coach.shaun.sprint_coach D/OkHttp: Vary: Accept-Encoding
2020-06-25 22:30:26.033 13749-13811/com.sprint_coach.shaun.sprint_coach D/OkHttp: Server: Microsoft-IIS/10.0
2020-06-25 22:30:26.034 13749-13811/com.sprint_coach.shaun.sprint_coach D/OkHttp: Request-Context: appId=cid-v1:037be9f0-7309-4880-8975-6ae211302d2b
2020-06-25 22:30:26.034 13749-13811/com.sprint_coach.shaun.sprint_coach D/OkHttp: X-Powered-By: ASP.NET
2020-06-25 22:30:26.034 13749-13811/com.sprint_coach.shaun.sprint_coach D/OkHttp: Set-Cookie: ARRAffinity=7b498185396ab6e22ba34a59367270862958d11522d339239b2c110274a21354;Path=/;HttpOnly;Domain=RandomWebApi.net
2020-06-25 22:30:26.034 13749-13811/com.sprint_coach.shaun.sprint_coach D/OkHttp: Date: Thu, 25 Jun 2020 14:30:25 GMT
2020-06-25 22:30:26.039 13749-13811/com.sprint_coach.shaun.sprint_coach D/OkHttp: {"id":"82ce9446-2776-41bd-bde8-3df6f924930a","name":"Ronnie","surname":"Colman","dob":"1962","isDeleted":null,"modifiedUserId":null,"createdUserId":null,"createdDateTime":"2020-06-25T07:55:21","modifiedDateTime":null,"athleteNumber":"464664","athleteIdno":"643464","coachId":"Bob"}
2020-06-25 22:30:26.039 13749-13811/com.sprint_coach.shaun.sprint_coach D/OkHttp: <-- END HTTP (281-byte body)
2020-06-25 22:30:28.689 13749-13822/com.sprint_coach.shaun.sprint_coach D/OkHttp: <-- 200 OK https://RandomWebApi.net/api/TblAthletes/ (3305ms)
2020-06-25 22:30:28.689 13749-13822/com.sprint_coach.shaun.sprint_coach D/OkHttp: Content-Type: application/json; charset=utf-8
2020-06-25 22:30:28.689 13749-13822/com.sprint_coach.shaun.sprint_coach D/OkHttp: Vary: Accept-Encoding
2020-06-25 22:30:28.689 13749-13822/com.sprint_coach.shaun.sprint_coach D/OkHttp: Server: Microsoft-IIS/10.0
2020-06-25 22:30:28.689 13749-13822/com.sprint_coach.shaun.sprint_coach D/OkHttp: Request-Context: appId=cid-v1:037be9f0-7309-4880-8975-6ae211302d2b
2020-06-25 22:30:28.690 13749-13822/com.sprint_coach.shaun.sprint_coach D/OkHttp: X-Powered-By: ASP.NET
2020-06-25 22:30:28.690 13749-13822/com.sprint_coach.shaun.sprint_coach D/OkHttp: Set-Cookie: ARRAffinity=7b498185396ab6e22ba34a59367270862958d11522d339239b2c110274a21354;Path=/;HttpOnly;Domain=RandomWebApi.net
2020-06-25 22:30:28.690 13749-13822/com.sprint_coach.shaun.sprint_coach D/OkHttp: Date: Thu, 25 Jun 2020 14:30:27 GMT
2020-06-25 22:30:28.698 13749-13822/com.sprint_coach.shaun.sprint_coach D/OkHttp: [{"id":"0f812b28-7285-11ea-b9a2-b42e9918ef25","name":"Shaun","surname":"Johnson","dob":"1992","isDeleted":null,"modifiedUserId":null,"createdUserId":null,"createdDateTime":"2020-03-30T20:50:40","modifiedDateTime":null,"athleteNumber":"6464","athleteIdno":"646464","coachId":"Bob"},{"id":"82ce9446-2776-41bd-bde8-3df6f924930a","name":"Ronnie","surname":"Colman","dob":"1962","isDeleted":null,"modifiedUserId":null,"createdUserId":null,"createdDateTime":"2020-06-25T07:55:21","modifiedDateTime":null,"athleteNumber":"464664","athleteIdno":"643464","coachId":"Bob"},{"id":"97171a8d-e2ca-454e-ad1e-491156c53cfc","name":"Bill","surname":"Burns","dob":"1985","isDeleted":null,"modifiedUserId":null,"createdUserId":null,"createdDateTime":"2020-06-25T10:09:41","modifiedDateTime":null,"athleteNumber":"222","athleteIdno":null,"coachId":"Bob"}]
2020-06-25 22:30:28.698 13749-13822/com.sprint_coach.shaun.sprint_coach D/OkHttp: <-- END HTTP (835-byte body)

已将对象类型从 Call 更改为 Call。因为我只返回一行,所以返回的 Java 脚本不正确。现在可以使用了。