1

Sorry for not being able to express me by the question title.

How I could map parameters for this URL with Retrofit?

 /aaapi.cgi?q={query}-!{syear},{eyear}-!{snfrate},{enfrate}-!{simdbrate},{eimdbrate}-!{genreid}-!{vtype}-!{audio}-!{subtitle}-!{imdbvotes}-!{downloadable}&t=ns&cl={clist}&st=adv&ob={sortby}&p={page}&sa={andor}

I tried to use @Path to replace this "{value}" where there is not an actual "Query" parameter like q={query}. 


@Headers(
        value = ["Accept: application/json", "X-Mashape-Key: xxxxx"])
@GET(
        "/aaapi.cgi-!{syear},{eyear}-!{snfrate},{enfrate}-!{simdbrate},{eimdbrate}-!{genreid}-!{vtype}-!{audio}-!{subtitle}-!{imdbvotes}-!{downloadable}-!cl={clist}-!ob={sortby}-!p={page}")
    fun search(
        @Path("syear") startYear: String = "1970",
        @Path("eyear") endYear: String = "2019",
        @Path("snfrate") startNetflixRate: String = "0",
        @Path("enfrate") endNetflixRate: String = "5",
        @Path("simdbrate") startImdbRate: String = "0",
        @Path("eimdbrate") endImdbRate: String = "10",
        @Path("genreid") genreId: String = "0",
        @Path("vtype") videoType: String = "Movie",
        @Path("audio") audio: String = "Any",
        @Path("subtitle") subtitle: String = "Any",
        @Path("imdbvotes") imdbVotes: String = "1000",
        @Path("downloadable") downloadable: String = "",
        @Path("clist") countryList: String = "29",
        @Path("sortby") sortBy: String = "Date",
        @Path("page") page: String = "1",
        @Query("q") query: String = "get:new7",
        @Query("t") type: String = "ns",
        @Query("st") st: String = "adv",
        @Query("sa") andOr: String = "and"
    ): Call<SearchResponse>

But of course it must not be correct since it generates this wrong url:

https://unogs-unogs-v1.p.mashape.com/aaapi.cgi-!1970,2019-!0,5-!0,10-!0-!Movie-!Any-!Any-!1000-!-!cl=29-!ob=Date-!p=1?q=get%3Anew7&t=ns&st=adv&sa=and

And this is a valid url that returns a correct json file:

https://unogs-unogs-v1.p.mashape.com/aaapi.cgi?q=get%3Anew7-!1900,2018-!0,5-!0,10-!0-!Any-!Any-!Any-!gt100-!Yes&t=ns&cl=all&st=adv&ob=Relevance&p=1&sa=an

EDIT

It works with:

@GET(
    "/aaapi.cgi"
)
fun search(
    @Query("q", encoded = true) query: String = "get:new7",
    @Query("t") type: String = "ns",
    @Query("st") st: String = "adv",
    @Query("ob") orderBy: String = "Date",
    @Query("sa") andOr: String = "and"

): Call<SearchResponse>

And passing the entire query as suggested:

service.search(
        query = "new:7-!1900,2018-!0,5-!0,10-!0-!Any-!Any-!Any-!gt100-!Yes",
        type = "ns",
        st = "adv",
        orderBy = "Relevance",
        andOr = "and"
    )
alexpfx
  • 6,412
  • 12
  • 52
  • 88

1 Answers1

2

without an example, it is unclear what the expected output should be. and assigning values is generally a bad idea, in case it might be intended to substitute these values, according to user input. there are way more fields defined, than the URL has; for example, field q is this whole string here:

?q={query}-!{syear},{eyear}-!{snfrate},{enfrate}-!{simdbrate},{eimdbrate}-!{genreid}-!{vtype}-!{audio}-!{subtitle}-!{imdbvotes}-!{downloadable}

these @Query fields are enough, no @Path parameters are required:

@Headers(value = ["Accept: application/json", "X-Mashape-Key: xxxxx"])
@GET("aaapi.cgi")
fun getResults(
    @Query(value = "q") queryString: String,
    @Query(value = "t") type: String,
    @Query(value = "st") st: String,
    @Query(value = "sa") andOr: String
): Call<SearchResponse>
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • I edit the question and put a valid example. The problem is that all parameters except "{download}" are mandatory. – alexpfx Dec 28 '18 at 23:31
  • @alexpfx that example URL-encodes the `:` (that would be `encoded = false`), nevertheless, the 4 `@Query` fields are enough, no `@Path` parameters are required (that is everything before the `?`) - but passing the whole `q` query-string into. – Martin Zeitler Dec 28 '18 at 23:34
  • I got it. I will give a try – alexpfx Dec 28 '18 at 23:38
  • that @Path annotations would not be useful for this case, right? – alexpfx Dec 28 '18 at 23:40
  • 1
    @alexpfx one could possibly build up the whole URL with `@Path`, while this tends to be problematic; eg. when certain search-parameters are optional, but are part of the definition. by passing the whole `q` query-string at once, one can pass whatever is required. – Martin Zeitler Dec 28 '18 at 23:50