Monday 30 January 2017

HTTP get Request in Kotlin and android studio

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"
}

}
}

}
}

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...