Retrofit ws error using #Query - android

I have try this interface :
public interface InterfaceWs
{
#GET("/?extract-mode=bestdeals&api-key={apikey}") public Observable<List<ModelBestDeals>> getBestDeals(#Query("apikey") String apikey);
}
Before using #Query I was using #Path. I change it
and I receive this error :
URL query string "extract-mode=bestdeals&api-key={apikey}" must not have replace block. For dynamic query parameters use #Query.
What is wrong ?

#GET("/?extract-mode=bestdeals&api-key={apikey}") public Observable<List<ModelBestDeals>> getBestDeals(#Query("apikey") String apikey);
should be
#GET("/?extract-mode=bestdeals") public Observable<List<ModelBestDeals>> getBestDeals(#Query("api-key") String apikey);
retrofit will take care of completing your url with api-key=value, where value is the value of apikey. You could also use a QueryMap to provide the other pair extract-mode=bestdeals. E.g.
Map<String, String> map = new HashMap<>();
map.put("extract-mode", "bestdeals");
map.put("api-key", apikey);
and your method
#GET("/") public Observable<List<ModelBestDeals>>
getBestDeals(#QueryMap Map<String, String> values);
which is, in my opinion, way more readable

Related

how to send data to therequest body to the server using retrofit

String Params_body = "{"mobileNumber":"9968599840"}";
String Params_head = "{"clientId":"C11","version":"v1","txnToken":"" + txnToken + ""}";
You have to define your service using the #Header and #Query annotations to indicate the params.
This is just an example of how your service should look like:
public interface RetrofitService {
#GET("/v1/your/endpoint")
Call<YourResponse> networkCall(
#Header("clientId") String clientId,
#Header("version") String version,
#Header("txnToken") String txnToken,
#Query("mobileNumber") String mobileNumber);
}
Then you just need to call the endpoint method and pass as parameter the values.
You can use #Body annotation and pass all the parameter as below class.
class ClienInfo
{
String clientId,
String version,
String txnToken,
String mobileNumber);
}
Use like this:-
#GET("/v1/your/endpoint")
Call<YourResponse> networkCall(#Body ClienInfo clientInfo);

how to send/parse my below json data in android retrofit using GET call

public interface iMakeType {
#Headers({"Content-Type:application/json"})
#GET("/getmaptable?map_type=Model&brand=(here i need to pass my value dynamically)")
MakeResponse getMakeTypeData(#Query("map_type")String map_type);
}
how can i send my brand value to above call dynamically
getmaptable?map_type=Model&brand=BMW
Use this:
public interface iMakeType {
#Headers({"Content-Type:application/json"})
#GET("/getmaptable")
MakeResponse getMakeTypeData(#Query("map_type") String map_type, #Query("brand") String brand);
}
Then:
yourApi.getMakeTypeData("Model", "BMW").enqueue(...)
you can take advantage of #QueryMap from retrofit.
- The #QueryMap annotation expects a Map with key-value-pairs of type String. Each key which has a non-null value will be mapped and you can dynamically add your desired query parameters.
public interface iMakeType {
#Headers({"Content-Type:application/json"})
#GET("/getmaptable")
MakeResponse getMakeTypeData(#QueryMap Map<String, String> map);
}
and implement it like below-
private MakeResponse makeType() {
Map<String, String> data = new HashMap<>();
data.put("map_type", "Model");
data.put("brand", "BMW");
MakeResponse call = api.getMakeTypeData(data);
call.enqueue(…);
}
I hope, this will help you to add #Query Param Dynamically !

Android Retrofit 2 make POST request multiple params (without FormUrlEncoded)

I would like to make a POST request call with multiple params instead of one #Body param. I'm not using #FormUrlEncoded annotation on this one and I don't want to. I'm using Retrofit 2.0.
Currently, the call is made this way :
#POST("user/register")
Call<APIResponse> register(#Body RequestRegisterParams params);
with RequestRegisterParams being :
public class RequestRegisterParams {
public String username;
public String email;
public String password;
}
I would like to be able to do this (with proper annotations of course) :
#POST("user/register")
Call<APIResponse> register(String username, String email, String password);
My goal is to get rid of the data model class. Is there a way to do this or a POST request without #FormUrlEncoded must have only one #Body param ? I know it can only be one #Body param but maybe with other annotations ?
Thanks in advance !
#FormUrlEncoded
#POST("user/register")
Call<APIResponse> updateUser(#Field("username") String username, #Field("email") String email, #Field("password") String password);
#Field is Named pair for a form-encoded request.

Sending various parameters (JSON) using Retrofit 2

This is the interface method:
#FormUrlEncoded
#POST (“url/myurl")
Call<JsonArray> sendParameters (#Header("x-imei") String imei, #Header("x-id-cliente") String idCliente, #Field(“param1") JsonObject param1, #Field(“param2") JsonArray param2, #Field(“param3") JsonArray param3, #Field(“param4") JsonArray param4,#Field(“param5") JsonArray param5);
And the method to use it:
Call<JsonArray> peticion= RetrofitClient.getRetrofitClient(getActivity()).sendParameters(settings.getString("imei", ""), settings.getString("idCliente", "”),param1,param2,param3,param4,param5);
This way, the call is not working.
I tried changing all the interface parameters to String, and doing param1.toString(), param2.toString() and so on in the call, and is not working neither.
Is there a simple way to send JsonObjects and JsonArrays in a POST, using Retrofit 2?
Thank you.
First you can create a custom class which you want to pass as a POST body
class CustomClass {
String param1;
String parma2;
String param3;
//all of your params with getters and setters
}
Then you change the Retrofit method according to your new CustomClass instance with the #Body annotation:
#POST (“url/myurl")
Call<JsonArray> sendParameters (#Header("x-imei") String imei, #Header("x-id-cliente") String idCliente, #Body CustomClass customClassInstance);
And eventually you make the call to post data:
CustomClass customClassInstance = new CustomClass();
customClassInstance.setParam1(...);
//...and so on
Call<JsonArray> peticion= RetrofitClient.getRetrofitClient(getActivity()).sendParameters(settings.getString("imei", ""), settings.getString("idCliente", ""), customClassInstance);
EDIT: removed the #FormUrlEncoded annotation

Pass multiple values to single parameter retrofit2

I'm trying to pass multiple Values to a SINGLE parameter for example :
http://api.giphy.com/v1/gifs?api_key=dc6zaTOxFJmzC&ids=feqkVgjJpYtjy,7rzbxdu0ZEXLy
I tried the following :
#GET("gifs")
Call<GIFModelMain> getGifsByID(#Field("ids")ArrayList<String> values, #Query("api_key") String API_KEY);
In my activity :
ArrayList<String> x = new ArrayList<>();
x.add("feqkVgjJpYtjy");
x.add("7rzbxdu0ZEXLy");
gifCall = interf.getGifsByID(x, BuildConfig.GIPHY_API_TOKEN);
But the built URL is of form:
http://api.giphy.com/v1/gifsids=feqkVgjJpYtjy&ids=7rzbxdu0ZEXLy&api_key=API_KEY_BLANK
I looked up similar questions but found no correct answer.
EDIT: As per what TooManyEduardos said i changed my Interface to
#GET("gifs")
Call<GIFModelMain> getGifsByID(#QueryMap Map<String, String> parameters,#Query("api_key") String API_KEY);
And my activity is now :
Map<String,String> map = new HashMap<>();
map.put("ids","feqkVgjJpYtjy");
map.put("ids","7rzbxdu0ZEXLy");
gifCall = interf.getGifsByID(map, BuildConfig.GIPHY_API_TOKEN);
But the built URL is still :
03-30 02:46:23.922: E/FavActivity(21607): Url : api.giphy.com/v1/gifs?ids=7rzbxdu0ZEXLy&api_key=KEY_HERE
You're looking for a
Map<String,String>
And in your #Get interface, you'll receive it like this:
(#QueryMap Map<String, String> parameters)
So your whole interface call would be like this:
#GET("gifs")
Call<GIFModelMain> getGifsByID(#QueryMap Map<String, String> parameters);
I wrote a whole tutorial on how to use Retrofit 2 if you want to check it out: http://toomanytutorials.blogspot.com/2016/03/network-calls-using-retrofit-20.html
EDIT
If you really want to pass multiple parameters, regardless of their key name, you can always do this:
Call<GIFModelMain> getGifsByID(#Query("api_key") String API_KEY, #Query("ids") String id1, #Query("ids") String id2, #Query("ids") String id3);
The obvious problem here is that you'll have to make multiple versions of the same method depending on how many ids you're passing

Categories

Resources