uscode

Untitled

Nov 5th, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.65 KB | None | 0 0
  1. package com.resmana.githubuser
  2.  
  3. import android.os.Bundle
  4. import android.util.Log
  5. import androidx.fragment.app.Fragment
  6. import android.view.LayoutInflater
  7. import android.view.View
  8. import android.view.ViewGroup
  9. import android.widget.LinearLayout
  10. import android.widget.ListView
  11. import android.widget.Toast
  12. import androidx.recyclerview.widget.LinearLayoutManager
  13. import com.bumptech.glide.Glide
  14. import com.bumptech.glide.request.RequestOptions
  15. import com.google.android.material.tabs.TabLayout
  16. import com.loopj.android.http.AsyncHttpClient
  17. import com.loopj.android.http.AsyncHttpResponseHandler
  18. import cz.msebera.android.httpclient.Header
  19. import kotlinx.android.synthetic.main.fragment_followers.*
  20. import kotlinx.android.synthetic.main.item_user.*
  21. import org.json.JSONArray
  22. import org.json.JSONObject
  23.  
  24. class FollowersFragment : Fragment() {
  25.  
  26.  
  27.  
  28.     companion object {
  29.         private val TAG = FollowingFragment::class.java.simpleName
  30.         private val ARG_USERNAME = "username"
  31.  
  32.  
  33.         fun newInstance(username: String?): FollowersFragment {
  34.             val fragment = FollowersFragment()
  35.             val bundle = Bundle()
  36.             bundle.putString(ARG_USERNAME, username)
  37.             fragment.arguments = bundle
  38.             return fragment
  39.         }
  40.     }
  41.     private val listView = ArrayList<Users>()
  42.     private lateinit var adapter: FollowersAdapter
  43.  
  44.  
  45.     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?): View? {
  46.  
  47.         return inflater.inflate(R.layout.fragment_followers, container, false)
  48.     }
  49.  
  50.     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  51.         super.onViewCreated(view, savedInstanceState)
  52.  
  53.         adapter = FollowersAdapter()
  54.         adapter.notifyDataSetChanged()
  55.  
  56.         listFolowers.setHasFixedSize(true)
  57.         listFolowers.layoutManager = LinearLayoutManager(this.context)
  58.         listFolowers.adapter = FollowersAdapter()
  59.         listFolowers.adapter = adapter
  60.  
  61.         val follower = arguments?.getString(ARG_USERNAME)
  62.         val url = "https://api.github.com/users/${follower}/followers"
  63.             val client = AsyncHttpClient()
  64.             client.addHeader("Authorization","token 05ccef4bfd9014178c9e3bca405d45863b0ff916")
  65.             client.addHeader("User-Agent", "request")
  66.             client.get(url, object : AsyncHttpResponseHandler(){
  67.                 override fun onSuccess(statusCode: Int, headers: Array<Header>, responseBody: ByteArray) {
  68.                     val UsersFollowers = ArrayList<Users>()
  69.                     val result = String(responseBody)
  70.                     Log.d(TAG, result)
  71.                     try {
  72.                         val jsonArray = JSONArray(result)
  73.                         for (i in 0 until jsonArray.length()) {
  74.                             val jsonObject = jsonArray.getJSONObject(i)
  75.                             val imgFollowers = jsonObject.getString("avatar_url")
  76.                             val nameFollowers = jsonObject.getString("login")
  77.                             val user = Users()
  78.                             user.avatar = imgFollowers
  79.                             user.username = nameFollowers
  80.                             UsersFollowers.add(user)
  81.                         }
  82.                     } catch (e: Exception) {
  83.                         Log.d(TAG, e.message.toString())
  84.                         e.printStackTrace()
  85.                     }
  86.                 }
  87.                 override fun onFailure(statusCode: Int, headers: Array<Header>, responseBody: ByteArray, error: Throwable) {
  88.                     Log.d(TAG, error.message.toString())
  89.                 }
  90.             })
  91.     }
  92. }
Add Comment
Please, Sign In to add comment