0

I'm completely new to Kotlin and android studio. 've URL, server UserID and Password already of my previous project. I want to get a request with parameters using HttpURLConnection . my problem I can't list information in textview

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    // get reference to all views
    var et_user_ID = findViewById(R.id.et_user_ID) as EditText
    var et_password = findViewById(R.id.et_password) as EditText
    var btn_reset = findViewById(R.id.btn_reset) as Button
    var btn_submit = findViewById(R.id.btn_submit) as Button
    val tv1 = findViewById(R.id.tv1) as TextView

    btn_reset.setOnClickListener {
        // clearing user_nameId and password edit text views on reset button click
        et_user_ID.setText("")
        et_password.setText("")
    }


    btn_submit.setOnClickListener {
        val userID = et_user_ID.text;
        val password = et_password.text;
        Toast.makeText(this@MainActivity, userID, Toast.LENGTH_LONG).show()

        var reqParam = URLEncoder.encode("userID", "UTF-8") + "=" + URLEncoder.encode(userID.toString(), "UTF-8")
        reqParam += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password.toString(), "UTF-8")

        val mURL = URL("http://localhost:9999/information?"+reqParam)

        with(mURL.openConnection() as HttpURLConnection) {
            // optional default is GET
            requestMethod = "GET"


            BufferedReader(InputStreamReader(inputStream)).use {
                val response = StringBuffer()

                var inputLine = it.readLine()
                while (inputLine != null) {
                    response.append(inputLine)
                    inputLine = it.readLine()
                }
                it.close()
                //println("Response : $response")
                tv1.text = "$response"
            }
        }
    }

    }
}
Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
  • 1
    Hello Hichmat, welcome to Stack Overflow. Your question is relatively basic and not well articulated. You cannot make network requests on the Main Thread, and by looking at your code, it seems you're really new to the platform. My best advice is to [go look at the Android Fundamentals documentation](https://developer.android.com/guide/components/fundamentals) provided by Google and take it from there. – Martin Marconcini Jun 27 '19 at 11:38
  • https://stackoverflow.com/a/61482596/4961126 https://medium.com/@ajay.dewari/connect-android-app-to-the-network-using-httpurlconnection-79a55c8c624e https://github.com/ajaydewari/Android-Network-Communication – HAXM Apr 28 '20 at 14:38

0 Answers0