saving . . . saved Unable to connect to the server error has been deleted. Unable to connect to the server error has been hidden .
Unable to connect to the server error
Title
Question
My code is:
MainActvity.kt
-------------------
<pre style="background-color:#ffffff;color:#000000;font-family:'DejaVu Sans Mono';font-size:9.0pt;"><span style="color:#000080;font-weight:bold;">package </span>com.example.irttsearchapp

<span style="color:#000080;font-weight:bold;">import </span>android.content.Intent
<span style="color:#000080;font-weight:bold;">import </span>androidx.appcompat.app.AppCompatActivity
<span style="color:#000080;font-weight:bold;">import </span>android.os.Bundle
<span style="color:#000080;font-weight:bold;">import </span>android.view.View
<span style="color:#000080;font-weight:bold;">import </span>android.widget.AdapterView
<span style="color:#000080;font-weight:bold;">import </span>android.widget.ArrayAdapter
<span style="color:#000080;font-weight:bold;">import </span>android.widget.Spinner
<span style="color:#000080;font-weight:bold;">import </span>android.widget.Toast
<span style="color:#000080;font-weight:bold;">import </span>kotlinx.android.synthetic.main.activity_main.*

<span style="color:#000080;font-weight:bold;">class </span>MainActivity : AppCompatActivity() {

<span style="color:#000080;font-weight:bold;">override fun </span>onCreate(savedInstanceState: Bundle?) {
<span style="color:#000080;font-weight:bold;">super</span>.onCreate(savedInstanceState)
setContentView(R.layout.<span style="color:#660e7a;font-style:italic;">activity_main</span>)

<span style="color:#000080;font-weight:bold;">var </span>fossVsFossID = HashMap<String, Int>()
fossVsFossID.put(<span style="color:#008000;font-weight:bold;">"Java"</span>,<span style="color:#0000ff;">10</span>)
fossVsFossID.put(<span style="color:#008000;font-weight:bold;">"Cpp"</span>,<span style="color:#0000ff;">57</span>)
fossVsFossID.put(<span style="color:#008000;font-weight:bold;">"Python"</span>,<span style="color:#0000ff;">26</span>)
fossVsFossID.put(<span style="color:#008000;font-weight:bold;">"RDBMS"</span>,<span style="color:#0000ff;">92</span>)

<span style="color:#000080;font-weight:bold;">var </span>languageVsLanguageID = HashMap<String,Int>()
languageVsLanguageID.put(<span style="color:#008000;font-weight:bold;">"English"</span>,<span style="color:#0000ff;">22</span>)
languageVsLanguageID.put(<span style="color:#008000;font-weight:bold;">"Hindi"</span>,<span style="color:#0000ff;">6</span>)
languageVsLanguageID.put(<span style="color:#008000;font-weight:bold;">"Gujarati"</span>,<span style="color:#0000ff;">5</span>)
languageVsLanguageID.put(<span style="color:#008000;font-weight:bold;">"Tamil"</span>,<span style="color:#0000ff;">18</span>)
languageVsLanguageID.put(<span style="color:#008000;font-weight:bold;">"Marathi"</span>,<span style="color:#0000ff;">12</span>)
languageVsLanguageID.put(<span style="color:#008000;font-weight:bold;">"Kannada"</span>,<span style="color:#0000ff;">7</span>)

<span style="color:#000080;font-weight:bold;">var </span>fossVsLanguage = HashMap<String, ArrayList<String>>()
<span style="color:#000080;font-weight:bold;">var </span>availablelanguagesforFoss: ArrayList<String>
availablelanguagesforFoss = <span style="font-style:italic;">arrayListOf</span>(<span style="color:#008000;font-weight:bold;">"English"</span>,<span style="color:#008000;font-weight:bold;">"Gujarati"</span>,<span style="color:#008000;font-weight:bold;">"Hindi"</span>,<span style="color:#008000;font-weight:bold;">"Kannada"</span>)
fossVsLanguage.put(<span style="color:#008000;font-weight:bold;">"Java"</span>,availablelanguagesforFoss)
availablelanguagesforFoss = <span style="font-style:italic;">arrayListOf</span>(<span style="color:#008000;font-weight:bold;">"English"</span>,<span style="color:#008000;font-weight:bold;">"Gujarati"</span>,<span style="color:#008000;font-weight:bold;">"Hindi"</span>,<span style="color:#008000;font-weight:bold;">"Kannada"</span>)
fossVsLanguage.put(<span style="color:#008000;font-weight:bold;">"Cpp"</span>,availablelanguagesforFoss)
availablelanguagesforFoss = <span style="font-style:italic;">arrayListOf</span>(<span style="color:#008000;font-weight:bold;">"English"</span>,<span style="color:#008000;font-weight:bold;">"Gujarati"</span>,<span style="color:#008000;font-weight:bold;">"Hindi"</span>,<span style="color:#008000;font-weight:bold;">"Kannada"</span>)
fossVsLanguage.put(<span style="color:#008000;font-weight:bold;">"Python"</span>,availablelanguagesforFoss)
availablelanguagesforFoss = <span style="font-style:italic;">arrayListOf</span>(<span style="color:#008000;font-weight:bold;">"English"</span>,<span style="color:#008000;font-weight:bold;">"Gujarati"</span>,<span style="color:#008000;font-weight:bold;">"Hindi"</span>,<span style="color:#008000;font-weight:bold;">"Kannada"</span>)
fossVsLanguage.put(<span style="color:#008000;font-weight:bold;">"RDBMS"</span>,availablelanguagesforFoss)

<span style="color:#000080;font-weight:bold;">var </span>fossSpinner = findViewById<Spinner>(R.id.<span style="color:#660e7a;font-style:italic;">Spinner1</span>)
<span style="color:#000080;font-weight:bold;">var </span>languageSpinner = findViewById<Spinner>(R.id.<span style="color:#660e7a;font-style:italic;">Spinner2</span>)
<span style="color:#000080;font-weight:bold;">var </span>fossOptions = fossVsFossID.<span style="color:#660e7a;font-weight:bold;">keys</span>.<span style="font-style:italic;">toList</span>()
<span style="color:#000080;font-weight:bold;">var </span>languageOptions = languageVsLanguageID.<span style="color:#660e7a;font-weight:bold;">keys</span>.<span style="font-style:italic;">toMutableList</span>()

<span style="color:#000080;font-weight:bold;">var </span>selectedFoss = <span style="color:#008000;font-weight:bold;">"none"
</span><span style="color:#008000;font-weight:bold;"> </span><span style="color:#000080;font-weight:bold;">var </span>selectedLanguage = <span style="color:#008000;font-weight:bold;">"none"
</span><span style="color:#008000;font-weight:bold;">
</span><span style="color:#008000;font-weight:bold;"> </span><span style="color:#000080;font-weight:bold;">var </span>fossAdapter = ArrayAdapter<String>(<span style="color:#660e7a;font-style:italic;">applicationContext</span>,android.R.layout.<span style="color:#660e7a;font-style:italic;">simple_list_item_activated_1</span>,fossOptions)
fossSpinner.<span style="color:#660e7a;font-style:italic;">adapter </span>= fossAdapter

<span style="color:#000080;font-weight:bold;">var </span>languageAdapter = ArrayAdapter<String>(<span style="color:#660e7a;font-style:italic;">applicationContext</span>,android.R.layout.<span style="color:#660e7a;font-style:italic;">simple_list_item_activated_1</span>,languageOptions)
languageSpinner.<span style="color:#660e7a;font-style:italic;">adapter </span>= languageAdapter

fossSpinner.<span style="color:#660e7a;font-style:italic;">onItemSelectedListener </span>= <span style="color:#000080;font-weight:bold;">object </span>: AdapterView.OnItemSelectedListener {



<span style="color:#000080;font-weight:bold;">override fun </span>onNothingSelected(parent: AdapterView<*>?) {
}

<span style="color:#000080;font-weight:bold;">override fun </span>onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {

languageOptions.clear()

selectedFoss = fossOptions.get(position)

languageOptions.addAll(ArrayList(fossVsLanguage.get(selectedFoss)!!))
languageAdapter.notifyDataSetChanged()
}
}

languageSpinner.<span style="color:#660e7a;font-style:italic;">onItemSelectedListener </span>= <span style="color:#000080;font-weight:bold;">object </span>: AdapterView.OnItemSelectedListener {
<span style="color:#000080;font-weight:bold;">override fun </span>onNothingSelected(parent: AdapterView<*>?) {
}

<span style="color:#000080;font-weight:bold;">override fun </span>onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {

selectedLanguage = languageOptions.get(position)
}
}

searchButton.setOnClickListener <span style="font-weight:bold;">{
</span><span style="font-weight:bold;"> </span><span style="color:#000080;font-weight:bold;">if </span>(selectedFoss == <span style="color:#008000;font-weight:bold;">"none" </span>|| selectedLanguage == <span style="color:#008000;font-weight:bold;">"none"</span>) {

Toast.makeText(<span style="color:#000080;font-weight:bold;">this</span>, <span style="color:#008000;font-weight:bold;">"Please select FOSS and Language both"</span>, Toast.<span style="color:#660e7a;font-style:italic;">LENGTH_SHORT</span>).show()

} <span style="color:#000080;font-weight:bold;">else </span>{

<span style="color:#000080;font-weight:bold;">var </span>fossID = fossVsFossID.get(selectedFoss)
<span style="color:#000080;font-weight:bold;">var </span>languageID = languageVsLanguageID.get(selectedLanguage)

<span style="color:#000080;font-weight:bold;">var </span>searchIntent = Intent(<span style="color:#000080;font-weight:bold;">this</span>, Main2Activity::<span style="color:#000080;font-weight:bold;">class</span>.<span style="color:#660e7a;font-style:italic;">java</span>).<span style="font-style:italic;">apply </span><span style="font-weight:bold;">{
</span><span style="font-weight:bold;"> </span>putExtra(<span style="color:#008000;font-weight:bold;">"fossID"</span>, <span style="color:#008000;font-weight:bold;">"" </span>+ fossID)
putExtra(<span style="color:#008000;font-weight:bold;">"languageID"</span>, <span style="color:#008000;font-weight:bold;">"" </span>+ languageID)
<span style="font-weight:bold;">}
</span><span style="font-weight:bold;">
</span><span style="font-weight:bold;"> </span>startActivity(searchIntent)
}
<span style="font-weight:bold;">}
</span><span style="font-weight:bold;">
</span><span style="font-weight:bold;"> </span>}

}

