cturner0098
cturner0098

Reputation: 17

Sending JSON POST using Retrofit2 and Kotlin

I am trying to use Strawpoll's API to create a poll using Retrofit2 in Kotlin. I am having trouble getting the correct response from the server, even though the POST seems to be going fine. Here's what I'm working with so far.

IStrawpollDAO.kt

@Headers("Content-Type: application/json", "Accept:application/json")
@POST("polls")
fun createStrawpoll(@Body polls: Poll): Call<StrawpollPost>

Poll.kt

data class Poll(
    @SerializedName("title") var title: String,
    @SerializedName("options") var options: ArrayList<String>,
    @SerializedName("multi") var multi: Boolean = true
)

StrawpollService.kt

fun createStrawpoll(): MutableLiveData<StrawpollPost>? {
    var strawpoll = MutableLiveData<StrawpollPost>()
    val service = StrawpollInstance.retrofitInstance?.create(IStrawpollDAO::class.java)

    var options = ArrayList<String>()
    options.add("Option 1")
    options.add("Option 2")

    var poll = Poll("31415926535897", options)
    
    val call = service?.createStrawpoll(poll)
    
    call?.enqueue(object : Callback<StrawpollPost> {
        override fun onFailure(bigcall: Call<StrawpollPost>, t: Throwable) {
            println("fail")
            println(t.message)
            println(bigcall.request().body)
        }

        override fun onResponse(call: Call<StrawpollPost>?, response: Response<StrawpollPost>?) {
            if(response?.code() == 200) {
                println("pass")
                strawpoll.value = response?.body()
            }
        }
    })

    return strawpoll
}

StrawpollInstance.kt

object StrawpollInstance {
    private var retrofit: Retrofit? = null
    private val BASE_URL = "https://strawpoll.me/api/v2/"

    val retrofitInstance: Retrofit?
        get() {
            if (retrofit == null) {
                var httpClient = OkHttpClient.Builder()
                val interceptor = HttpLoggingInterceptor()

                interceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
                httpClient.interceptors().add(interceptor)

                val gson = GsonBuilder()
                    .setLenient()
                    .create()

                retrofit = Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .client(httpClient.build())
                    .build()
            }

            return retrofit
        }
}

And here is some data from the Logcat: Logcat1

Logcat 2

It seems the response is currently just returning their HTML error page, instead of an expected JSON object back.

Upvotes: 0

Views: 954

Answers (1)

MeowMeowMeow
MeowMeowMeow

Reputation: 49

I made a POST and got the response:

<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="https://www.strawpoll.me/api/v2/polls">here</a>.</h2>
</body></html>

So, change your endpoint to https://www.strawpoll.me/api/v2/polls .

Upvotes: 1

Related Questions