I'm write code for JSON parsing, but JSON data is not retrieved. What would be the issue?
Below is the Data object class
data class UsersItem(
val avatar_url: String,
val events_url: String,
val followers_url: String,
val following_url: String,
val gists_url: String,
val gravatar_id: String,
val html_url: String,
val id: Int,
val login: String,
val node_id: String,
val organizations_url: String,
val received_events_url: String,
val repos_url: String,
val site_admin: Boolean,
val starred_url: String,
val subscriptions_url: String,
val type: String,
val url: String
)
Below is the List defined
class Users : ArrayList<UsersItem>()
Below is the ApiUtilities
object ApiUtilities {
private val BaseURl = "https://api.github.com/"
fun getInstance(): Retrofit {
return Retrofit.Builder().baseUrl(BaseURl)
.addConverterFactory(GsonConverterFactory.create()).build()
}
}
Below is the ApiInterface
interface ApiInterface {
@GET("/Users")
suspend fun getUsers(): Response<Users>
}
Below is the MainActivity, I just tested to get the JSON Response with login id, but i'm not getting the result. Please correct the code or give any suggestions
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val helloTxt = findViewById<TextView>(R.id.txt) as TextView
val userApi = ApiUtilities.getInstance().create(ApiInterface::class.java)
GlobalScope.launch {
val result = userApi.getUsers()
if (result.isSuccessful && result.body() != null) {
result.body()?.forEach {
Toast.makeText(this@MainActivity, "${it.id}", Toast.LENGTH_LONG).show()
Log.d("Manju", "${it.login}")
helloTxt.setText("${it.login}")
}
}
}
}
}
if (result.isSuccessful && result.body() != null) {, where's your check for if the response isn't successful ?where's your check for if the response isn't successful ?