Tải bản đầy đủ (.docx) (310 trang)

Lập trình android phần 4

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (8.12 MB, 310 trang )

Lập trình Android phần 4 (the and)
Bài 46: Sử dụng .Net Webservice trong Android
Bài 47: Xử lý tin nhắn SMS bằng BroadCastReceiver
Bài 48: Cách sử dụng Facebook SDK trong Android
Bài 49: Cách debug ứng dụng trên thiết bị Android bằng WIFI (không cần cắm
USB Cable)
Bài 50: Cách sử dụng Google Cloud Message trong Android
Bài 51: Xử lý JSON trong Android
Bài 52: Tạo định dạng JSON trong C# asp.net service vs Android
Bài 57: Xây dựng phần mềm nghe trộm Tin Nhắn trong Android
Bài 58: Làm quen với môi trường lập trình Android Studio – Phần 1
Bài 59: Làm quen với môi trường lập trình Android Studio – Phần 2
Bài 60: Cấu hình Hardware Accelerated Execution để chạy máy ảo Android 5.0
Bài 61: Cách đưa định dạng JSon về Java class bằng GSon
Bài 62: Cách đưa định dạng JSon về Java class bằng GSon (tiếp 1)
Bài 63: Cách đưa định dạng JSon về Java class bằng GSon (tiếp 2)
Bài 65: Xây dựng phần mềm Camera Uploader trong Android Studio
Bài 66: Cách vẽ các loại biểu đồ trong Android bằng AChartEngine
Bài 67: Cài đặt Google Play Service cho máy ảo Genymotion

1


Bài 46: Sử dụng .Net Webservice trong Android
Để làm được bài 46 thì các bạn nên xem qua trước bài tập 44 và 45.
Trong bài tập 46 này Tui muốn trình bày 4 ví dụ về cách tương tác từ Android tới
.net web service thông qua thư viện KSOAP API.
Các ví dụ này lần lượt sẽ là:
1) Cách lấy dữ liệu Primitive Data từ .net webservice xuống Android
2) Cách lấy dữ liệu Complex Data từ .net webservice xuống Android
3) Cách truyền Parameter primitive data từ Android tới .net web service


4) Cách truyền Object data từ Android tới .net web service
Thư viện KSOAP API các bạn tải ở đây (tạm thời tự nghiên cứu
trước): />Tui sẽ tranh thủ đưa 4 ví dụ trên để các bạn có thể kịp làm bài tập 43.
——————————————————————————
Tất cả 4 ví dụ này Tui sẽ sử dụng Service Description
của ở bài tập trước. Khi
bạn mở với Service description thì nó cung cấp cho bạn đầy đủ thông tin như hình
Tui chụp dưới đây:


NameSpace và tên hàm là rất quan trọng để dựa vào nó ta truy xuất dữ liệu, nên sai
namespace hay method thì chắc chắn không thể truy suất.
Sau đây tui hướng dẫn các bạn những ví dụ ở trên
Các bài phải :
<uses-permission android:name=”android.permission.INTERNET” />
1) Cách lấy dữ liệu Primitive Data từ .net webservice xuống Android
– Bạn tạo một Project Android giống như hình Tui chụp dưới này:


Bạn chú ý là Tui đánh thứ tự từ 1, 2, 3, 4 là có ý đồ kỹ thuật. Bạn phải sắp xếp như
vậy thì chương trình mới sử dụng đúng thư viện và có thể truy suất webservice, nếu
đặt sai có thể nó báo lỗi không thể thực thi ứng dụng.
Mặc định khi bạn tạo Project thì nó sẽ không đúng thứ tự như vậy đâu, bạn phải tự
làm lại thứ tự, cách đổi thứ tự như thế nào Tui sẽ nói rõ cho bạn.
Tiếp tới là bạn phải tham chiếu tới thự Viện KSOAP API mà tui khoanh màu đỏ, bạn
lưu nó ở đâu thì bạn tham chiếu tới cho đúng, hoặc bạn có thể nhét nó vào thư
viện libs. (bấm chuột phải vào Project / chọn Build Path/ chọn Add External
Archives –> chọn đúng thư viện KSOAP API để add vào ứng dụng)
– Cách đổi thứ tự tham chiếu như sau:
Bấm chuột phải vào Project của bạn/ chọn Build Path/Configure build path…:



