0

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}")
                }

            }
        }

    }
}
8
  • have you tried using breakpoints or logs to see what's happening or what isn't happening ? Commented Feb 16, 2022 at 12:06
  • yes I tried breakpoints and logcat also, response is not getting Commented Feb 16, 2022 at 12:10
  • you only have if (result.isSuccessful && result.body() != null) { , where's your check for if the response isn't successful ? Commented Feb 16, 2022 at 12:11
  • I called instance of Retrofit and connected to server val userApi = ApiUtilities.getInstance().create(ApiInterface::class.java) Commented Feb 16, 2022 at 12:14
  • that doesn't answer my question : where's your check for if the response isn't successful ? Commented Feb 16, 2022 at 12:25

1 Answer 1

1

I think that you schould use "/users" with small "u":

interface ApiInterface {
@GET("/users")
suspend fun getUsers(): Response<Users>
}

insted of:

interface ApiInterface {
@GET("/Users")
suspend fun getUsers(): Response<Users>
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.