HTTP получить запрос в Kotlin and Android Studio - PullRequest
0 голосов
/ 27 июня 2019

Я совершенно новичок в Kotlin и Android-студии.URL, UserID и пароль сервера уже моего предыдущего проекта.Я хочу получить запрос с параметрами, используя HttpURLConnection.моя проблема, я не могу перечислить информацию в 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"
            }
        }
    }

    }
}
...