Call api with Retrofit - android

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.

Related

Retrofit2 GET request with Id

I am trying to make a request in my android project. The url is this one
"https://api.spoonacular.com/recipes/716429/information?includeNutrition=false"
I am using retrofit 2 but i can't figure out how to make it work.
Here is what i tried to do.
I get the id of a recipe and call this function passing the id.
fun applyQueryById(recipeId: Int): String{
val searchByIdQuery = "${recipeId}/information?includeNutrition=false&apiKey=${API_KEY}"
return searchByIdQuery
}
And the GET request is this one
#GET("/recipes/")
suspend fun getRecipeById(
#Query("id") searchById:String
):Response<PersonalizedRecipeResult>
I thinks that because the id is in the middle, making a raw string like i am doing is not a good idea. if anyone could suggest something different I'll appreciate
You are using #Query("id") which will ad the value as query.
From your example, I can see that you want to use #Path
You can use it like this
#GET("/recipes/{id}")
suspend fun getRecipeById(
#Path("id") searchById:String
):Response<PersonalizedRecipeResult>
by this way the searchById will be replaced with {id} in your example call

retrofit return HTTP 422 in the github user search url

I ran this URL in the browser and it returns the correct values:
enter link description here
but when I run this URL in the retrofit I get HTTP 422 error
how can I fix it?
#Headers("Authorization: token MY_TOKEN")
#GET("search/users")
suspend fun searchUser(
#Query("q") user: String? = "",
#Query("page") page: Int = 1
): SearchUserEntity
I think this problem is happened when I pass "" to query. But it working I the browser. How can I fix the ""
#GET("search/users")
suspend fun searchProfilesSuspend(#Header("Authorization") token: String, #Query("q") query: String): ProfileSearchResults
Everything seems to be correct, here is a snippet from my program, I myself recently studied coroutines and rewrote my project using them.
Have you looked at the additional information that the server returns along with the error code?
Maybe the number of requests has expired, there is a limit like 60 per hour if you are unauthorized.
Here GithubSearcher you may see how it was implemented and several other requests.

What am I missing in my Retrofit Restful call for the filepath to upload properly?

I have an app that takes a photo, uploads it to Google Drive and then posts some information to my database via an API call made with retrofit in Android Studio.
The photo works nicely, I can upload this to Google Drive without any problem and I can see the file ID it has when uploaded. In my logcat I am writing the sharing URL and this also works.
However, I am tearing my hair out on what should be the simplest part, the upload to SQL.
The call to the API works fine if I call it directly from postman, and if I call the proc from SQL Server, it also works fine, so the problem is the call as it's coming from my Kotlin app.
For some reason, the image url is not being passed to the call, and I can't work out why.
The interface is defined as follows:
private var BaseURL = "https://api.domain.com/api/"
interface ReadInterface {
#POST("read?key=xxx&ipadress=1.1.1.1")
fun AddRead(
#Query("operation") operation: String,
#Query("dt") dt: String,
#Query("reading") reading: Int,
#Query("imageurl") imageurl: String
): Call<UploadRead>
companion object {
fun create():ReadInterface {
val retrofit = Retrofit.Builder()
.addConvertorFactory(GsonConvertorFactory.create())
.baseurl(BaseURL)
.build()
return retrofit.create(ReadInterface::class.java)
}
}
}
My call to the api is:
class MainActivity : AppComatActivity() {
var rURL: String = ""
...
uploadImagetoDrive(bitmapToFile(Photo1))
Log.d("URL:", rURL)
ReadInterface.create().AddRead("new", "2021-06-22", 1234, rURL).enqueue(object: Callback<UploadRead>
}
The logcat entry for the URL is being written happily with the URL that is set by uploadImagetoDrive (D/URL:: https://drive.google.com/file/d/klfhdfhkd...) and teh very next line of code should be passing that variable to AddRead. I have onFailure and onResponse override functions that both write to logcat and I am getting the correct response. However, the imageurl is not getting uploaded to the database (the column is nullable). The row is added, so I can see the call is being made with the supplied date and reading. If I change the last parameter in AddRead to pass a fixed string of "www.google.com", for example, it works.
I've tried setting a new variable and writing that to the logcat before and after and both are written nicely. I just can't work out why the call fails when I pass it as a variable.
Can anyone shed any light on it for me please?
I'm not sure why it doesn't work if the value is outside the call that uploads the image to google drive, but after reviewing a suggestion for something else, I decided to try adding the code that calls the API into the same function that uploads the images. This works, so it seems that the call to upload the image is finishing before upload aspect has completed. To me this seems counter-intuitive, but I found a similar problem with another aspect of Kotlin, so I can only assume that the same thing is happening here.

Android Retrofit Change Dynamic BaseUrl by User

I want to change baseurl with spinner by letting the user choose.
Because the api I use is also the baseurls are allocated to different servers.
Sample :
euw1.site.com
na1.site.com
tr1.site.com
I want to make this server selection to the user with the spinner.
App Sample
As I am new to programming, I could not succeed no matter how hard I tried. Thank you to everyone who has already helped.
Retrofit - Change BaseUrl , change request get, post .. request url
#GET suspend fun getAPI(#Url url: String?): Response<Any> // url is dynamic

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}")

Categories

Resources