问题
I am trying to hit a web service with an android application. I am getting following exception about which I don't have any Idea. Please help:
09-01 11:21:29.873: WARN/System.err(921): SoapFault - faultcode: 'soapenv:Server' faultstring: 'com.hello.service.DataStore' faultactor: 'null' detail: org.kxml2.kdom.Node@44e9a320
09-01 11:21:29.883: WARN/System.err(921): at org.ksoap2.serialization.SoapSerializationEnvelope.parseBody(SoapSerializationEnvelope.java:112)
09-01 11:21:29.883: WARN/System.err(921): at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:137)
09-01 11:21:29.883: WARN/System.err(921): at org.ksoap2.transport.Transport.parseResponse(Transport.java:63)
09-01 11:21:29.883: WARN/System.err(921): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:100)
09-01 11:21:29.883: WARN/System.err(921): at com.example.HelloTestActivity.onCreate(HelloTestActivity.java:64)
09-01 11:21:29.883: WARN/System.err(921): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-01 11:21:29.883: WARN/System.err(921): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-01 11:21:29.883: WARN/System.err(921): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-01 11:21:29.883: WARN/System.err(921): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-01 11:21:29.883: WARN/System.err(921): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-01 11:21:29.883: WARN/System.err(921): at android.os.Handler.dispatchMessage(Handler.java:99)
09-01 11:21:29.883: WARN/System.err(921): at android.os.Looper.loop(Looper.java:123)
09-01 11:21:29.894: WARN/System.err(921): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-01 11:21:29.894: WARN/System.err(921): at java.lang.reflect.Method.invokeNative(Native Method)
09-01 11:21:29.894: WARN/System.err(921): at java.lang.reflect.Method.invoke(Method.java:521)
09-01 11:21:29.894: WARN/System.err(921): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-01 11:21:29.894: WARN/System.err(921): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-01 11:21:29.904: WARN/System.err(921): at dalvik.system.NativeStart.main(Native Method)
The request is not even hitting the server. It gets fail at calling only. Following is my code for web service call:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView)findViewById(R.id.hello);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
DataStore t=new DataStore("samsung");
PropertyInfo pi = new PropertyInfo();
pi.setName("name");
pi.setValue(t);
pi.setType(t.getClass());
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
envelope.setAddAdornments(false);
envelope.implicitTypes=true;
envelope.addMapping(NAMESPACE, "DataStore",new DataStore().getClass());
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
try{
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject response=(SoapObject) envelope.getResponse();
String Name = response.getProperty(0).toString();
tv.setText("Response :" + Name);
} catch (Exception e) {
e.printStackTrace();
}
}
An following is my java bean:
public class DataStore implements KvmSerializable
{
private static final String NAMESPACE = "http://service.hello.com";
private String path;
public String getPath() {
return path;
}
private void setPath(String path) {
this.path = path;
}
public DataStore(String path) {
this.path = path;
}
public DataStore() {
// TODO Auto-generated constructor stub
}
public Object getProperty(int arg0) {
Log.d("KVM","gp");
switch(arg0)
{
case 0:
Log.d("KVM","after");
return path;
}
return null;
}
public int getPropertyCount() {
Log.d("KVM","gpc");
return 1;
}
public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
Log.d("KVM","gpi "+index);
switch(index)
{
case 0:
info.type = PropertyInfo.STRING_CLASS;
info.name = "path";
break;
default:break;
}
}
public void setProperty(int index, Object value) {
Log.d("KVM","sp");
switch(index)
{
case 0:
path = (String)value;
break;
default:
break;
}
}
public void register(SoapSerializationEnvelope cm) {
// TODO Auto-generated method stub
Log.d("KVM","register");
cm.addMapping(NAMESPACE, "DataStore", this.getClass());
}
}
Please help me in resolving this issue.
Regards, Rahul
回答1:
If you have soapfault you need to parse the answer by hand :( on your exception
catch (Exception e) {
e.printStackTrace();
}
and I think you using old version of ksoap2 :( http://code.google.com/p/ksoap2-android/
this gives you the answer then you can use sax parser or other to parse your answer:
androidHttpTransport.responseDump;
if you find other way to do these please tell me I would be appreciated.
回答2:
Check your property names in android are the same with web service side .
For example .
PropertyInfo pi = new PropertyInfo();
pi.setName("name");
pi.setValue(t);
pi.setType(t.getClass());
your paramater name "name" may not same in web service method. Check Out !
回答3:
I use the following method:
/**
* Method to retrieve the errorMessage from the given SoapFault.
* @param soapFault
* @return String representing the errorMessage found in the given SoapFault.
*/
private static String getSoapErrorMessage (SoapFault soapFault) {
String errorMessage;
try {
Node detailNode = soapFault.detail;
Element faultDetailElement = (Element)detailNode.getElement(0).getChild(1);
Element errorMessageElement = (Element)faultDetailElement.getChild(0);
errorMessage = errorMessageElement.getText(0);
}
catch (Exception e) {
e.printStackTrace();
errorMessage = "Could not determine soap error.";
}
return errorMessage;
}
来源:https://stackoverflow.com/questions/7266774/how-to-resolve-soap-fault-in-ksoap2