---------------------
Main2Activity.kt
----------------
<span style="color:#000080;font-weight:bold;">package </span>com.example.irttsearchapp

<span style="color:#000080;font-weight:bold;">import </span>androidx.appcompat.app.AppCompatActivity
<span style="color:#000080;font-weight:bold;">import </span>android.os.Bundle
<span style="color:#000080;font-weight:bold;">import </span>android.widget.Toast
<span style="color:#000080;font-weight:bold;">import </span>com.android.volley.Request
<span style="color:#000080;font-weight:bold;">import </span>com.android.volley.Response
<span style="color:#000080;font-weight:bold;">import </span>com.android.volley.toolbox.StringRequest
<span style="color:#000080;font-weight:bold;">import </span>com.android.volley.toolbox.Volley
<span style="color:#000080;font-weight:bold;">import </span>kotlinx.android.synthetic.main.activity_main2.*
<span style="color:#000080;font-weight:bold;">import </span>org.json.JSONArray
<span style="color:#000080;font-weight:bold;">import </span>org.json.JSONObject

<span style="color:#000080;font-weight:bold;">class </span>Main2Activity : AppCompatActivity() {

<span style="color:#000080;font-weight:bold;">var </span><span style="color:#660e7a;font-weight:bold;">finalResultsArrayList</span>= ArrayList<VideoItem>()

<span style="color:#000080;font-weight:bold;">override fun </span>onCreate(savedInstanceState: Bundle?) {
<span style="color:#000080;font-weight:bold;">super</span>.onCreate(savedInstanceState)
setContentView(R.layout.<span style="color:#660e7a;font-style:italic;">activity_main2</span>)
<span style="color:#000080;font-weight:bold;">var </span>foss = <span style="color:#660e7a;font-style:italic;">intent</span>.getStringExtra(<span style="color:#008000;font-weight:bold;">"fossID"</span>)
<span style="color:#000080;font-weight:bold;">var </span>language = <span style="color:#660e7a;font-style:italic;">intent</span>.getStringExtra(<span style="color:#008000;font-weight:bold;">"languageID"</span>)
selectedOption.<span style="color:#660e7a;font-style:italic;">text </span>= <span style="color:#008000;font-weight:bold;">"Foss ID : " </span>+ foss + <span style="color:#008000;font-weight:bold;">"</span><span style="color:#000080;font-weight:bold;">\n</span><span style="color:#008000;font-weight:bold;">Language ID : " </span>+ language
<span style="color:#000080;font-weight:bold;">val </span>queue = Volley.newRequestQueue(<span style="color:#000080;font-weight:bold;">this</span>)
<span style="color:#000080;font-weight:bold;">val </span>url = <span style="color:#008000;font-weight:bold;">"https://spoke-tutorial.org/api/get_tutorials/"</span>+foss+<span style="color:#008000;font-weight:bold;">"/"</span>+language
<span style="color:#000080;font-weight:bold;">val </span>stringRequest = StringRequest(Request.Method.<span style="color:#660e7a;font-style:italic;">GET</span>,url, Response.Listener<String>
<span style="font-weight:bold;">{</span>response <span style="font-weight:bold;">->
</span><span style="font-weight:bold;"> </span>Toast.makeText(<span style="color:#660e7a;font-style:italic;">applicationContext</span>,<span style="color:#008000;font-weight:bold;">"Received server response!"</span>, Toast.<span style="color:#660e7a;font-style:italic;">LENGTH_SHORT</span>).show()

extractJsonData(response)

<span style="font-weight:bold;">}</span>, Response.ErrorListener <span style="font-weight:bold;">{
</span><span style="font-weight:bold;"> </span>Toast.makeText(<span style="color:#660e7a;font-style:italic;">applicationContext</span>, <span style="color:#008000;font-weight:bold;">"Unable to connect to the server"</span>, Toast.<span style="color:#660e7a;font-style:italic;">LENGTH_SHORT</span>).show()
<span style="font-weight:bold;">}</span>)
Toast.makeText(<span style="color:#660e7a;font-style:italic;">applicationContext</span>,
<span style="color:#008000;font-weight:bold;">"Contacting Server..."</span>, Toast.<span style="color:#660e7a;font-style:italic;">LENGTH_SHORT</span>).show()
queue.add(stringRequest)


}
<span style="color:#000080;font-weight:bold;">private fun </span>extractJsonData(jsonResponse: String) {

<span style="color:#000080;font-weight:bold;">var </span>videosDataArray = JSONArray(jsonResponse)

<span style="color:#000080;font-weight:bold;">var </span>singleVideoJsonObject: JSONObject
<span style="color:#000080;font-weight:bold;">var </span>singleVideoItem: VideoItem
<span style="color:#000080;font-weight:bold;">var </span>i = <span style="color:#0000ff;">0
</span><span style="color:#0000ff;"> </span><span style="color:#000080;font-weight:bold;">var </span>size = videosDataArray.length()

<span style="color:#000080;font-weight:bold;">while </span>(i < size)
{
singleVideoJsonObject = videosDataArray.getJSONObject(i)

singleVideoItem = VideoItem(singleVideoJsonObject.getString(<span style="color:#008000;font-weight:bold;">"video_id"</span>),singleVideoJsonObject.getString(<span style="color:#008000;font-weight:bold;">"tutorial_name"</span>),singleVideoJsonObject.getString(<span style="color:#008000;font-weight:bold;">"tutorial_level"</span>))

<span style="color:#660e7a;font-weight:bold;">finalResultsArrayList</span>.add(singleVideoItem)

i++
}

<span style="font-style:italic;">println</span>(<span style="color:#008000;font-weight:bold;">"The parsed videoItems are :"</span>)
<span style="color:#660e7a;font-weight:bold;">finalResultsArrayList</span>.<span style="font-style:italic;">forEach </span><span style="font-weight:bold;">{ </span><span style="font-style:italic;">println</span>(<span style="font-weight:bold;">it</span>) <span style="font-weight:bold;">}
</span><span style="font-weight:bold;">
</span><span style="font-weight:bold;"> </span>}
}
--------------------------
VideoItem.kt
------------
<span style="color:#000080;font-weight:bold;">package </span>com.example.irttsearchapp

<span style="color:#000080;font-weight:bold;">data class </span>VideoItem(<span style="color:#000080;font-weight:bold;">val </span><span style="color:#660e7a;font-weight:bold;">videoId </span>: String,
<span style="color:#000080;font-weight:bold;">val </span><span style="color:#660e7a;font-weight:bold;">videoTitle</span>: String,
<span style="color:#000080;font-weight:bold;">val </span><span style="color:#660e7a;font-weight:bold;">videoLevel</span>: String)



</pre>

Android-app-using-Kotlin URL-Request 07-08 min 40-50 sec 18-06-21, 11:38 p.m. Rsenthil_1976

Answers:


Your website name in the code is wrong.(https://spoke-tutorial.org/ )

It is spoken-tutorial.org. Change the code and try.
21-06-21, 3:27 p.m. NirmalaVenkat


Log-in to answer to this question.