4

我正在从我的 android 客户端应用程序调用 Web 服务。在我尝试显示它时得到响应后,我得到了 ClassCastException。以下是我的代码:

public void onClick(View v) {
  setContentView(R.layout.report);
  SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
  EpcDetails epcdetails=new EpcDetails();
  epcdetails.setEpcId(input_val.getText().toString());

        request.addProperty("id", id
        SoapSerializationEnvelope sse=new SoapSerializationEnvelope(SoapEnvelope.VER11);
        sse.setOutputSoapObject(request);

        sse.addMapping(NAMESPACE, 
        ProductDetailsRequest.ProductDetailsRequest.getSimpleName(),
        ProductDetailsRequest.ProductDetailsRequest);

        sse.implicitTypes=true;
        sse.setAddAdornments(false);
        AndroidHttpTransport aht=new AndroidHttpTransport(URL);

        try 
        {
         aht.call(SOAP_ACTION, sse);
         SoapObject response= (SoapObject) sse.getResponse();
         item_code.setText((CharSequence)(response.getProperty(6)));
         desc.setText((CharSequence) (response.getProperty(5)));
         price.setText(calc_price.toString());

        } catch (IOException e) 
        {
         e.printStackTrace();
        } catch (XmlPullParserException e)
        {
          e.printStackTrace();
       }

我在item_code.setText((CharSequence)(response.getProperty(6))) 处遇到异常; 作为

12-20 19:26:12.864: ERROR/AndroidRuntime(811): FATAL EXCEPTION: main
12-20 19:26:12.864: ERROR/AndroidRuntime(811): java.lang.ClassCastException: org.ksoap2.serialization.SoapPrimitive
12-20 19:26:12.864: ERROR/AndroidRuntime(811):     at com.trueVUE.modules.report.MainSimulation.onClick(MainSimulation.java:123)
12-20 19:26:12.864: ERROR/AndroidRuntime(811):     at android.view.View.performClick(View.java:2408)
12-20 19:26:12.864: ERROR/AndroidRuntime(811):     at android.view.View$PerformClick.run(View.java:8816)
12-20 19:26:12.864: ERROR/AndroidRuntime(811):     at android.os.Handler.handleCallback(Handler.java:587)
12-20 19:26:12.864: ERROR/AndroidRuntime(811):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-20 19:26:12.864: ERROR/AndroidRuntime(811):     at android.os.Looper.loop(Looper.java:123)
12-20 19:26:12.864: ERROR/AndroidRuntime(811):     at android.app.ActivityThread.main(ActivityThread.java:4627)
12-20 19:26:12.864: ERROR/AndroidRuntime(811):     at java.lang.reflect.Method.invokeNative(Native Method)
12-20 19:26:12.864: ERROR/AndroidRuntime(811):     at java.lang.reflect.Method.invoke(Method.java:521)
12-20 19:26:12.864: ERROR/AndroidRuntime(811):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-20 19:26:12.864: ERROR/AndroidRuntime(811):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-20 19:26:12.864: ERROR/AndroidRuntime(811):     at dalvik.system.NativeStart.main(Native Method)

请尽快提出一些解决方案。

问候, 拉胡尔

4

3 回答 3

22

因为你的 webservice 返回的类型是 String ,所以你可以这样解决:

Object  response=  sse.getResponse();  

当您的网络服务返回值类型为 byte[] 时,您可以这样做

SoapObject response=(SoapObject)envelope.bodyIn;
于 2011-06-08T18:59:06.047 回答
3

可能是您的 Web 服务正在返回一个 XML 对象,该对象在客户端导致 ClassCastException,因为 KSOAP 不支持它,尝试将您的 Web 服务的结果作为字符串返回(最好使用 CDATA),您的问题就会得到解决。

于 2011-10-24T09:24:06.343 回答
2

这个问题会对你有所帮助。

您需要使用 getString() 将返回的属性转换为字符串:

item_code.setText(response.getProperty(6).toString());
于 2010-12-20T14:38:36.237 回答