Async와 HttpUrlConnection으로 데이터 받아서 recyclerview에 뿌리기 실습
class NetWork1Activity : AppCompatActivity() {
private lateinit var binding: ActivityNetWork1Binding
val url = URL("https://jsonplaceholder.typicode.com/posts")
val connection: HttpURLConnection = url.openConnection() as HttpURLConnection
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityNetWork1Binding.inflate(layoutInflater)
setContentView(binding.root)
BackgroundAsyncTask2(
connection,
LayoutInflater.from(this@NetWork1Activity),
binding,
this@NetWork1Activity
).execute()
}
}
class BackgroundAsyncTask2(
var connection: HttpURLConnection,
val inflater: LayoutInflater,
val binding: ActivityNetWork1Binding,
val context: Context
) : AsyncTask<Any?, Any?, Array<Person>>() {
var data: Array<Person>? = null
override fun onPreExecute() {
}
override fun doInBackground(vararg params: Any?): Array<Person> {
connection.requestMethod = "GET"
connection.setRequestProperty("Content_Type", "application/json")
var buffer = ""
if (connection.responseCode == HttpURLConnection.HTTP_OK) {
Log.d("connn", "inputstream" + connection.inputStream)
val reader = BufferedReader(
InputStreamReader(
connection.inputStream,
"UTF-8"
)
)
buffer = reader.readText()
Log.d("connn", "buffer" + buffer)
data = Gson().fromJson(buffer, Array<Person>::class.java)
Log.d("connn", "data" + data!![0].title)
}
return data!!
}
override fun onProgressUpdate(vararg values: Any?) {
}
override fun onPostExecute(result: Array<Person>) {
val adatper = NetworkAdapter(data!!, inflater)
binding.recyclerNet.adapter = adatper
binding.recyclerNet.layoutManager = LinearLayoutManager(context)
}
override fun onCancelled() {
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NetWork1Activity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_net"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>```
```kotlin
class NetworkAdapter(val itemList: Array<Person>, val inflater: LayoutInflater) :
RecyclerView.Adapter<NetworkAdapter.ViewHolder>() {
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var userId: TextView? = null
var id: TextView? = null
var title: TextView? = null
var body: TextView? = null
init {
userId = itemView.findViewById<TextView>(R.id.userId)
id = itemView.findViewById<TextView>(R.id.id)
title = itemView.findViewById<TextView>(R.id.title)
body = itemView.findViewById<TextView>(R.id.body)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val viewHolder = inflater.inflate(R.layout.network_list, parent, false)
return ViewHolder(viewHolder)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.userId!!.text = itemList[position].userId.toString()
holder.title!!.text = itemList[position].title.toString()
holder.body!!.text = itemList[position].body.toString()
}
override fun getItemCount(): Int {
return itemList.size
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#DD6767"
android:orientation="vertical">
<TextView
android:id="@+id/userId"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/id"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/body"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>