Android Retrofit api link presntation - android

I'm still beginner on retrofit API for android, but I still didn't get it !!
I know about the Annotation #Path and #Query but I still don't know what is the use of #Field
and I also know about #POST and #GET but I don't know what is #PUT
and one last question.. lets say that in my API I created the following service.
#GET("/bookmarks")
public abstract void bookmarks(#Query("countryCode") String paramString, #Query("limit") int paramInt1, Callback<BookmarksResult> paramCallback);
how this call is actually presented as a link?? I mean would it be like this
http://www.example.com/api/bookmarks?countryCode=X&limit=X
please some help my whole day on this and I still don't have good answers
thanks

if your baseUrl is http://www.example.com/api the answer is yes. The url will be resolved in
http://www.example.com/api/bookmarks?countryCode=X&limit=X
and the same applies for the other request methods.
I would use #QueryMap instead of passing more than one #Query, but that's more a matter of taste.
I just notice that your method is marked as abstract. I am pretty sure that one of the constraints of retrofit is that you have to use an interface to declare your endpoints

Related

How to resolve "#Url cannot be used with #PUT URL (parameter #1)"

I want to use #PUT and #Url together, but it throws an IllegalArgumentException
Edit
#PUT
fun editPost(
#Url() url: String = "xxxx/threads/{tid}",
#Path("tid") postId:Long,
#Body x: X
)
This answer is based on the assumption of you using Retrofit Library to make a API call. If that's not the case, my apologies and let me know so I can modify/remove the answer.
From what I've researched, you've probably implemented your interface method like the following:
#PUT("")
Call...
With this call, you should encounter java.lang.IllegalArgumentException: Missing either #GET URL or #Url parameteras you've not provided the addition parameter which is needed to complete the API call.
Therefore, you must keep the base URL in the mainActivity where you'll be making the call and route of the API within the bracket of the interface
#PUT("user/id")
or if you want to keep it blank as original, you must use #PUT(".") as this will declare that your final URL is the same as the Base URL provided in the mainActivity.
You were very close...
#PUT("xxxx/threads/{tid}"
fun editPost(
#Path("tid") postId: Long,
#Body x: X
)

Retrofit2: #Path parameter in OkHttp Interceptor

In our API we have sign based on both #Query and #Path parameters. Everyone suggests to use OkHttp Interceptor for this. Everything is fine with query params, but I don't think there is any way to get path parameter values and names. For example:
/api/{version}/books/{id}
/api/v1.1/books/10
To make a correct sign, I need Map:
{"id":"10", "version":"v1.1"}
Am I missing something?
Follow the following code, probably your problem will be solved.
/api/{version}/books/{id}
public type method(#Path("version") String versionValue,#Path("id") String idValue,.....)

what is the difference between #Query and #path in Retrofit?

I wanted to use retrofit library for parsing and posting the data by passing some parameters. But When defining model class some times we will use #Serialized in-front of variable, What is the use of that Serialized.And What is the difference between #Get and #Query in passing params to API.Can Any one explain the difference.
Lets say you have api method #GET("/api/item/{id}/subitem/") so by using #Path("id") you can specify id for item in path. However your api may take additional parameters in query like sort, lastupdatetime, limit etc so you add those at end of url by #Query(value = "sort") String sortQuery
So full method will look like:
#GET("/api/item/{id}/subitem")
SubItem getSubItem(#Path("id") int itemId, #Query("sort") String sortQuery, #Query("limit") int itemsLimit);
and calling api.getSubItem(5, "name", 10) will produce url #GET("/api/item/5/subitem/?sort=name&limit=10")
and #Get is HTTP method
http://www.w3schools.com/tags/ref_httpmethods.asp says
Two commonly used methods for a request-response between a client and
server are: GET and POST.
GET - Requests data from a specified resource POST - Submits data to
be processed to a specified resource
#GET is request method. You mark method with that.
#Query is query parameter (i.e. the one in the URL). You mark method parameters with that.
#Serialized probably does not belong to Retrofit, look at its package name (move cursor there and press `Ctrl+Q in Android studio)

Any way to have generic URL using Retrofit

I have several URL that accepts the same GET parameters (mainly for pagination purposes) as follow :
public interface AsynchronousApi {
#GET("/api/users")
public void listUsers(#Query("limit") Integer limit,
#Query("offset") Integer offset,
Callback<userList> callback);
#GET("/api/posts")
public void listPosts(#Query("limit") Integer limit,
#Query("offset") Integer offset,
Callback<postList> callback);
...
}
Since I have lots of URL, this is getting a bit repetitive.
So I would like to have way to refactor this so I don't have to repeat #Query("limit") and #Query("offset") everytime. Maybe another annotation would help ?
No. Retrofit values the semantic weight of separate methods much more than de-duplication of interface method declarations.
While the similar behavior of the API endpoints is good API design, it would be poor Java design. If this service was local (i.e., inside your app) you wouldn't consolidate the two methods because they fetch two very different things.
See this other question
Apparently retrofit added an #Url annotation for that purpose.
public interface APIService {
#GET
Call<Users> getUsers(#Url String url);
}
This feature has been added in version 2.0.0.

Make my methods generic in interface or have as many as I want

#POST("/public/login")
void postlogin(
#Body Login login,
Callback<LoginResponse> response
);
#POST("/public/registration")
void postregestration(
#Body Regestration regesteration,
Callback<RegestrationResponse> response
);
I have these two methods,should I combine them both and make a generic Post function, or just leave like this for better readability. "
If I have to make generic, how can I achieve that ? because my callback expects a particular response.
I am new to retrofit,so any kind of help would be highly appreciated.
Keep them separate.
Retrofit's goal is mapping your remote server API to a Java API. In this case, your server has two endpoints which do two very different things. It would make sense then for the interface to have two methods which correspond to those two very different things.

Categories

Resources