My code is:MainActvity.kt-------------------package com.example.irttsearchappimport android.content.Intentimport androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport android.view.Viewimport android.widget.AdapterViewimport android.widget.ArrayAdapterimport android.widget.Spinnerimport android.widget.Toastimport kotlinx.android.synthetic.main.activity_main.*class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) var fossVsFossID = HashMap<String, Int>() fossVsFossID.put("Java",10) fossVsFossID.put("Cpp",57) fossVsFossID.put("Python",26) fossVsFossID.put("RDBMS",92) var languageVsLanguageID = HashMap<String,Int>() languageVsLanguageID.put("English",22) languageVsLanguageID.put("Hindi",6) languageVsLanguageID.put("Gujarati",5) languageVsLanguageID.put("Tamil",18) languageVsLanguageID.put("Marathi",12) languageVsLanguageID.put("Kannada",7) var fossVsLanguage = HashMap<String, ArrayList<String>>() var availablelanguagesforFoss: ArrayList<String> availablelanguagesforFoss = arrayListOf("English","Gujarati","Hindi","Kannada") fossVsLanguage.put("Java",availablelanguagesforFoss) availablelanguagesforFoss = arrayListOf("English","Gujarati","Hindi","Kannada") fossVsLanguage.put("Cpp",availablelanguagesforFoss) availablelanguagesforFoss = arrayListOf("English","Gujarati","Hindi","Kannada") fossVsLanguage.put("Python",availablelanguagesforFoss) availablelanguagesforFoss = arrayListOf("English","Gujarati","Hindi","Kannada") fossVsLanguage.put("RDBMS",availablelanguagesforFoss) var fossSpinner = findViewById<Spinner>(R.id.Spinner1) var languageSpinner = findViewById<Spinner>(R.id.Spinner2) var fossOptions = fossVsFossID.keys.toList() var languageOptions = languageVsLanguageID.keys.toMutableList() var selectedFoss = "none" var selectedLanguage = "none" var fossAdapter = ArrayAdapter<String>(applicationContext,android.R.layout.simple_list_item_activated_1,fossOptions) fossSpinner.adapter = fossAdapter var languageAdapter = ArrayAdapter<String>(applicationContext,android.R.layout.simple_list_item_activated_1,languageOptions) languageSpinner.adapter = languageAdapter fossSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { override fun onNothingSelected(parent: AdapterView<*>?) { } override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { languageOptions.clear() selectedFoss = fossOptions.get(position) languageOptions.addAll(ArrayList(fossVsLanguage.get(selectedFoss)!!)) languageAdapter.notifyDataSetChanged() } } languageSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { override fun onNothingSelected(parent: AdapterView<*>?) { } override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { selectedLanguage = languageOptions.get(position) } } searchButton.setOnClickListener { if (selectedFoss == "none" || selectedLanguage == "none") { Toast.makeText(this, "Please select FOSS and Language both", Toast.LENGTH_SHORT).show() } else { var fossID = fossVsFossID.get(selectedFoss) var languageID = languageVsLanguageID.get(selectedLanguage) var searchIntent = Intent(this, Main2Activity::class.java).apply { putExtra("fossID", "" + fossID) putExtra("languageID", "" + languageID) } startActivity(searchIntent) } } }}---------------------Main2Activity.kt----------------package com.example.irttsearchappimport androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport android.widget.Toastimport com.android.volley.Requestimport com.android.volley.Responseimport com.android.volley.toolbox.StringRequestimport com.android.volley.toolbox.Volleyimport kotlinx.android.synthetic.main.activity_main2.*import org.json.JSONArrayimport org.json.JSONObjectclass Main2Activity : AppCompatActivity() { var finalResultsArrayList= ArrayList<VideoItem>() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main2) var foss = intent.getStringExtra("fossID") var language = intent.getStringExtra("languageID") selectedOption.text = "Foss ID : " + foss + "\nLanguage ID : " + language val queue = Volley.newRequestQueue(this) val url = "https://spoke-tutorial.org/api/get_tutorials/"+foss+"/"+language val stringRequest = StringRequest(Request.Method.GET,url, Response.Listener<String> {response -> Toast.makeText(applicationContext,"Received server response!", Toast.LENGTH_SHORT).show() extractJsonData(response) }, Response.ErrorListener { Toast.makeText(applicationContext, "Unable to connect to the server", Toast.LENGTH_SHORT).show() }) Toast.makeText(applicationContext, "Contacting Server...", Toast.LENGTH_SHORT).show() queue.add(stringRequest) } private fun extractJsonData(jsonResponse: String) { var videosDataArray = JSONArray(jsonResponse) var singleVideoJsonObject: JSONObject var singleVideoItem: VideoItem var i = 0 var size = videosDataArray.length() while (i < size) { singleVideoJsonObject = videosDataArray.getJSONObject(i) singleVideoItem = VideoItem(singleVideoJsonObject.getString("video_id"),singleVideoJsonObject.getString("tutorial_name"),singleVideoJsonObject.getString("tutorial_level")) finalResultsArrayList.add(singleVideoItem) i++ } println("The parsed videoItems are :") finalResultsArrayList.forEach { println(it) } }}--------------------------VideoItem.kt------------package com.example.irttsearchappdata class VideoItem(val videoId : String, val videoTitle: String, val videoLevel: String)
3733 visits
Outline:About Spoken tutorial server About API Spoken Tutorial URL format How to make a request to the URL? Add the dependency of Volley library and Picasso Add the required code to get Internet permission for the App Display JSON data for various FOSS id of spoken tutorials How JSON works? Write Volley code required to request the URL to fetch data Pass the Foss and Language ids to the URL of Spoken Tutorial Run the Kotlin App and display the fetched data
About Spoken tutorial server About API Spoken Tutorial URL format How to make a request to the URL? Add the dependency of Volley library and Picasso Add the required code to get Internet permission for the App Display JSON data for various FOSS id of spoken tutorials How JSON works? Write Volley code required to request the URL to fetch data Pass the Foss and Language ids to the URL of Spoken Tutorial Run the Kotlin App and display the fetched data
Show video info
Pre-requisite