Cửa sổ mới hiển thị lên/chọn tab Order and export:

Ta sắp xếp như màn hình trên, để di chuyển thì dùng các nút : Up,down, top, bottom.


Bạn chú ý là phải đảm bảo rằng tất cả các mục trong này được CHECKED rồi mới
bấm nút OK.
– Sau đây là giao diện chính (rất đơn giản) của project này như sau:

– Nút
“Get Catalog count” sẽ triệu gọi Service và trả về có bao nhiêu Danh mục trong Cơ
sở dữ liệu.
– Cấu trúc XML của giao diện:
1
2
3
4
5
6
7
8
9
10
11

<LinearLayout xmlns:android=" />xmlns:tools=" />android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:orientation="vertical"
tools:context=".MainActivity" >
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number Of Catalog:" />


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30


android:id="@+id/txtcount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#008040"
android:gravity="center"
android:textColor="#FFFFFF"
android:textSize="30sp" />
android:id="@+id/btncount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get Catalog Count" />
</LinearLayout>

– Ta tiến hành xử lý coding trong MainActivity.java như sau:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

16
17
18
19
20
21
22
23
24
25
26
27
28
29

package tranduythanh.com;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import
import
import
import
import
import

android.os.Bundle;
android.os.StrictMode;

android.app.Activity;
android.view.View;
android.widget.Button;
android.widget.TextView;

public class MainActivity extends Activity {
TextView txtcount;
Button btncount;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//thiết lập Permit để kết nối internet
StrictMode.ThreadPolicy policy = new
StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
txtcount=(TextView) findViewById(R.id.txtcount);
btncount=(Button) findViewById(R.id.btncount);
btncount.setOnClickListener(new
View.OnClickListener() {
public void onClick(View arg0) {
doCount();
}
});
}


30
31
32

33
34
35 public void doCount(){
36 try{
37 //khai báo namespace trong Webservice
38 final String NAMESPACE=" />//hàm cần truy suất
39 final String METHOD_NAME="CountCatalog";
40 final String SOAP_ACTION=NAMESPACE+METHOD_NAME;
41 //service description
42 final String URL=" />43 //khai báo đối tượng SoapOBject
SoapObject request=new SoapObject(NAMESPACE, METHOD_NAME);
44 //thiết lập version
45 SoapSerializationEnvelope envelope=
46 new SoapSerializationEnvelope(SoapEnvelope.VER11);
47 envelope.dotNet=true;
//thiết lập output
48 envelope.setOutputSoapObject(request);
49 //tạo đối tượng HttpTransportSE
50 HttpTransportSE androidHttpTransport=
51 new HttpTransportSE(URL);
52 //tiến hành triệu gọi Service
androidHttpTransport.call(SOAP_ACTION, envelope);
53 //lấy kết quả trả về SoapPrimitive, vì hàm CountCatalog chỉ trả về kiểu
54 int (primitive data)
55 SoapPrimitive response=(SoapPrimitive) envelope.getResponse();
56 //hiển thị kết quả lên giao diện
57 txtcount.setText(response.toString());
}
58 catch(Exception e) {
59 e.printStackTrace();

60 }
61 }
}//end MainActivity
62
63
64
65
66
67

– Bạn quan sát dòng lệnh:
1
2
3
4

StrictMode.ThreadPolicy policy = new
StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);

Dòng lệnh trên là cho phép ứng dụng kết nối tới internet, nhưng chú ý cách làm này là không TỐT, phải sửa
lại. Từ Android 4.0 nó yêu cầu khi kết nối internet phải viết trong Tiểu Trình (dùng đa tiến trình, các bạn
xem lại các ví dụ trước). Còn viết như bên trên TUI viết thì rất dở, nó làm Đơ Ứng Dụng vì phải mất Time
Request, chỉ là giải pháp tình huống để các bạn dễ hiểu thôi.


Bạn nên xóa dòng lệnh bên trên đi, đưa hàm doCount() vào một tiểu trình (bạn dùng
Handler class hoặc AsyncTask class), cách làm đa tiến trình Tui đã hướng dẫn rất kỹ
lưỡng ở những bài trước, bài này các bạn tự làm (nếu không làm được thì chưa đạt).

