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);
Related
This is my Api.php
$command=$_POST["command"];
if($command=="getUsers"){
getUsers();
}
elseif ($command=="getNews")
{
getNews();
}
I'm using retrofit to show result in my android app. I need to pass parameter Command through Retrofit Post and Get a result at the same time.
what's the solution?
Try like this example from Retrofit website:
#FormUrlEncoded
#POST("user/edit")
Call<User> updateUser(#Field("first_name") String first, #Field("last_name") String last);
Source: https://square.github.io/retrofit/
I'm familiar with how to use dynamic URLs with Retrofit2 but having issue sending username & password in the request. The webAPI works by entering a URL, a login prompt appears on the screen for a username & password, after authentication a JSON response is displayed. The interface is defined for a dynamic URL:
#GET
public Call<User> getJSON(#Url String string);
My request is as follows:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
LoginService service = retrofit.create(LoginService.class);
Call<User> call = service.getJSON("https://username:password#api.url.com/");
call.enqueue(new Callback<User>() {
#Override
public void onResponse(Call<User> call, retrofit2.Response<User> response) {
System.out.println("Response status code: " + response.code());
I'm certain the URL is correct as it works in the browser & but I keep getting error the username & password aren't correct?
I/System.out: Response status code: 401
Also, as far as I can tell I can only use #GET rather than #POST because whenever I try #POST the response code is:
I/System.out: Response status code: 405
At first I tried to follow something similar to this post using an encoded flag because it's an example of how to use #PATH & #URL with Retrofit2 but didn't have any success. That's why I tried the username:password# prepend to the URL. Most of the other examples all use the #POST method.
Any feedback or ideas on how I can authenticate? Thanks
Not sure how to do it in retrofit, but you can add it via an OkHttp interceptor --
OkHttpClient client = new OkHttpClient().newBuilder().addNetworkInterceptor(
new Interceptor() {
#Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request request = chain.request();
HttpUrl url = request.url();
url = url.newBuilder().username("username").password("password").build();
Request newRequest = request.newBuilder().url(url).build();
return chain.proceed(newRequest);
}
}
).build();
be sure to add this client to your retrofit instance --
Retrofit retrofit = new Retrofit.Builder()
.client(client)
.baseUrl(API_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
Another way to use basic authentication with Retrofit2 would be to pass the authentication string as an argument to your interface method.
So you would change the method signature to:
#GET
public Call<User> getJSON(#Url String string, #Header("Authorization") String myAuthString);
And then call it like this:
Call<User> call = service.getJSON("https://api.url.com/", "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
Where you substitute QWxhZGRpbjpvcGVuIHNlc2FtZQ== for your Base64-encoded username:password string.
If you need to pass the username and password for every API call and want to keep the method signatures clean, it might be better to use the custom OkHttpInterceptor method instead.
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);
I need to execute request via Retrofit :
url = http://ENDPOINT_URL/users/77?expand=followers,image,followees
Code :
#GET(ENDPOINT_URL+"users/{user_id}?expand=" WHAT NEED PRESENT HERE???)
void getUserInfo(#Path("user_id) int userId, String... expand)
For userId i use Path annotation, but how i can say to Retrofit, that I need to use array items separated via comma?
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);