使用KSOAP调用webservice时获取NumberFormatException

时间:2012-12-31 13:37:43

标签: android android-ksoap2 numberformatexception

我正在使用此代码调用在PHP中创建的网络服务

SoapObject request = new SoapObject("getCustomerInfo",  "getCustomerInfo");
    request.addProperty("user_id", Info.userId);
    request.addProperty("authToken", Info.authToken);
    request.addProperty("request_id", Integer.toString(CUSTOMERACCOUNTINFO));

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        HttpTransportSE httpTransport = new HttpTransportSE(soapURL);
        httpTransport.debug=true;
        try {               
            httpTransport.call(soap, envelope); //Throwing Exception here
            System.out.println("dump state "+httpTransport.responseDump);
            client.success((Object) envelope.getResponse());
        }catch(NumberFormatException e){
            e.printStackTrace();
            System.out.println(" exception  num format");
            //client.error("NumberFormatException");
            client.error(e.getLocalizedMessage());
        }catch(XmlPullParserException xppe){
            System.out.println(" exception  pullparser");
            //client.error("XmlPullParserException");
            client.error(xppe.getLocalizedMessage());
        }catch(IOException ioe){
            System.out.println(" exception  io"+ ioe.toString());
            //client.error("IOException");
            client.error(ioe.getLocalizedMessage());
        }

并收到以下错误

12-31 10:52:03.024: W/System.err(2306): java.lang.NumberFormatException: Invalid int: ""
12-31 10:52:03.074: W/System.err(2306):     at java.lang.Integer.invalidInt(Integer.java:138)
12-31 10:52:03.104: W/System.err(2306):     at java.lang.Integer.parseInt(Integer.java:359)
12-31 10:52:03.124: W/System.err(2306):     at java.lang.Integer.parseInt(Integer.java:332)
12-31 10:52:03.134: W/System.err(2306):     at org.ksoap2.serialization.DM.readInstance(DM.java:39)
12-31 10:52:03.145: W/System.err(2306):     at org.ksoap2.serialization.SoapSerializationEnvelope.readInstance(SoapSerializationEnvelope.java:406)
12-31 10:52:03.155: W/System.err(2306):     at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:364)
12-31 10:52:03.174: W/System.err(2306):     at org.ksoap2.serialization.SoapSerializationEnvelope.readUnknown(SoapSerializationEnvelope.java:233)
12-31 10:52:03.184: W/System.err(2306):     at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:366)
12-31 10:52:03.194: W/System.err(2306):     at org.ksoap2.serialization.SoapSerializationEnvelope.readVector(SoapSerializationEnvelope.java:287)
12-31 10:52:03.214: W/System.err(2306):     at org.ksoap2.serialization.SoapSerializationEnvelope.readInstance(SoapSerializationEnvelope.java:432)
12-31 10:52:03.224: W/System.err(2306):     at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:364)
12-31 10:52:03.234: W/System.err(2306):     at org.ksoap2.serialization.SoapSerializationEnvelope.readUnknown(SoapSerializationEnvelope.java:233)
12-31 10:52:03.254: W/System.err(2306):     at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:366)
12-31 10:52:03.264: W/System.err(2306):     at org.ksoap2.serialization.SoapSerializationEnvelope.parseBody(SoapSerializationEnvelope.java:121)
12-31 10:52:03.274: W/System.err(2306):     at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:137)
12-31 10:52:03.284: W/System.err(2306):     at org.ksoap2.transport.Transport.parseResponse(Transport.java:63)
12-31 10:52:03.294: W/System.err(2306):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:100)
12-31 10:52:03.334: W/System.err(2306):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-31 10:52:03.344: W/System.err(2306):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-31 10:52:03.364: W/System.err(2306):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-31 10:52:03.374: W/System.err(2306):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-31 10:52:03.384: W/System.err(2306):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-31 10:52:03.394: W/System.err(2306):     at java.lang.Thread.run(Thread.java:856)

2 个答案:

答案 0 :(得分:1)

此错误表示您正在传递空字符串“”,而被调用的方法需要字符串表示数字。或者更准确地说,您应该发送的消息必须包含某个地方的数字,而不是可以解释为数字的内容,而是传递空字符串。

仔细检查您在request中输入的参数的值。我猜其中一个是导致异常的空字符串。

答案 1 :(得分:0)

我认为在CUSTOMERACCOUNTINFO中,该值为null ...因此,请务必向CUSTOMERACCOUNTINFO添加一些值并将解析添加到Integer中。

制造这样的条件..

if(CUSTOMERACCOUNTINFO.length()>0)
{
   SoapObject request = new SoapObject("getCustomerInfo",  "getCustomerInfo");
   request.addProperty("user_id", Info.userId);
   request.addProperty("authToken", Info.authToken);
   request.addProperty("request_id", Integer.toString(CUSTOMERACCOUNTINFO));
}