我创建了一个简单的 web 服务来从 android 设备连接。我使用 JAX-WS 遵循本指南:http ://www.mkyong.com/webservices/jax-ws/jax-ws-spring-integration-example/
当我在客户端上运行时没问题,但在设备上我有故障。它是“ SoapFault - faultcode: 'S:Server' faultstring: 'java.lang.IllegalArgumentException' faultactor: 'null' detail: org.kxml2.kdom.Node@41aa21f0 ”
有WSDL文件
<!--
Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.3-b01-.
-->
<!--
Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.3-b01-.
-->
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws.dic.med.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.dic.med.com/" name="WebserviceService">
<types/>
<message name="getMed">
<part name="arg0" type="xsd:int"/>
</message>
<message name="getMedResponse">
<part name="return" type="xsd:string"/>
</message>
<portType name="WebserviceInterface">
<operation name="getMed">
<input wsam:Action="http://ws.dic.med.com/WebserviceInterface/getMedRequest" message="tns:getMed"/>
<output wsam:Action="http://ws.dic.med.com/WebserviceInterface/getMedResponse" message="tns:getMedResponse"/>
</operation>
</portType>
<binding name="WebservicePortBinding" type="tns:WebserviceInterface">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/>
<operation name="getMed">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal" namespace="http://ws.dic.med.com/"/>
</input>
<output>
<soap:body use="literal" namespace="http://ws.dic.med.com/"/>
</output>
</operation>
</binding>
<service name="WebserviceService">
<port name="WebservicePort" binding="tns:WebservicePortBinding">
<soap:address location="http://192.168.248.1:8080/Webservice1/smd"/>
</port>
</service>
</definitions>
然后是我在android中的代码
package com.example.smdtestwebservice;
import java.io.IOException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity {
private String METHOD_NAME_1 = "getMed";
private String NAME_SPACE = "http://ws.dic.med.com/";
private String SOAP_ACTION_1 = NAME_SPACE + METHOD_NAME_1;
private static final String URL = "http://192.168.248.1:8080/Webservice1/smd?wsdl";
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
SoapObject request = new SoapObject(NAME_SPACE, METHOD_NAME_1);
request.addProperty("arg0", 1);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
String a = "";
try {
androidHttpTransport.call(SOAP_ACTION_1, envelope);
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
a = response.toString();
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
Toast.makeText(this, a, Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
任何人都可以帮我解决这个问题吗?
我已经更新了代码
Web服务接口.java
package com.med.dic.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
@WebService
@SOAPBinding(style = Style.RPC)
public interface WebserviceInterface {
@WebMethod String getMed(int id);
@WebMethod String getHelloWorld(String a);
}
网络服务.java
package com.med.dic.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
import com.med.dic.dao.MedicineDAO;
@WebService(endpointInterface = "com.med.dic.ws.WebserviceInterface")
public class Webservice implements WebserviceInterface {
private MedicineDAO medicineDAO;
@WebMethod(exclude=true)
public void setMedicineDAO(MedicineDAO medicineDAO) {
this.medicineDAO = medicineDAO;
}
@Override
@WebMethod(operationName="getMed")
public String getMed(int id) {
String medName = medicineDAO.searchByID(id).getMedName();
return medName;
}
@Override
@WebMethod(operationName="getHelloWorld")
public String getHelloWorld(String a){
return "JAX-WS + Spring! " + a;
}
}