Using retrofit how can i add path param in url - android

http://domain.com/5729e7aee4b0f52e476419c0
How can i add this part 5729e7aee4b0f52e476419c0 using Retrofit?

Just do like this
#POST("/method_name/{param}")
public void methodName(#Path("param") String id, Callback<Response> responseCallback);

Related

How to use two fields in body for methos put in retrofit

I have two strings that I should set them in body for my put request. How can do it with retrofit?
#PUT("/user-management/Account/activate")
#FormUrlEncoded
#Headers({ "Content-Type: application/json"})
Call<Verification> activation(#Part("code") String code , #Part("token") String token);
You may try similar code I posted below: For detailed code please post your context or a part of your code.
#Multipart
#PUT("user/photo")
Call<User> updateUser(#Part("photo") RequestBody photo, #Part("description") RequestBody description);
You can pass multiple Strings in Body like this:
Create a Class
public class Verification
{
public String code;
public String token;
}
Set the data to object
Verification loginCredentials = new Verification();
loginCredentials.code= "12345;
loginCredentials.token= "54321";
Call your api
#PUT("/user-management/Account/activate")
Call<Verification> activation(#Body Verificationcredentials);

What is the Interface in This URL in Retrofit

In Get Method:
http://xyz.erprnd.com/api/v1/customers/get?yearmonth=201807
My design interface:
#Headers({ "Content-Type: application/json;charset=UTF-8"})
#GET("customers/{get}/{yearmonthValue}")
Call<CustResponse> CustomerData(#HeaderMap Map<String, String> headers,
#Path("get") String get,
#Query("yearmonth") String yearmonth,
#Path("yearmonthValue") String yearmonthValue,
#Header("Authorization") String authHeader);
What can I change to make this work properly?
First, double check that you are passing "http://xyz.erprnd.com/api/v1" as your baseUrl when building Retrofit.
Then, base on the URL you posted, the interface should look something like:
#Headers({ "Content-Type: application/json;charset=UTF-8"})
#GET("customers/get")
Call<CustResponse> CustomerData(#HeaderMap Map<String, String> headers,
#Header("Authorization") String authHeader,
#Query("yearmonth") String yearmonth);
when you define query parameters, you just need to add the annotation #Query("param_name") and then the value of the variable passed in will be transform intro: param_name=variable_value
Also, I removed your #Path("get") parameter, since it seems that that part of the URL will always be /get and won't change.
Unfortunately, I cannot test it since I don't have authorization to your API.

Retrofit error-Missing either #GET URL or #Url parameter

I am working on Youtube API.
The base URL is <https://www.googleapis.com/youtube/v3/search/>
Request :GET
https://www.googleapis.com/youtube/v3/search?part=snippet&q={search_keyword}&key={API_KEY}
ApiService Interface code-
public interface ApiService {
#GET("")
Call<YoutubeResponse> searchVideos(#Query("part") String part,
#Query("q") String q,#Query("key") String apiKey);
}
The error: java.lang.IllegalArgumentException: Missing either #GET URL or #Url parameter.
in the line of code
Call<YoutubeResponse> call=service.searchVideos("snippet",s, URLConstants.Youtube_API_KEY);
I'm a beginner. Please help!
It's much more semantically correct to use https://www.googleapis.com/youtube/v3/ as your base URL and then declare #GET("search/") on your service method.
That said, if you really want your base URL to be the full path you can use #GET(".") to declare that your final URL is the same as your base URL.

Error in retrofit GET request

I am using retrofit for api calling in my android app. In one of my request url of request is like this:
/v1/employee/1?employeeId=50124
my retrofit method is:
#GET("/v1/xyz/{employeeId}?companyId={companyId}")
void getEmployee(#Path("employeeId")int employeeId, #Path("companyId") int companyId, Callback<List<Model>> callback);
but when i call api it throws error, please help how to append url like this in retrofit GET request.
For dynamic query parameters use #Query.
Try using the below code:
#GET("/v1/xyz/{employeeId}")
void getEmployee(#Path("employeeId")int employeeId, #Query("companyId")
int companyId, Callback<List<Model>> callback);

How #PUT some value in webservice via Retrofit

I wanna send a list of integer with userName and password to WebService some thing like bellow request
UpdateDocumentState(List<int> documentIds, string userName, string password)
But I don't know How to do that ? Use #Post Or #Put ? use #Query Or #Field ? I googled but didn't find any good example or tutorial which explained these well. ( All tutorial I found was about #GET )
could anyone give me some piece of code , how to do that ?
About the use of #PUT or #POST I think you had to get this information from the WebService developers.
Anyway, here sample code for both of Retrofit annotations with or without Callback response.
#POST("your_endpoint")
void postObject(#Body Object object, Callback<Response> callback);
#PUT("/{path}")
String foo(#Path("path") String thePath);
EDIT:
Object is a custom class which represent the data you had to send to the WebService.
public class DataToSend {
public List<Int> myList;
public String username;
public String password;
}
For example when the #POST annotation declaration will be:
#POST
void postList(#Body DataToSend dataToSend, Callback<Response> callback);
and then you call the method using Retrofit service
yourService.postList(myDataToSend, postCallback);

Categories

Resources