Sending Array as Param in Retrofit Get Request A - android

I am trying to send an array as parameter in GET request in Retrofit
Eg: prod = [1,2,3,4]
Tried almost every solution present on Stack OverFlow, GitHub
Non of them worked
Can someone explain how to make an array and send get request in RetroFit
Thankyou in advance!

You can try like this
#GET(".....")
Call<ResponseBody> getSomthingApi(#Query("prod") ArrayList<String> values);

Related

Retrofit converted POST to GET

I have really strange problem. Use retrofit to send request.
#POST("api/v2/reviews/vote/{vote}/dislike/{dislike}")
Single<SimpleResponse> postReviewLike(#Path("vote") Integer vote, #Path("dislike") Integer dislike);
This is my request. But when I send it to server - retrofit converted it to GET request. Bellow photo for a proof.
Can you suggest, what it can be? And how I can fix it.
With other POST request all is good, if I use #Field.
Could you please try this out using the #Body annotation?
#POST("api/v2/reviews/vote/{vote}/dislike/{dislike}")
Single<SimpleResponse> postReviewLike(#Body #Path("vote") Integer vote, #Path("dislike") Integer dislike);
Problem was with redirect. Adding '/' dislike/{dislike}/ helped me.

Patch request using Retrofit2 not working Android

I have created patch request using Retrofit2.
#PATCH("/tests/{id}")
Call<Test> updateTest(#Path("id") int id, #Body Test test);
This request working using Postman. It returns updated JSON of Test class. But it is not working using Retrofit.
Any help ?
Add "/" at the end of URL.
#PATCH("/tests/{id}/")
well you should display your error but in general in retrofit you should pass your path parameter before so it will be something like this
#PATCH("/tests/{id}")
Call<Test> updateTest(#Path("id") int id, #Body Test test);
hope this helps
please check method (GET , POST , DELETE) and change code to :
#GET("/tests/{id}")

pass two objects in POST retrofit 2.1.0 service

How to post multiple objects in POST retrofit service?
I POST single object and it works fine but now i want to send two objects in POST retrofit web service
Below code for single POST object that works fine
#POST("Device/AddDevice")
Call<String> addDevice(#Body Device value);
I code for multiple objects but it give error
#POST("Device/AddDevice")
Call<String> addDevice(#Body Device deviceValue , #Body User userValue);
Hi Looks like a duplicate. But here the answer from this question
#Multipart
#POST("Device/AddDevice")
void addDevice(#Part("deviceValue") Device coordinates,
#Part("userValue") User maxDistance,
Callback callback);
For more documentation and examples on retrofit have a look at the follwoing page: https://futurestud.io/tutorials/retrofit-getting-started-and-android-client

Retrofit, how to parse JSON objects with variable keys

First I know my title is bad as I didn't come up with better, I'm opened to suggestion.
I'm using retrofit to get data from an api of this kind : #GET("users/{userid}")
It works fine and I'm happy with it, the problem is when I call the same api with #POST("users/widget") with a list of ids. I have the following answer :
{
"long_hash_id": {
"_id": "long_hash_id"
.......
},
"long_hash_id": {
"_id": "long_hash_id",
.....
},
........
}
the "long_hash_id" is typicaly "525558cf8ecd651095af7954"
it correspond to the id of the user attached to it.
When I didn't use retrofit, I used Gson in stream mode to get each user one by one. But I don't know how to tell retrofit.
Hope I'm clear and
Thank you in advance.
----------- Solution :
I made my interface this way :
#FormUrlEncoded
#POST(AppConstants.ROUTE_USER_GROUP)
Call<Map<String,User>> getUsers( #Field("ids") List<String> param, #QueryMap Map<String,String> options);
and I simply gave my ArrayList of ids. Thank you very much
Gson is able to deal with JSON objects with variable keys like the one you posted. What you have to do, in this case, is to declare a Map<String, ModelClass>, where ModelClass is the content of the JSONObject you want to represent

Android Annotations: Query params gets appended to url?

#Get("/ServiceTest?version=1&dateRange={dateRange}")
ResponseModel getDateRangeTest(String dateRange);
in RestClient interface this make the following Get
http://localhost:8080/ServiceTest/2014-01-29%25202014-01-29?version=1" resulted in 400 (Bad Request); invoking error handler
Am i doing something wrong in sending the query params.
I want address this way:
/ServiceTest?version=1&dataRange=2014-01-29%25202014-01-29
Which in this case somehow i am failed to generate with Android annotations #Get
Answer: My calls were not correct and main problem was with uri encode, correct one is
/ServiceTest?version=1&dataRange=2014-01-29%202014-01-29
you might do wrong.
Your server get /ServiceTest
but you access server with address /ServiceTest/2014-01-29%25202014-01-29
Look careful that your server receive as /ServiceTest?version=1&dateRange={dateRange}
and {dataRange} is what you intend to give as variable.
Your address should be /ServiceTest?version=1&dataRange=2014-01-29%25202014-01-29
== EDIT ==
I'm not familiar with Android Get annotation, but try this.
#Get("/ServiceTest?version=1&dateStart={dateStart}&dateEnd={dateEnd}")
ResponseModel getDateRangeTest(String dateStart, String dateEnd);
and access with /ServiceTest?version=1&dataStart=2014-01-29&dateEnd=2014-01-29
Note that I change argument for more specific. and it would be a better approach.

Categories

Resources