0

Below image is the response of whatsap api which run fine on postman. enter image description here

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"] = "+923364451111@c.us"
        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.

F_Z
  • 581
  • 3
  • 16
  • 3
    it would be helpful if you could share the `Whatsapp` class for more clarification – Hossein Farrokhi Apr 23 '21 at 19:14
  • @HosseinFarrokhi just edited my question. – F_Z Apr 24 '21 at 05:11
  • it seems to be alright. anyways, add `loggingInterceptor` to your app to see the request details in `logcat`. it can help you debug the network requests easily. [link](https://github.com/square/okhttp/tree/master/okhttp-logging-interceptor) – Hossein Farrokhi Apr 24 '21 at 09:23

1 Answers1

1

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:

@POST("/sendMessage")

Even if your base url in the retrofit instance is https://api.chat-api.com/instance259349, it'll translate to https://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 a 404 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.

Fred
  • 16,367
  • 6
  • 50
  • 65
  • I will thankful to you if you tell me how to exactly write endpoint, token, and base url. – F_Z Apr 27 '21 at 11:22
  • I used instance as you asked but still getting null body, I think I am using token incorrectly. – F_Z Apr 27 '21 at 11:31
  • @ZeeshanFareed the token seems to be fine according to what you did and the picture from postman. Did you try what I mentioned about the base url and the path inside `@POST`? – Fred Apr 27 '21 at 15:16
  • Yes I have tried what you asked but still getting null – F_Z Apr 27 '21 at 15:17
  • response.errorBody(): okhttp3.ResponseBody$1@da58b98 – F_Z Apr 27 '21 at 15:33
  • yeah, you'll need to read it as well. Probably something like `response.errorBody().string()` – Fred Apr 27 '21 at 17:04