//AndroidManifest.xml
<manifest
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:usesCleartextTraffic="true">
//생략
</application>
/manifest>
private lateinit var queue: RequestQueue
: 처리할 request 객체 저장
queue = Volley.newRequestQueue(application)
: application 대신 this 사용 가능
binding.btnConnect.setOnClickListener {
val url = "...생략..."
val request = JsonObjectRequest(Request.Method.GET, url, null,
{
//처리가 잘 됐을 때 call-back 함수
Toast.makeText(application, it.toString(), Toast.LENGTH_LONG).show()
},
{
//에러가 났을 때 호출되는 함수
Toast.makeText(application, it.toString(), Toast.LENGTH_LONG).show()
}
)
queue.add(request)
}
- JSONObject: {객체}
- JSONArray: [배열]
//MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var queue: RequestQueue
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
queue = Volley.newRequestQueue(application)
binding.btnConnect.setOnClickListener {
val url = "https://yts.torrentbay.to/api/v2/list_movies.json?sort=rating&limit=30"
val request = JsonObjectRequest(
Request.Method.GET,
url,
null,
{
//처리가 잘 됐을 때 call-back 함수
Toast.makeText(application, it.toString(), Toast.LENGTH_LONG).show()
},
{
//에러가 났을 때 호출되는 함수
Toast.makeText(application, it.toString(), Toast.LENGTH_LONG).show()
}
)
queue.add(request)
}
}
}