-2

I use this JSON https://api.github.com/users

I need to get string name, followers, following, and more. But on the program says "No value for name". I think I need to go to a specific user example: https://api.github.com/users/mojombo to getting that info, but I don't know-how.

And I using loopj library.

Here's My Code

private fun getDataGitDetail() {
        progressBar.visibility = View.VISIBLE
        val client = AsyncHttpClient()
        client.addHeader("Authorization", "token 6fe9dff2e5e43d25eb3abe9ff508a750b972f725")
        client.addHeader("User-Agent", "request")
        val url = "https://api.github.com/users"
        client.get(url, object : AsyncHttpResponseHandler() {
            override fun onSuccess(
                statusCode: Int,
                headers: Array<Header>,
                responseBody: ByteArray
            ) {
                progressBar.visibility = View.INVISIBLE
                val result = String(responseBody)
                Log.d(TAG, result)
                try {
                    val jsonArray = JSONArray(result)
                    for (i in 0 until jsonArray.length()) {
                        val jsonObject = jsonArray.getJSONObject(i)
                        val username: String? = jsonObject.getString("login")
                        val name: String? = jsonObject.getString("name")
                        val avatar: String? = jsonObject.getString("avatar_url")
                        val company: String? = jsonObject.getString("url")
                        val location: String? = jsonObject.getString("url")
                        val repository: Int = 0
                        val followers: Int = 0
                        val following: Int = 0
                        listData.add(
                            Data(
                                username,
                                name,
                                avatar,
                                company,
                                location,
                                repository,
                                followers,
                                following
                            )
                        )
                    }
                    showRecyclerList()
                } catch (e: Exception) {
                    Toast.makeText(this@MainActivity, e.message, Toast.LENGTH_SHORT)
                        .show()
                    e.printStackTrace()
                }
            }

            override fun onFailure(
                statusCode: Int,
                headers: Array<Header>,
                responseBody: ByteArray,
                error: Throwable
            ) {
                progressBar.visibility = View.INVISIBLE
                val errorMessage = when (statusCode) {
                    401 -> "$statusCode : Bad Request"
                    403 -> "$statusCode : Forbidden"
                    404 -> "$statusCode : Not Found"
                    else -> "$statusCode : ${error.message}"
                }
                Toast.makeText(this@MainActivity, errorMessage, Toast.LENGTH_LONG)
                    .show()
            }
        })
    }
Kasım Özdemir
  • 5,414
  • 3
  • 18
  • 35

2 Answers2

0

The current response you are getting does not contain a key name in the JSONObject. If you want the Name of all the users you will have to go to each users endpoint in the api. You'll need to make another request inside your for loop that gets datafrom an endpoint like https://api.github.com/users/mojombo

val jsonArray = JSONArray(result)
           for (i in 0 until jsonArray.length()) {
              val jsonObject = jsonArray.getJSONObject(i)
              val username: String? = jsonObject.getString("login")
              //Make the request here using "https://api.github.com/users/" + login

You can then choose to get the rest of the data from either the first response or the 2nd one as both contain that information.

I hope this helps.

Aditya Kurkure
  • 422
  • 5
  • 19
0

No need for a JSON array, cz API https://api.github.com/users/mojombo is JSON Object.

Example:

client.get(url, object : AsyncHttpResponseHandler() {
    override fun onSuccess(statusCode: Int, headers: Array<Header>, responseBody: ByteArray) {
        try {
            //parsing json
            val result = String(responseBody)
            val responseObject = JSONObject(result)
            textView2.text = responseObject.getString("login")
            textView3.text = responseObject.getString("name")
            textView9.text = responseObject.getString("location")
            desc.text = responseObject.getString("company")
            view?.let { Glide.with(it).load(responseObject.getString("avatar_url")).into(imageView2) }
        } catch (e: Exception) {
            Log.d("Exception", e.message.toString())
        }
    }
}
cela
  • 2,352
  • 3
  • 21
  • 43