Below image is the response of whatsap api which run fine on postman.
But when I try to run it on android project by using retrofit it is giving null response.
Here is my api parameter in Andorid:
fun getData(mainActivity: MainActivity): Boolean {
val params = HashMap<String, Any>()
params["phone"] = "[email protected]"
params["body"] = "Zeeshan Hello, world! "
retrofitClient.abc("01jideaw5zab024y", params).enqueue(object : Callback<Whatsapp> {
override fun onResponse(call: Call<Whatsapp>, response: Response<Whatsapp>) {
Log.d("SoS", "response: " + response.body())
Log.d("SoS", "response: " + response.body()?.sent)
}
override fun onFailure(call: Call<Whatsapp>, t: Throwable) {
Log.d("SoS", "error: " + t.message)
}
})
}
And its endpoint I am using like this:
@POST("/sendMessage")
fun abc(@Query("token") token: String, @Body body: HashMap<String, Any>): Call<Whatsapp>
Here is my Whatsapp Class:
class Whatsapp{
@SerializedName("sent")
@Expose
var sent: Boolean? = null
@SerializedName("message")
@Expose
var message: String? = null
@SerializedName("id")
@Expose
var id: String? = null
@SerializedName("queueNumber")
@Expose
var queueNumber: Int? = null
}
I think I am not using parameters correctly. Anyone help me how to use parameters here.
I don't think this is small enough to fit in a comment, so I'll add as an answer. I'm guessing some of the things here.
The simplest guess I have is related to your path in the URL. As you shown in the image, it should be
instance259349/sendMessage
, yet in your interface you have:Even if your base url in the retrofit instance is
https://api.chat-api.com/instance259349
, it'll translate tohttps://api.chat-api.com/sendMessage
(without the instance part) because of how retrofit treats leading/
. A quick way to check this is to remove the leading/
and just use@POST("sendMessage")
or, in case your base url is doesn't include the instance part, use@POST("instance259349/sendMessage")
.The other problem that might be happen is that Postman under the hood adds headers that your app does not. These headers might be required by the server and hence it returns an error.
I'd recommend to look into
response.errorBody()
as well as the status of the response to understand what error you're getting. Note that if my first guess is correct, you might be getting a404 NOT FOUND
because that path doesn't exist.If you don't want to check it manually, a good way to do it a bit more easily is with the logging interceptor.