Java.lang.ClassCastException: org.ksoap2.serialization.SoapPrimitive?

I am calling the web service from my Android client application. After receiving the response, when I try to display it, I get a ClassCastException. Below 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 get an exception in 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 a few soln ASAP.

Regards, Rahul

+4
source share
3 answers

because the type of web service you are returning is String, so you can solve it like this:

 Object response= sse.getResponse(); 

when your webservice return type is a byte [], you can do this

 SoapObject response=(SoapObject)envelope.bodyIn; 
+23
source

Maybe your web service returns an XML object that throws a ClassCastException on the client side, because it is not supported by KSOAP, try returning the result of your web service as a string (preferably with CDATA), and your problem will be solved.

+3
source

This question will be useful to you.

You need to use getString () to convert the returned property to a string:

 item_code.setText(response.getProperty(6).toString()); 
+2
source

Source: https://habr.com/ru/post/1332770/


All Articles