至此hessian與Java服務端通信所有測試完成,主要是傳值方面的測試,包括中文字符、VO對象、HashMap、List<String>和String[]的雙向傳值測試。
java
public
interface
BasicAPI {
/*
*
*/
public
String hello();
/*
* 測試中文字符
*/
public
String TestChinese(String str);
/*
* 測試對象傳入
*/
public
void
TestObject(TestVO vo);
/*
* 測試對象傳出
*/
public
TestVO TestObject();
/*
* 測試HashMap
*/
public
HashMap TestHashMap(HashMap map);
/*
* 測試數組
*/
public
String[] TestArray(String[] strArray);
/*
* 測試泛型
*/
public
List
<
String
>
TestList(List
<
String
>
list);
}
public
class
TestVO
implements
Serializable {
public
int
Id;
public
String Title;
public
String Content;
}
public
class
BasicService
extends
HessianServlet
implements
BasicAPI {
/**
*
*/
private
static
final
long
serialVersionUID
=
-
8301199876420831947L
;
private
String _greeting
=
"
Hello, world
"
;
public
void
setGreeting(String greeting)
{
_greeting
=
greeting;
}
public
String hello()
{
return
_greeting;
}
/*
* 測試中文傳輸
* @see com.BasicAPI#TestChinese(java.lang.String)
*/
@Override
public
String TestChinese(String str) {
System.out.println(
"
TestChinese:
"
+
str);
return
"
歡迎來到服務端!
"
;
}
/*
* 測試HashMap
* @see com.BasicAPI#TestHashMap(java.util.HashMap)
*/
@Override
public
HashMap TestHashMap(HashMap map) {
System.out.println(
"
TestHashMap:
"
+
map.get(
"
abc
"
));
map.put(
"
result
"
,
"
ok
"
);
return
map;
}
/*
* 測試TestObject傳入
* @see com.BasicAPI#TestObject(com.TestVO)
*/
@Override
public
void
TestObject(TestVO vo) {
System.out.println(
"
TestObjectEx:
"
+
vo.Id);
}
/*
* 測試TestObject傳出
* @see com.BasicAPI#TestObject()
*/
@Override
public
TestVO TestObject() {
System.out.println(
"
TestObjectEx Return TestVO
"
);
TestVO vo
=
new
TestVO();
vo.Id
=
1
;
vo.Title
=
"
title
"
;
vo.Content
=
"
Content
"
;
return
vo;
}
/*
* 測試List<String>
* @see com.BasicAPI#TestList(java.util.List)
*/
@Override
public
List
<
String
>
TestList(List
<
String
>
list) {
System.out.println(
"
TestList:
"
+
list.get(
0
));
list.add(
"
addOne
"
);
return
list;
}
/*
* 測試String[]
* @see com.BasicAPI#TestArray(java.lang.String[])
*/
@Override
public
String[] TestArray(String[] strArray) {
System.out.println(
"
TestArray:
"
+
strArray[
0
]);
strArray[
0
]
=
"
Change Form Server
"
;
return
strArray;
}
}
a). 關於服務端的配置請參照上篇文章:http://www.cnblogs.com/over140/archive/2010/07/30/1788563.html。
String url
=
"
http://192.168.1.1:8080/HessianServer/hello.do
"
;
HessianProxyFactory factory
=
new
HessianProxyFactory();
try
{
factory.setDebug(
true
);
factory.setReadTimeout(
1000
);
BasicAPI basic
=
(BasicAPI)factory.create(BasicAPI.
class
, url,getClassLoader());
//
Hello World
//
Toast.makeText(this, "調用結果:"+basic.hello(), Toast.LENGTH_LONG).show();
//
測試中文字符傳輸
//
Toast.makeText(this, "測試中文字符:"+basic.TestChinese("中文字符!"), Toast.LENGTH_LONG).show();
//
測試對象傳入
//
TestVO vo = new TestVO();
//
vo.Id= 1;
//
factory.setOverloadEnabled(true);
//
basic.TestObject(vo);
//
測試對象傳出
factory.setHessian2Reply(
false
);
TestVO vo
=
basic.TestObject();
Toast.makeText(
this
,
"
TestObject:
"
+
vo.Title, Toast.LENGTH_LONG).show();
//
測試List泛型
//
List<String> list = new ArrayList<String>() ;
//
list.add("1個元素");
//
factory.setHessian2Reply(false);
//
list = basic.TestList(list);
//
Toast.makeText(this, "TestObject:"+list.size(), Toast.LENGTH_LONG).show();
//
測試Hashmap
//
HashMap map = new HashMap();
//
map.put("abc", "123");
//
map = basic.TestHashMap(map);
//
Toast.makeText(this, "result:"+map.get("result"), Toast.LENGTH_LONG).show();
//
測試數組
//
String[] strArray = new String[]{"Send To Server"};
//
factory.setHessian2Reply(false);
//
strArray = basic.TestArray(strArray);
//
Toast.makeText(this, "TestArray:"+strArray[0], Toast.LENGTH_LONG).show();
}
catch
(MalformedURLException e) {
e.printStackTrace();
}
d). 另外若是有函數重載的話須要設置"factory.setOverloadEnabled(true);"。這裏TestObject()這個地方並無設置,也能調用成功,可是TestObject(TestVO vo)就必須設置了,這裏猜測是否是有默認行爲。
原本覺得能夠很快結束測試,結果卡客戶端代碼說明c裏面的問題了,也是從錯誤中reply中猜想出來,這個錯誤仍是我把服務端hessian從3.0.20換到最新的4.0.7才顯示出來,不過如今解決了,又切換回老版本的了 :)