This question already has answers here:
Send Post Request with params using Retrofit
(6 answers)
Closed 4 years ago.
I need to send an array with Post Request using Retrofit. In Postman worked correctly like this
But in Android, I can not send the correct request.Please, help me.
UPDATE
I try like this:
#POST("/api/friends")
fun getContactsList(#Header("Authorization") token String,#Query("phones[]") phones : Array<String>) : Single<List<Friends>>
I think you can try this:
#POST("http://server/service")
Call<YourModel> postSomething(#Query("phones") List<String> array);
Generated url should look like this:
http://server/service?phones=123&phones=345&phones=567&phones=789
Related
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
I am new to kotlin / android development, I have been following the google / android docs with using Volley to send requests....Still have not go to handling JSON yet.
When I put the example GET in a function in kotlin using android development studio, I am getting the error message from ErrorListener
val textView = findViewById<TextView>(R.id.textReturn)
val url = "http://wwww.google.com"
val queue = Volley.newRequestQueue(this)
val stringRequest =
StringRequest(Request.Method.GET, url, Response.Listener<String> { response ->
var strRes = response.toString()
textView.text = strRes
}, Response.ErrorListener {textView.text = "That didn't work!"})
queue.add(stringRequest)
}
I am unsure on how to debug and come from a very javascript / php background where console.log() or print() will post to the console, kotlin does not seem to have such a simple function for this.
Not exactly certain about Volley, but eg. with Retrofit, the error listener will only be triggered on hard errors - but common HTTP status codes, besides 200 OK, end up in the response listener. Retrofit with GsonConverter and ORM is the most common combo for converting JSON to POJO (one can define a whole API as a web-service and it converts the JSON automatically). However, when still learning Java/Kotlin, it might not hurt to use Volley and not use ORM auto-mapping (because all the nuts & bolts are being abstracted away). PHP background is useful for whatever API interaction.
And concerning the actual question, in Kotlin one can log with: println and Android Studio can be used for the actual debugging (it provides similar functionality alike F12 tools and xdebug provide). Using logging to debug is only partially applicable, eg. when wanting to let the console tell the story of what happened - otherwise, just use break-points, watches or evaluate expressions.
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);
This question already has answers here:
Kotlin Data Class from Json using GSON
(3 answers)
Closed 4 years ago.
JSON decoding was improved in Swift4
you simply just call JSONDecoder().decode
and give it the json object and it will be converted into object
here is an example of that way
https://roadfiresoftware.com/2018/02/how-to-parse-json-with-swift-4/
my question that is there a way in Kotlin similar to the new way in Swift4 ?
Yes, Android has had that for quite a few years now.
It's a library from Google themselves, called GSON:
https://github.com/google/gson
Example:
Gson gson = new GsonBuilder().create();
gson.fromJson("MY JSON STRING", MyClass.class);
There are many overloads for the 'fromJson' function. There is also a 'toJson' function, to turn an object into a JSON string.
This is not just for Kotlin, it also works in Java.
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");
}