Bạn tải source code ở đây (nhớ khi down về thì đổi lại tham chiếu tới thư viện
KSOAP), bạn nên nhét nó vào thư mục Libs để chép tới nơi khác không phải tham
chiếu lại:
/>tiveData.rar
Bạn phải chú ý làm nhiều lần để cho hiểu rõ vấn đề.
2) Cách lấy dữ liệu Complex Data từ .net webservice xuống Android
– Ở phần 1 Bạn đã biết cách truy suất webservice và trả về dữ liệu Primivite data,
trong bài này Tui hướng dẫn các bạn lấy dữ liệu về dạng Complex Data.
– Cách cấu hình tham chiếu thư viện, đổi thứ tự tham chiếu … thì ở mục 1 tui đã
nói, mục này Tui đi thẳng vào ứng dụng:
– Tui muốn tạo 1 Ứng dụng cho phép triệu gọi danh sách Danh Mục Sản phẩm từ
webservice với giao diện vô cùng đơn giản như sau (bạn tạo ứng dụng tên
là LearnWebservice_complex_data):



Ứng dụng trên sẽ triệu gọi hàm : getListCatalog.
– Nó khác biệt là nó trả về danh sách đối tượng Danh Mục, làm thế nào để ta lấy
được danh sách đối tượng nafy???
– Sau đây là XML Layout của ứng dụng:
1
2
3
4
5
6
7
8
9
10

11
12
13
14
15

<LinearLayout xmlns:android=" />xmlns:tools=" />android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
android:id="@+id/btnlistcatalog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get List Catalog" />
android:id="@+id/lvcatalog"
android:layout_width="match_parent"


16
17
18
19
20
21

android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

– Source code Xử lý trong MainActivity như sau:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

package tranduythanh.com;
import java.util.ArrayList;
import
import
import
import
import

org.ksoap2.SoapEnvelope;
org.ksoap2.serialization.MarshalFloat;
org.ksoap2.serialization.SoapObject;
org.ksoap2.serialization.SoapSerializationEnvelope;
org.ksoap2.transport.HttpTransportSE;


import
import
import
import
import
import
import

android.os.Bundle;
android.os.StrictMode;
android.app.Activity;
android.view.View;
android.widget.ArrayAdapter;
android.widget.Button;
android.widget.ListView;

public class MainActivity extends Activity {
Button btngetlist;
ListView lvcatalog;
final String URL=" />ArrayList<String> arrCate=new ArrayList<String>();
ArrayAdapter<String>adapter=null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new
StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
lvcatalog=(ListView) findViewById(R.id.lvcatalog);
btngetlist=(Button) findViewById(R.id.btnlistcatalog);

btngetlist.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0)
{doGetList();}
});
adapter=new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, arrCate);
lvcatalog.setAdapter(adapter);
}
public void doGetList() {
try{final String NAMESPACE=" />final String METHOD_NAME="getListCatalog";
final String SOAP_ACTION=NAMESPACE+METHOD_NAME;
SoapObject request=new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope=
new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
//Nếu truyền số thực trên mạng bắt buộc phải đăng ký MarshalFloat


43
44
45
46
47
48
49
50
51
52
53

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

//không có nó thì bị báo lỗi
MarshalFloat marshal=new MarshalFloat();

marshal.register(envelope);
HttpTransportSE androidHttpTransport=
new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
//Get Array Catalog into soapArray
SoapObject soapArray=(SoapObject) envelope.getResponse();
arrCate.clear();
//soapArray.getPropertyCount() return number of
//element in soapArray
//vòng lặp duyệt qua từng dòng dữ liệu
for(int i=0; i{
//(SoapObject) soapArray.getProperty(i) get item at position i
SoapObject soapItem =(SoapObject) soapArray.getProperty(i);
//soapItem.getProperty("CateId") get value of CateId property
//phải mapp đúng tên cột:
String cateId=soapItem.getProperty("CateId").toString();
String cateName=soapItem.getProperty("CateName").toString();
//đẩy vào array
arrCate.add(cateId+" - "+cateName);
}
//xác nhận cập nhật giao diện
adapter.notifyDataSetChanged();
}
catch(Exception e){}}

}

