Web Services 错误代码:使用soap从android设备调用Web服务时为'null'

57hvy0tb  于 2022-11-15  发布在  Android
关注(0)|答案(1)|浏览(160)

我创建了一个简单的Web服务来连接Android设备。我已经使用了JAX-WS,请遵循以下指南:http://www.mkyong.com/webservices/jax-ws/jax-ws-spring-integration-example/
当我在客户端上运行时,它是正常的,但在设备上,我有一个错误。它是“SoapFault - faultcode:“S:服务器”错误字符串:“java.lang.非法参数异常”错误指示符:'null'详细信息:******
存在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);
}

Web服务.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;
    }
}
dffbzjpn

dffbzjpn1#

请更换:request.addProperty("arg0", 1);
至:

propertyInfo1 = new PropertyInfo();
    propertyInfo1.setName("arg0");
    propertyInfo1.setValue(1);
    propertyInfo1.setType(Integer.class);
    request.addProperty(propertyInfo1);

相关问题