4

I am calling a web service from my android client application. After getting response when i am trying to display it i am getting ClassCastException. Following is my code:

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

I am getting Exception at item_code.setText((CharSequence)(response.getProperty(6))); as

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)

Please suggest some soln ASAP.

Regards, Rahul

Rahul
  • 739
  • 4
  • 15
  • 31

3 Answers3

22

because the type of your webservice return is String ,so you can solve it this way:

Object  response=  sse.getResponse();  

when your webservice return values type is byte[] ,you can do it

SoapObject response=(SoapObject)envelope.bodyIn;
Carlo Moretti
  • 2,213
  • 2
  • 27
  • 41
crazydragon
  • 236
  • 2
  • 3
  • So do we do it whenever we are not returning a string? I'm returning a class from WCF and the second code snippet helped me. I'd appreciate if you can spot the light on it :) – JustADev Dec 22 '14 at 19:24
3

May be your web service is returning an XML object which is causing ClassCastException at client's end as it is not supported by KSOAP, try returning result of your web service as a String(preferably with CDATA) and your problem would get solved.

rahil008
  • 31
  • 2
  • Hi, My web service returns XML. So is it correct to use Ksoap2 in my android application. I get the same error..Pls suggest me a correct solution. – Arun PS Aug 16 '13 at 07:13
2

This question will be helpful to you.

You need to use getString() to convert a returned property into a String:

item_code.setText(response.getProperty(6).toString());
Community
  • 1
  • 1
kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
  • I got NullPointerException when i used toString(). I am getting NullPointerException even if i am using it as: item_code.setText("helloworld"); what to please tell me. – Rahul Dec 21 '10 at 10:14