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
Related
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.
I have an issue with load XML feed from url using Retrofit.
Given a dynamic url that clearly returns xml feed f.e: https://anchor.fm/s/53faae8/podcast/rss
When I try to load this via Retrofit I can see in Android Studio Network Profilier that it returns a scrambled response. Something like this:
ì½ÝvÜF’.z=ó5mÏ&d&€ÛmÊ’Ûš¶-K=š>{íµWH°`Ve EšsÕ÷çö¬5ûö
<X?ɉˆLüT‰jIUîÖL·šUäoDFdü|ñÙ?¯–Þ•iÚ²®~÷$8õŸx¦Ê꼬.~÷ä�o¾bêÉŸÖ´VíYžýîÉ¢ëÖgOŸ®7Íò´n.žæÙS³4+SuíÓà4xúÄ=›ÕU_¾û4úOWu¾Yšö©{pxQwõjxëúúúôZÐ{Ü÷ççðã“qȇl_+»MeÚ
Etc.
I've added an xml converter in my retrofitBuilder:
retrofitBuilder.addConverterFactory(SimpleXmlConverterFactory.createNonStrict(Persister(AnnotationStrategy())))
And my service code to retrieve the xml from url looks like:
interface XmlFeedService {
#GET
fun getXmlFeedFromUrl(#Url url: String): Call<RssFeed>
I am using #Url annotation here since the url is dynamic and depending what another api returns to me can result in urls with different base url's.
Of course the response body can't be parsed like this, but I am unclear why it comes back like this.
Can anyone help out?
Figured it out already.
It's not due to a wrong configuration of the Retrofit builder or the service, but the scrambled response that the Network Profiler showed me was due to the some of the Annotation in the Data models being wrongly configured...
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.
The Premise
I'm working on a simple app where I want to list out a user's GitHub repos in a RecyclerView. I'm using this as my endpoint while building this.
The Problem
The problem I'm facing is that the GitHub API returns only 30 repos in one go. To get more, I can append a per_page=100 (100 is the maximum) to my query string; But, what do we do about users with more than 100 repos?
The solution the API docs provide is to get the next; url from the "Link" response header to make a second API call.
How does one go about this? Thanks!
The Response class will let you access the headers. The GitHub client would have a method like:
#GET("/search/code")
Observable<Response<T>> getUser(...)
Inside onNext(), you'd do something like:
#Override
public void onNext(Response<T> response) {
String next = response.headers().get("next");
}
From my experimenting so far, it appears that Volley accesses its cache based on the URL alone. I would like to know if it also can determine which response it should return based on the combination of URL and request body data.
Is there a setting for this, or and extension point where I can implement this myself.
I don't see anywhere that the request body data is returned in the NetworkResponse object, or I would do the check in the parseNetworkResponse method.
Thanks
You have to override Request.getCacheKey() method to include request body as well, when implementing your custom request.