<?php
include "myconnection.php";
$id = $_GET['id'];
$sql = "SELECT * FROM tblstudents WHERE stud_id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $id);
$stmt->execute();
$rs = $stmt->get_result();
if ($rs->num_rows > 0) {
$row = $rs->fetch_assoc();
//encode to json format
$jsonData = json_encode($row);
echo($jsonData);
}else{
echo "0";
}
$stmt->close();
$conn->close();
?>
ANDROID
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
fun accessInternet(){
val studentId = tfUsername.editText?.text.toString()
val lugar = tfPassword.editText?.text.toString()
var url = "http://192.168.43.95/advprog/webservices/getstudent.php"
val urlBuilder = url.toHttpUrlOrNull()!!.newBuilder()
urlBuilder.addQueryParameter("id", studentId)
url = urlBuilder.build().toString()
val okHttpClient = OkHttpClient()
val request = Request.Builder().url(url).build()
okHttpClient.newCall(request).enqueue(object: Callback{
override fun onFailure(call: Call, e: IOException) {
runOnUiThread{
Toast.makeText(applicationContext, e.message, Toast.LENGTH_LONG).show()
}
}
override fun onResponse(call: Call, response: Response) {
val jsonString = response.body?.string()
val msg = if (jsonString?.trim() == "0"){
"Invalid Student ID "
}else{
val jsonObject = JSONObject(jsonString)
val ngalan = jsonObject.getString("stud_name")
val address = jsonObject.getString("stud_address")
"Full Name : $ngalan and the address is $address"
}
runOnUiThread{
Toast.makeText(applicationContext, msg, Toast.LENGTH_LONG).show()
}
}
})
}
No comments:
Post a Comment