I'm trying to send JSON via a POST request to a rest API with Retrofit. The problem I am having is that I cannot seem to figure out/use the type of data(JSONArray, String, INT ...etc)
that retrofit wants in order for it to POST to my rest API. As of now, I have only tried to hard code the JSON in a string and parse it to Retrofit in hopes of it POSTing to the rest API and messing around with what type I input I use to give to retrofit to POST
when I try and parse a String I get the following: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
this error says that I am not parsing JSON. the problem is that I have no idea what to parse.
when I tried to parse either a JSONarray or a JSONobject or any other type of data my apps view would not load and my app would crash.
my code
interface SimpleApi {
@POST("posttest.php")
suspend fun postit(@Body post: String): Phone
}
this is the retrofit API that I. the String value is the one that I believe is giving me trouble, the Phone class is data class I am using for my recylerview.
private fun TestGet() {
val gson = GsonBuilder()
.setLenient()
.create()
val api = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
.create(SimpleApi::class.java)
GlobalScope.launch(Dispatchers.IO) {
val response = api.postit(test)//it wants a JSONarray
//the above wants some kind of JSON to work I was passing Strings instead
try {
//here lies the app logic and recylerview setup
}catch (e: Exception){
println("you messed up the connection some how")
}
}
}
this function is how I am calling my the rest API, parsing and returning the JSON and handling the business logic.
now the variable test
has been a few things originally it was just a hardcoded string exactly like this private var test = "[\"text1\", \"text2\", \"text3\"]"
and private var test = """["text1", "text2", "text3"]"""
, this did not work, I have also tried to implement A GSON converter as stated above this did not work.
thank you for your time.
EDIT
with out the addConverterFactory(GsonConverterFactory.create(gson))
I get the following errors FATAL EXCEPTION: DefaultDispatcher-worker-1
and com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $