开发者

Java Web Service returns incorrect value 0

开发者 https://www.devze.com 2023-03-23 12:03 出处:网络
I\'m recently studying Java Web Service with the book \"Java Web Service: Up and Running\", in chapter 3 of the book, there\'s an example called \"Rabbit Counter\" which is a Java Web Service, I try t

I'm recently studying Java Web Service with the book "Java Web Service: Up and Running", in chapter 3 of the book, there's an example called "Rabbit Counter" which is a Java Web Service, I try to set up the "Rabbit Counter" service on my machine, so I copy those codes and add to test web methods. Here is the code of "Rabbit Counter"

@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 = 
        Co开发者_如何学编程llections.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;
    }
}

After I set up the web service, I write an Android client code to consume the "Rabbit Counter" service using ksoap2 library. In these three methods, only testString() works fine, it returns the correct result, but when calling the other two methods with an integer as a parameter, the result is always 0. Here is the code of client-side.

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();
        }

    }
}

And the Soap messages

<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>

A lot of THX in advance.

0

精彩评论

暂无评论...
验证码 换一张
取 消