Như vậy bạn chú ý có sự khác biệt trong Vòng lặp duyệt để lấy thông tin từng dòng
đối tượng, bạn chú ý ngẫm nghĩ để tự áp dụng cho các bài toán khác (lấy danh sách

sản phẩm, lấy danh sách sản phẩm theo danh mục ….)
Bạn tải source code mẫu ở
đây: />lex_data.rar
Chú ý phải làm lại nhiều lần, nhất là Tui đảm bảo các bạn còn Quáng Gà với vòng
lặp lấy dữ liệu ở trên.


3) Cách truyền Parameter primitive data từ Android tới .net web service
– Hai phần trên các bạn đã biết cách lấy dữ liệu primitive data và complex data từ
Server về client, phần 3 này các bạn sẽ được học cách thức truyền dữ liệu từ client
lên server thông qua Webservice như thế nào.
– Giao diện của bài tập 3 như sau:

– Mô tả sương sương như sau: Màn hình chính cho phép hiển thị danh mục, mỗi lần
nhấn vào danh mục nào đó thì hiển thị danh sách sản phẩm của danh mục này. Ở đây
ta dùng hàm getListProductByCatalogId của webservice để hiển thị danh sách sản
phẩm theo danh mục.
-Bạn tạo Project như dưới đây:


– Vậy chương trình có 2 Activity, một số class: Product, Cate…
– Layout “activity_main.xml”:
1
2
3
4
5
6
7
8

9
10
11
12
13
14
15
16
17
18
19
20
21

<LinearLayout xmlns:android=" />xmlns:tools=" />android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
android:id="@+id/btnlistcatalog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get List Catalog" />
android:id="@+id/lvcatalog"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>



– Layout “activity_list_product_by_catalog.xml”:
1
2
3
4
5
6
7
8
9
10
11
12
13
14

<LinearLayout xmlns:android=" />xmlns:tools=" />android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ListProductByCatalogActivity" >
android:id="@+id/lvproduct"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>


– Lớp Product:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

30
31
32

package tranduythanh.com;
public class Product {
private String productId;
private String productName;
private int quantity;
private double unitPrice;
private double totalMoney;
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public double getUnitPrice() {

return unitPrice;
}
public void setUnitPrice(double unitPrice) {
this.unitPrice = unitPrice;
}
public double getTotalMoney() {
return totalMoney;
}
public void setTotalMoney(double totalMoney) {
this.totalMoney = totalMoney;
}
public Product(String productId, String productName, int quantity,


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

49
50
51
52
53

double unitPrice, double totalMoney) {
super();
this.productId = productId;
this.productName = productName;
this.quantity = quantity;
this.unitPrice = unitPrice;
this.totalMoney = totalMoney;
}
@Override
public String toString() {
return this.productName+"-"+(this.totalMoney);
}
}

– Lớp Cate:
1
2
3
4
5
6
7
8
9

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

package tranduythanh.com;
public class Cate {
private String cateId;
private String cateName;
public String getCateId() {
return cateId;
}
public void setCateId(String cateId) {
this.cateId = cateId;
}
public String getCateName() {

return cateName;
}
public void setCateName(String cateName) {
this.cateName = cateName;
}
public Cate(String cateId, String cateName) {
super();
this.cateId = cateId;
this.cateName = cateName;
}
@Override
public String toString() {
return this.cateId.trim()+" - "+this.cateName.trim();
}
}


28

– Lớp MainActivity:
1
2
3
4
5
6
7
8
9
10

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

41
42
43
44
45
46
47

package tranduythanh.com;
import java.util.ArrayList;
import
import
import
import
import

org.ksoap2.SoapEnvelope;
org.ksoap2.serialization.MarshalFloat;
org.ksoap2.serialization.SoapObject;
org.ksoap2.serialization.SoapSerializationEnvelope;
org.ksoap2.transport.HttpTransportSE;

import
import
import
import
import
import
import
import

import
import
import

android.os.Bundle;
android.os.StrictMode;
android.app.Activity;
android.content.Intent;
android.view.View;
android.widget.AdapterView.OnItemClickListener;
android.widget.AdapterView;
android.widget.ArrayAdapter;
android.widget.Button;
android.widget.ListView;
android.widget.Toast;

