我最近正在学习Java Web Service,书中有“Java Web Service:Up and Running”,在本书的第3章中,有一个名为“Rabbit Counter”的例子,它是一个Java Web服务,我尝试设置我的机器上的“Rabbit Counter”服务,所以我复制这些代码并添加到测试Web方法。这是“兔子计数器”的代码
@WebService(targetNamespace = "http://ch03.fib/")
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT,
use = SOAPBinding.Use.LITERAL,
parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
public class RabbitCounter {
//store previous computed values
private Map<Integer, Integer> cache =
Collections.synchronizedMap(new HashMap<Integer, Integer>());
@WebMethod
public int countRabbits(int n) throws FibException {
if (n < 0) throw new FibException("Negative arg not allowed ", n +" < 0");
if (n < 2) return n;
if (cache.containsKey(n)) return cache.get(n);
if (cache.containsKey(n - 1) && cache.containsKey(n - 2)) {
cache.put(n , cache.get(n - 1) + cache.get(n - 2));
return cache.get(n);
}
int fib = 1, prev = 0;
for( int i = 2; i <= n; i++) {
int tmp = fib;
fib += prev;
prev = tmp;
}
cache.put(n, fib);
return fib;
}
@WebMethod
public String testString() {
return "This is a test String";
}
@WebMethod
public int testInt(int n) {
return n;
}
}
设置完Web服务后,我编写了一个Android客户端代码,使用ksoap2库来使用“Rabbit Counter”服务。在这三种方法中,只有testString()工作正常,它返回正确的结果,但是当使用整数作为参数调用其他两个方法时,结果始终为0.这是客户端的代码。
public class rabbitclient extends Activity {
/** Called when the activity is first created. */
private static final String serviceNameSpace = "http://ch03.fib/";
private static final String countRabbits = "countRabbits";
private static final String test = "testString";
private static final String testInt = "testInt";
private static final String serviceURL = "http://10.111.230.24:8888/fib?wsdl";
private static final String soapAction = "http://ch03.fib/countRabbits";
private static final Integer value = new Integer(2);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SoapObject request = new SoapObject(serviceNameSpace, testInt);
PropertyInfo propInfo = new PropertyInfo();
propInfo.setName("n");
propInfo.setType(PropertyInfo.INTEGER_CLASS);
propInfo.setValue(value);
request.addProperty(propInfo);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = request;
(new MarshalBase64()).register(envelope);
Log.v("AAAAAAA", envelope.toString());
HttpTransportSE ht = new HttpTransportSE(serviceURL);
ht.debug = true;
try {
ht.call(null, envelope);
Log.e("SOAP_REQUEST", "----------------------------");
Log.e("SOAP_REQUEST", XmlUtils.format(ht.requestDump));
Log.e("SOAP_REQUEST", "----------------------------");
Log.e("SOAP_RESPONSE", "----------------------------");
Log.e("SOAP_RESPONSE", XmlUtils.format(ht.responseDump));
Log.e("SOAP_RESPONSE", "----------------------------");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
肥皂消息
<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
<v:Header/>
<v:Body>
<n0:testInt xmlns:n0="http://ch03.fib/" id="o0" c:root="1">
<n i:type="d:int">2</n>
</n0:testInt>
</v:Body>
</v:Envelope>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns3:testIntResponse xmlns:ns2="http://ch03.fib" xmlns:ns3="http://ch03.fib/">0</ns3:testIntResponse>
</S:Body>
</S:Envelope>
提前做了很多THX。