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
Related
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);
What is the most common and appropriate way to update single fields of a JSON resource with a PATCH request in Retrofit?
I see 3 ways of doing it:
Using #Body to send the full object, while leaving fields that are not supposed to be updated as null, so GSON will drop them:
#PATCH("posts/{id}")
Call<Post> patchPost(#Path("id") int id, #Body Post post);
Using #FormUrlEncoded and only pass the fields that are supposed to be updated, for example with a #FieldMap.
#FormUrlEncoded
#PATCH("posts/{id}")
Call<Post> patchPost(#Path("id") int id, #FieldMap Map<String, String> fields);
Defining a custom model class that only contains fields that are supposed to be updated, so we don't have to set anything to null.
#PATCH("posts/{id}")
Call<Post> patchPost(#Path("id") int id, #Body PostUpdate postUpdate);
Am I missing other ways? Which one is the most commonly used?
I generally use JSON with RequestBody(Okhttp). JSON object includes just relevant fields. And then I convert it RequestBody. It's like below:
#PATCH(RestConstants.POST_EMPTY)
Call<EmptyResponseModel> postEmpty(#Body RequestBody body);
MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(MEDIA_TYPE_JSON, json.toString()));
I need some help to pass the jsonarray in retrofit post method in Android.
I have create one pojo class List<ReportList> when I call the api that time i convert this pojo class into JSONArray using Gson() like this
JsonArray myCustomArray = new Gson().toJsonTree(ReportsArray).getAsJsonArray();
and than I post this jsonArray into retrofit post method like this
#FormUrlEncoded
#POST(midURL)
Call<Json> ReportsManagement(#Field(fClass) String classs,
#Field(fMethod) String method,
#Field(fType) String type,
#Field(fUserID) String userID,
#Field(fDate) String Date,
#Field(fReportsArray) String ReportsArray,
#Field(fEntryByUser) String EntryByUser);
Now when I call the api that time I have show some issue in passing the json data in api in android studio logcat.
class=crud&method=ReportsManagement&Type=ADD&UserID=8&Date=2018-06-08&ReportsArray=[{"Date":"08-06-2018","IV1":"","IV10":"","IV11":"","IV12":"","IV13":"","IV14":"","IV15":"","IV16":"","IV17":"","IV18":"","IV19":"","IV2":"","IV20":"","IV21":"","IV22":"","IV23":"","IV24":"","IV25":"","IV26":"","IV3":"","IV4":"","IV5":"","IV6":"","IV7":"","IV8":"","IV9":"","IVF":""},{"Date":"08-06-2018","FIO2":"","IV1":"","IV10"%06-08 08:13:07.461 23291-23476/com.abc D/OkHttp:3A"","IV11":"","IV12":"","IV13":"","IV14":"","IV15":"","IV16":"","IV17":"","IV18":"","IV19":"","IV2":"","IV20":"","IV21":"","IV22":"","IV23":"","IV24":"","IV25":"","IV26":"","IV3":"","IV4":"","IV5":"","IV6":"","IV7":"","IV8":"","IV9":"","IVF":""}]
Error in json :%06-08 08:13:07.461 23291-23476/com.abc D/OkHttp:3A
I want to send this parameter in request and tried to send a simple array list but that was not working
"Language": [
"string","string","string"
]
If "Language" is key and ["string1","string2","string3"] is value then create method in interface as below.
#FormUrlEncoded
#POST(LINK_API)
Call<ResponseModel> getResponse(#Field("Language") String languageArray);
and call it using interface instance like this :
JSONArray languageArray = new JSONArray();
languageArray.add("string1");
languageArray.add("string2");
languageArray.add("string3");
String langArray = languageArray.toString();
Call<ResponseModel> responseModel = apiObject.getResponse(langArray);
responseModel.enqueue(...);
This will work perfectly.
try post query with field parameter as string..
convert your jsonObject to String using,
String b = json_object.toString();
At Server Side, convert string back to JsonObject or whatever else you need.
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.