简体   繁体   中英

What is the right way to pass the parameter in the retrofit GET call in Android?

I wanted to fetch the data from the AWS server in android app and the way I'm using retrofit for this. This part has been resolved.

I wanted to fetch the data with method ONE which is not working.

Please consider that the base URL has no problem <DOMAIN/> .

Method ONE:

@GET("/release-1a/vendor/getCustomerProfile")
@Headers("Accept-type: application/json")
fun getCustomerProfile(
        @Query("appId") appId: String?,
        @Query("clientId") clientId: String?,
        @Query("clientPhone") clientPhone: String?
): Observable<GetCustomerProfileResponse?>?

Network Request

CustomerApi customerApi = RetrofitBuilder.getInstance(NEW_CUSTOMER_PROFILE_URL).create(CustomerApi.class);
    Observable<GetCustomerProfileResponse> observable =
            customerApi.getCustomerProfile("4", "5", "%2B919829732808");

While using this method I'm getting the error Code 400 which means bad request. but when I use Method two I got the desired result.

Method TWO:

@GET("/release-1a/vendor/getCustomerProfile?appId=4&clientId=5&clientPhone=%2B919829732808")
@Headers("Accept-type: application/json")
fun getCustomerProfile(): Observable<GetCustomerProfileResponse?>?

Network Request

CustomerApi customerApi = RetrofitBuilder.getInstance(NEW_CUSTOMER_PROFILE_URL).create(CustomerApi.class);
    Observable<GetCustomerProfileResponse> observable =
            customerApi.getCustomerProfile();

I see no difference in them. So now I want to know about the correct practice.

You see, the actual value that you're passing to AWS is +919829732808, but "+" cannot be present in URL so it gets encoded as %2B.

You could call:

customerApi.getCustomerProfile("4", "5", "+919829732808");

But when you call it like:

customerApi.getCustomerProfile("4", "5", "%2B919829732808");

Retrofit makes additional url encoding (% is replaced with %25) and AWS gets %2B919829732808 instead of +919829732808. So there are 2 ways - call it with "+" and Retrofit encodes it to %2B or call it with %2B and encoded=true. Have a look at url encoding description: https://www.w3schools.com/tags/ref_urlencode.ASP

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM