Reputation: 149
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?
Upvotes: 0
Views: 46
Reputation: 149
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.
Upvotes: 0