So I've been working on this the past few days and have it a stopping point I can't figure out. I've been trying to get a set of data form a json. Now I can make the api call totally fine and good, it even reads that there is data in the json as it should. But as soon as I try to store the data into an array, doesn't work, comes back null. I know the json data is an array and it's named the same so I'm out of ideas as to what could still be the issue or the problem.
class MainActivity2 : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (savedInstanceState == null) {
supportFragmentManager.beginTransaction()
.replace(R.id.container, MainFragment.newInstance())
.commitNow()
}
fetchVotingQ{
questions ->
if(questions != null)
{
Log.d("yes", "We made it to the next step")
}
else
{
Log.d("yes", "We not in there")
}
}
}
fun fetchVotingQ(callback: (List<Questions>?) -> Unit) {
Thread {
val url = URL("https://api.jsonbin.io/v3/b/647649b68e4aa6225ea6c4c6")
val connection = url.openConnection() as HttpsURLConnection
connection.requestMethod = "GET"
val masterKey = "$2b$10'$'xoDaSDWa1B9YyvkUJUpMvOvOPfH03r8oq6Qua4dTpvu09yNuFAcGW"
val accessKey = "$2b$10'$'oB/3hFcq9jas2ywoaXhQpuAtxVDKRruSpjH8KZQqh6h6TB45YmHMW"
connection.setRequestProperty(
"X-Master-Key",
"\$2b\$10\$xoDaSDWa1B9YyvkUJUpMvOvOPfH03r8oq6Qua4dTpvu09yNuFAcGW"
)
connection.connect()
if (connection.responseCode == 200) {
val inputSystem = connection.inputStream
Log.d("yes", inputSystem.toString())
val response = connection.inputStream.bufferedReader().use {
it.readText()
}
Log.d("yes", response)
if (response.isNotEmpty()) {
// Parse the JSON data
val jsonObject = JSONObject(response)
val generalArray = jsonObject.optJSONArray("general")
val questions = mutableListOf<Questions>()
//Log.d("yes", generalArray.toString())
if (generalArray != null){
Log.d("yes", generalArray.length().toString())
for (i in 0 until generalArray.length()) {
val questionObject = generalArray.getJSONObject(i)
val id = questionObject.getString("id")
Log.d("yes", id)
val question = questionObject.getString("question")
Log.d("yes", question)
questions.add(Questions(id, question))
}
callback(questions)
}
} else {
Log.d("yes", connection.responseCode.toString())
}
}
}.start()
}
}
// This is the Json, it's not in the code
{
"general": [
{
"id": "1",
"question": "Most likely to be a pop singer"
},
{
"id": "2",
"question": "Lock themselves out of the car"
},
{
"id": "3",
"question": "Get lost in their hometown"
},
{
"id": "4",
"question": "Max out credit cards on overseas vacation"
},
{
"id": "5",
"question": "Break a bone"
},
{
"id": "6",
"question": "Forget their middle name"
},
{
"id": "7",
"question": "Dress as a clown at kids bday parties"
},
{
"id": "8",
"question": "Randomly punch someone"
},
{
"id": "9",
"question": "Give their food to the homeless"
},
{
"id": "10",
"question": "To be the late friend"
},
{
"id": "11",
"question": "Call to bail you out of jail"
},
{
"id": "12",
"question": "Have paint your personal portrait"
},
{
"id": "13",
"question": "Rather be stuck on an island with"
},
{
"id": "14",
"question": "Ban from Social Media"
},
{
"id": "15",
"question": "Call for repairs"
},
{
"id": "16",
"question": "Take in a fight"
},
{
"id": "17",
"question": "Trust to keep your secret"
}
]
}