public class MainActivity extends Activity {
Button btngetlist;
ListView lvcatalog;
final String URL=" />WSDL";
ArrayList<Cate> arrCate=new ArrayList<Cate>();
ArrayAdapter<Cate>adapter=null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new
StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
lvcatalog=(ListView) findViewById(R.id.lvcatalog);

btngetlist=(Button) findViewById(R.id.btnlistcatalog);
btngetlist.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0)
{doGetList();}
});
adapter=new ArrayAdapter<Cate>
(this, android.R.layout.simple_list_item_1, arrCate);
lvcatalog.setAdapter(adapter);
lvcatalog.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
//xử lý hiển thị danh sách sản phẩm theo danh mục
Cate c=arrCate.get(arg2);
Intent in=new Intent(MainActivity.this,
ListProductByCatalogActivity.class);
in.putExtra("cateid", c.getCateId());
startActivity(in);
}


48
49
50
51
52
53
54
55
56

57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

87
88
89
90
91
92
93
94
95
96
97
98
99

});
}
public void doGetList() {
try{final String NAMESPACE=" />final String METHOD_NAME="getListCatalog";
final String SOAP_ACTION=NAMESPACE+METHOD_NAME;
SoapObject request=new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope=
new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
//Nếu truyền số thực trên mạng bắt buộc phải đăng ký MarshalFloat
//không có nó thì bị báo lỗi
MarshalFloat marshal=new MarshalFloat();
marshal.register(envelope);
HttpTransportSE androidHttpTransport=
new HttpTransportSE(URL);

androidHttpTransport.call(SOAP_ACTION, envelope);
//Get Array Catalog into soapArray
SoapObject soapArray=(SoapObject) envelope.getResponse();
arrCate.clear();
//soapArray.getPropertyCount() return number of
//element in soapArray
//vòng lặp duyệt qua từng dòng dữ liệu
for(int i=0; i{
//(SoapObject) soapArray.getProperty(i) get item at position i
SoapObject soapItem =(SoapObject) soapArray.getProperty(i);
//soapItem.getProperty("CateId") get value of CateId property
//phải mapp đúng tên cột:
String cateId=soapItem.getProperty("CateId").toString();
String cateName=soapItem.getProperty("CateName").toString();
//đẩy vào array
Cate c=new Cate(cateId, cateName);
arrCate.add(c);
}
//xác nhận cập nhật giao diện
adapter.notifyDataSetChanged();
}
catch(Exception e)
{
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}



100
101
102

– Lớp ListProductByCatalogActivity:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

package tranduythanh.com;
import java.util.ArrayList;
import
import
import
import
import

org.ksoap2.SoapEnvelope;

org.ksoap2.serialization.MarshalFloat;
org.ksoap2.serialization.SoapObject;
org.ksoap2.serialization.SoapSerializationEnvelope;
org.ksoap2.transport.HttpTransportSE;

import
import
import
import
import
import
import

android.os.Bundle;
android.os.StrictMode;
android.app.Activity;
android.content.Intent;
android.view.Menu;
android.widget.ArrayAdapter;
android.widget.ListView;

public class ListProductByCatalogActivity extends Activity {
ListView lvproduct;
final String URL=" />WSDL";
ArrayList<Product> arrProduct=new ArrayList<Product>();
ArrayAdapter<Product>adapter=null;
String cateId="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_list_product_by_catalog);
StrictMode.ThreadPolicy policy = new
StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
lvproduct=(ListView) findViewById(R.id.lvproduct);
adapter=new ArrayAdapter<Product>
(this, android.R.layout.simple_list_item_1, arrProduct);
lvproduct.setAdapter(adapter);
Intent in=getIntent();
cateId= in.getStringExtra("cateid");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
doGetListProduct();
}
public void doGetListProduct()
{
try{
final String NAMESPACE=" />final String METHOD_NAME="getListProductByCatalogId";


46
47
48
49
50
51

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

final String SOAP_ACTION=NAMESPACE+METHOD_NAME;
SoapObject request=new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("id", cateId);
SoapSerializationEnvelope envelope=
new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
MarshalFloat marshal=new MarshalFloat();
marshal.register(envelope);
HttpTransportSE androidHttpTransport=
new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
//Get Array Catalog into soapArray

SoapObject soapArray=(SoapObject) envelope.getResponse();
arrProduct.clear();
//soapArray.getPropertyCount() return number of
//element in soapArray
//vòng lặp duyệt qua từng dòng dữ liệu
for(int i=0; i{
//(SoapObject) soapArray.getProperty(i) get item at position i
SoapObject soapItem =(SoapObject) soapArray.getProperty(i);
//phải mapp đúng tên cột:
String productId=soapItem.getProperty("ProductId").toString();
String productName=soapItem.getProperty("ProductName").toString();
String squantity=soapItem.getProperty("Quantity").toString();
String sunitPrice=soapItem.getProperty("UnitPrice").toString();
String stotalMoney=soapItem.getProperty("TotalMoney").toString();
int quantity=Integer.parseInt(squantity);
double unitPrice=Double.parseDouble(sunitPrice);
double totalMoney=Double.parseDouble(stotalMoney);
//đẩy vào array
Product p=new Product(productId, productName, quantity, unitPrice,
totalMoney);
arrProduct.add(p);
}
//xác nhận cập nhật giao diện
adapter.notifyDataSetChanged();
}
catch(Exception e) {}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.list_product_by_catalog, menu);
return true;
}
}


98
99
100
101
102

– Quan sát dòng 55-56 đó là cách truyền Primivte data từ Android lên Webservice.
– Ta nên để trong OnReSume để đảm bảo rằng mọi thứ được sẵn sàng. Trong này sẽ
lọc toàn bộ sản phẩm theo danh mục.
– Source code phần 3 ở
đây: />rameter_Primitive.rar
Các bạn chú ý phải làm lại nhiều lần bài 3 này vì nó khó và hay!
4) Cách truyền Object data từ Android tới .net web service
– Đây là phần cuối cùng Tui muốn hướng dẫn các bạn cách truyền Object Data từ
Client Android lên Server thông qua Webservice.
– Ở bài tập 4 này bạn bổ sung thêm 1 hàm InsertCatalog vào Webservice, với đối số
truyền vào là đối tượng Catalog:
1
2
3
4
5

