Bài 31-Truy cập NodeJS RESTful Web Services bằng Android Kotlin-HTTPGET

Như vậy các Thím đã biết cách truy cập API lấy toàn bộ Product bằng HTTPGET trong Android Kotlin. Bài này ta tiếp tục truy cập API dùng method HTTP GET để lấy thông tin chi tiết của một Product.

Tiếp tục mở lại Project AndroidKotlinToNodeJS trong bài 30. Trong bài này ta đã có Custom layout có nút Edit. Bây giờ ta xử lý nhấn vào nút Edit đó thì mở màn hình thông tin chi tiết lên (lấy Mã của Product đang chọn rồi truy cập API lấy thông tin chi tiết Product, ví dụ lấy Product có mã P999: http://192.168.1.137/nodejsapi/products/P999

cụ thể, ta có hình sau:

Chi tiết các bước làm như sau:

Tạo một Activity mới tên là ChiTietActivity:

Layout XML của activity_chi_tiet.xml:

[code language=”xml”]
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:app=”http://schemas.android.com/apk/res-auto”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:orientation=”vertical”
tools:context=”.ChiTietActivity”>

<TextView
android:id=”@+id/textView2″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”Mã Product:”
android:textSize=”20sp” />

<EditText
android:id=”@+id/edtMa”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:ems=”10″
android:inputType=”textPersonName” />

<TextView
android:id=”@+id/textView”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”Tên Product:”
android:textSize=”20sp” />

<EditText
android:id=”@+id/edtTen”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:ems=”10″
android:inputType=”textPersonName” />
<TextView
android:id=”@+id/textView3″
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”Đơn giá:”
android:textSize=”20sp” />

<EditText
android:id=”@+id/edtDonGia”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:ems=”10″
android:inputType=”numberDecimal” />

<Button

android:id=”@+id/btnLuu”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”Đổi thông tin”
android:textSize=”20sp” />

</LinearLayout>
[/code]

Tiến hành sửa Coding ProductAdapter:

[code language=”java”]
package com.communityuni.adapter

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.AsyncTask
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.ImageView
import android.widget.TextView
import com.communityuni.androidkotlintonodejs.ChiTietActivity
import com.communityuni.androidkotlintonodejs.R
import com.communityuni.model.Product

class ProductAdapter(internal var context: Activity, internal var resource: Int) : ArrayAdapter<Product>(context, resource) {

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
var custom = context.layoutInflater.inflate(resource, null)
var txtMa = custom.findViewById<TextView>(R.id.txtMa)
var txtTen = custom.findViewById<TextView>(R.id.txtTen)
var txtDonGia = custom.findViewById<TextView>(R.id.txtDonGia)
var p = getItem(position)
txtMa.text = p!!.Ma
txtTen.text = p.Ten
txtDonGia.text = p.DonGia.toString()
var img=custom.findViewById<ImageView>(R.id.imgEdit)
img.setOnClickListener { xuLyEdit(p) }
return custom
}

private fun xuLyEdit(p: Product?) {
var i=Intent(context,ChiTietActivity::class.java)
i.putExtra(“Ma”,p!!.Ma)
context.startActivity(i)
}
}
[/code]

Coding ở trên ta bổ sung sự kiện cho ImageView, ta truyền Mã Product được chọn qua màn hình ChiTietActivity.

Bên màn hình ChiTietActivity sẽ nhận được Mã Product này mà truy cập API xem chi tiết Product là xong, Xem coding của lớp ChiTietActivity:

[code language=”java”]
package com.communityuni.androidkotlintonodejs

import android.os.AsyncTask
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.EditText
import com.communityuni.model.Product
import org.json.JSONObject
import java.net.URL

class ChiTietActivity : AppCompatActivity() {
lateinit var edtMa:EditText
lateinit var edtTen:EditText
lateinit var edtDonGia:EditText
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_chi_tiet)
addControls()
}

private fun addControls() {
edtMa=findViewById(R.id.edtMa)
edtTen=findViewById(R.id.edtTen)
edtDonGia=findViewById(R.id.edtDonGia)
}

override fun onResume() {
super.onResume()
var ma=intent.getStringExtra(“Ma”)
HTTPGetProductDetailTask().execute(ma)
}
inner class HTTPGetProductDetailTask : AsyncTask<String, Void, Product?>()
{
override fun doInBackground(vararg p0: String?): Product? {
var p:Product?=null
var ma=p0[0]
try
{
var url=URL(“http://192.168.1.137/nodejsapi/products/”+ma)
var urlConnection=url.openConnection()
var data = urlConnection.inputStream.bufferedReader().readText()
var jsonObject=JSONObject(data)
var Ma=jsonObject.getString(“Ma”)
var Ten=jsonObject.getString(“Ten”)
var DonGia=jsonObject.getDouble(“DonGia”)
p=Product(Ma,Ten,DonGia)
}
catch (ex:Exception)
{
Log.e(“LOI”,ex.toString())
}
return p
}
override fun onPostExecute(result: Product?) {
super.onPostExecute(result)
if(result!=null)
{
edtMa.setText(result.Ma)
edtTen.setText(result.Ten)
edtDonGia.setText(result.DonGia.toString())
}
}
}
}
[/code]

bài 30 ta đã biết cách triệu gọi danh sách Product rồi, trong bài 31 này lại đơn giản hơn, nó chỉ trả về duy nhất 1 Product nên ta không cần vòng lặp. Mà lấy luôn từ JSonObject.

Chỗ coding này đã quen thuộc nên Tui sẽ không giải thích kỹ lại nữa vì bạn đã hiểu trong bài 30 rồi.

Như vậy ta đã hoàn thành xong bài triệu gọi Web API lấy Chi Tiết Product được viết bằng NodeJS và Deploy trên IISNode WebServer.

Source code Triệu gọi Web API lấy thông tin chi tiết Product tải ở đây.

Bài sau Tui sẽ hướng dẫn các Thím cách dùng Android Kotlin để  truy cập API RESTful Chỉnh Sửa Product như thế nào. Các bạn chú ý theo dõi nhé (vẫn dùng lại Project này)

Các khóa học online khác, bạn có thể tham khảo tại đây:

https://unica.vn/?aff=11929

Chúc các bạn thành công!