Patch request using Retrofit2 not working Android - 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}")

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.

Sending Array as Param in Retrofit Get Request A

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);

Call api with Retrofit

I want to consume athe api located at http://base-url/delivery-orders?driverId.equals=1116
This is my function:
#GET("delivery-orders")
fun getAllDeliveryOrder(
#Header("Authorization") token: String,
#Query("driverId") idDriver: Int
): Observable<OrderItems>
but I got error code 500.
Error codes from 500-599 are server errors. Meaning, your code is working but the server doesn't. Better recheck the code from the server to fix this error.
This problem usually from server..
try to check Method or maybe there has invinity looping.
My friend also get the same problem, he say you must check database. Maybe your web can't call some data in database. just try delete all or make empty database, and then run again ur Android
I found the problem about that request. the problem is the URL have driverId.equal and my network interface is
#GET("delivery-orders")
fun getAllDeliveryOrder(#Header("Authorization") token: String, #Query("driverId") idDriver: Int): Observable<OrderItems>
so retrofit think driverId on URL is driverId in #Query, so I just change "driverId" in #Query with IdDriver. that solve the problem.

what is the format for below Api call in retrofit?

postman image with Api call format is attached below,
response is success when called through postman.
but Api response is failure when try through Code.
#GET("booking-list/{userId}")
fun getOrderHistoryList(#Path("userId") userId: Int):Observable<ResponseClass>
please help me to correcting format.
You need to change #Path to #Query like below, also usedId is a query parameter so you need to remove from path.
#GET("/booking-list")
fun getOrderHistoryList(#Query("userId") userId: Int):Observable<ResponseClass>
The query component is indicated by the first question mark ("?") character. For more information look at it Query component
Your get url should be
#GET("booking-list?userId={userId}")

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