6
7
8
9
10
11
12
13
14
15
16

//10 - thêm 1 catalog vào CSDL
[WebMethod]
public int insertCatalog(Catalog cate)
{
try
{
cate.Products.Clear();
db.Catalogs.InsertOnSubmit(cate);
db.SubmitChanges();
}
catch
{
return -1;
}
return 1;
}

– Tui có giao diện đơn giản để demo cho phần này như sau:



– Khi nhấn nút “Show List Catalog”: Chương trình sẽ triệu gọi Webservice để hiển
thị danh mục sản phẩm vào ListView bên dưới.
– Khi nhấn nút “Insert Cate”: Chương trình sẽ truyền đối tượng Catalog từ client lên
Server thông qua Webservice. Ở đây Tui dùng cách truyền Properties cho tiện. Bạn
chú ý phải quan sát kỹ Service Description của nó để truyền cho đúng Parameter:


– Tương tự như cho các hàm mà có truyền tham số khác cũng vậy, phải đặt đúng tên
trong Service Descripton.
– Ta có XML Layout của ứng dụng như sau:
1
2

<LinearLayout xmlns:android=" />xmlns:tools=" />android:id="@+id/LinearLayout1"


3
4
5
6
7
8
9
10
11
12
13
14

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

45
46
47
48
49
50
51
52
53
54

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFF80"
android:text="Input Catalog:"
android:textSize="20sp" />
android:layout_width="match_parent"
android:layout_height="wrap_content" >
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F0E7B3"
android:text="Cate Id:" />
android:id="@+id/editcateid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
</TableRow>
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F0E7B3"
android:text="Cate Name:" />
android:id="@+id/editcatename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />

</TableRow>
android:id="@+id/tableRow4"


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

android:layout_width="wrap_content"
android:layout_height="wrap_content" >
android:id="@+id/btninsertcate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:text="Insert Cate" />
</TableRow>
</TableLayout>

android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF80FF"
android:text="Show List Catalog" />
android:id="@+id/btnshowlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show List Catalog" />
android:id="@+id/lvcatalog"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>

– Xử lý coding cho MainActivity như sau:
1
2
3
4
5

package tranduythanh.com;
import java.util.ArrayList;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.MarshalFloat;



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×