# Axis2 Problem: leeres Array?



## trivium (2. Dez 2009)

Hallo, ich habe ein Problem bei meinem Web-Sevice für Axis2. Ich will von meinem Client aus ein Double-Array übergeben und mir dann von meinem Service die Länge des Arrays zurückgeben lassen. Ich bekomme aber immer 0.0 zurück.

Mein Service Code:


```
package de.matlabservice;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;


public class MatlabService {
	
	Socket sock = null;
	PrintWriter out = null;
	BufferedReader in = null;
	String sendRequest = "";
	String fromServer = "";
	
	public void init(org.apache.axis2.context.ServiceContext serviceContext){
		
		
		//Verbindung aufbauen
		try{
			sock = new Socket( "localhost", 4444 );
		    out = new PrintWriter( sock.getOutputStream(), true );
		    in = new BufferedReader( new InputStreamReader( sock.getInputStream()) );
		}
		catch(UnknownHostException e){
			System.err.println("no localhost");
			System.exit(1);
		} 
		catch(IOException e){
			System.err.println( e );
			System.exit(1);
		}
	}
	
	public void destroy(org.apache.axis2.context.ServiceContext serviceContext){
		
		out.close();
	    try {
			in.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    try {
			sock.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public double getMedian(double[] array){
		
		
		//Array in String wandeln
		//Form: x = [1;2;3];
		
		/*
		String stringArray = "x = [";
				
		for(int i = 0; i < array.length; i++){
			stringArray = stringArray + array[i];
			if(i<(array.length-1)){
				stringArray = stringArray + ";";
			}
		}
		
		stringArray = stringArray + "];";
		
		//Request absetzen
		String sendRequest = stringArray;
		//Array x
		out.println(sendRequest);
		//Median berechnen lassen
		out.println("median(x)");
		//Antwort einlesen
		String rez = "";
		int c;
		try {
			while((c = in.read()) != 13){
				rez = rez + ((char)c);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		*/
		//Antwort in double umwandeln
		
		return (double)array.length; 
	}
}
```

Mein Client Code:


```
package client;

import javax.xml.namespace.QName;

import org.apache.axiom.om.OMElement;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.databinding.utils.BeanUtil;
import org.apache.axis2.engine.DefaultObjectSupplier;


public class AxisMatlabClient {
	
	public static void main(String[] args2) throws AxisFault{
		
		ServiceClient sender = new ServiceClient();
		Options options = sender.getOptions();
		EndpointReference targetEPR = new EndpointReference("http://localhost:8081/axis2/services/Axis2Matlab");
		options.setTo(targetEPR);
		
		// die Operation "getMedian" soll aufgerufen werden
		QName opGetMedian = new QName("http://matlabservice.de", "getMedian");
		
		// Parameter für die Operation "getMedian" definieren
		double[] zahlen = { 0.0 , 1.0 , 5.0 };
		
		System.out.println(zahlen[1]);
		
		Object[] opArgs = new Object[] { zahlen };
		
		
		// OMElement mit der Request-Nachricht erzeugen
		OMElement request = BeanUtil.getOMElement(opGetMedian,opArgs, null, false, null);
		
		// Request an den Service schicken... der Aufruf erfolgt
		
		// synchron mit dem Kommunikationsmuster IN-OUT
		OMElement response = sender.sendReceive(request);
		
	
		System.out.println(response.toString());
		
		
		// diese Typen sollte der Web Service zurückliefern...
		Class<?>[] returnTypes = new Class[] { Double.class };
		
		
		// Antwort mit Hilfsroutine in ein Objekt-Array überführen
		Object[] result = BeanUtil.deserialize(response, returnTypes, new DefaultObjectSupplier());
		
		// Median ausgeben
		double median = (Double) result[0];
		
		System.out.println("Der Median ist:" + median);
		
		
	}
}
```

Meine services.xml:

[XML]<service>
  <description>MatlabJavaService</description>
  <parameter name="ServiceClass">
    de.matlabservice.MatlabService
  </parameter>
  <operation name="getMedian">
    <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
  </operation>
</service>[/XML]

Meine services.wsdl:

[XML]<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" 
xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" 
xmlns:xsd="http://matlabservice.de" xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://matlabservice.de">
    <wsdl:types>
        <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" 
            targetNamespace="http://matlabservice.de">
            <xs:element name="getMedian">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element maxOccurs="unbounded" minOccurs="0" name="array" type="xs:double"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
            <xs:element name="getMedianResponse">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="0" name="return" type="xs:double"/>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:schema>
    </wsdl:types>
    <wsdl:message name="getMedianRequest">
        <wsdlart name="parameters" element="xsd:getMedian"/>
    </wsdl:message>
    <wsdl:message name="getMedianResponse">
        <wsdlart name="parameters" element="xsd:getMedianResponse"/>
    </wsdl:message>
    <wsdlortType name="MatlabServicePortType">
        <wsdlperation name="getMedian">
            <wsdl:input message="xsd:getMedianRequest" wsaw:Action="urn:getMedian"/>
            <wsdlutput message="xsd:getMedianResponse" wsaw:Action="urn:getMedianResponse"/>
        </wsdlperation>
    </wsdlortType>
    <wsdl:binding name="MatlabServiceSoap11Binding" type="xsd:MatlabServicePortType">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
        <wsdlperation name="getMedian">
            <soapperation soapAction="urn:getMedian" style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdlutput>
                <soap:body use="literal"/>
            </wsdlutput>
        </wsdlperation>
    </wsdl:binding>
    <wsdl:binding name="MatlabServiceSoap12Binding" type="xsd:MatlabServicePortType">
        <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
        <wsdlperation name="getMedian">
            <soap12peration soapAction="urn:getMedian" style="document"/>
            <wsdl:input>
                <soap12:body use="literal"/>
            </wsdl:input>
            <wsdlutput>
                <soap12:body use="literal"/>
            </wsdlutput>
        </wsdlperation>
    </wsdl:binding>
    <wsdl:binding name="MatlabServiceHttpBinding" type="xsd:MatlabServicePortType">
        <http:binding verb="POST"/>
        <wsdlperation name="getMedian">
            <httpperation location="MatlabService/getMedian"/>
            <wsdl:input>
                <mime:content type="text/xml" part="getMedian"/>
            </wsdl:input>
            <wsdlutput>
                <mime:content type="text/xml" part="getMedian"/>
            </wsdlutput>
        </wsdlperation>
    </wsdl:binding>
    <wsdl:service name="MatlabService">
        <wsdlort name="MatlabServiceHttpSoap11Endpoint" binding="xsd:MatlabServiceSoap11Binding">
            <soap:address location="http://localhost:8080/axis2/services/MatlabService"/>
        </wsdlort>
        <wsdlort name="MatlabServiceHttpSoap12Endpoint" binding="xsd:MatlabServiceSoap12Binding">
            <soap12:address location="http://localhost:8080/axis2/services/MatlabService"/>
        </wsdlort>
        <wsdlort name="MatlabServiceHttpEndpoint" binding="xsd:MatlabServiceHttpBinding">
            <http:address location="http://localhost:8080/axis2/services/MatlabService"/>
        </wsdlort>
    </wsdl:service>
</wsdl:definitions>
[/XML]

Die services.xml habe ich selbst erstellt und die services.xml habe ich mir vom Axis2 Codegernerator erstellen lassen.


----------



## Keo (3. Dez 2009)

benutzt doch für die Client-Implementierung die generierten Sourcen. Das wäre einfacher:


```
sender.getMedian(zahlen)
```

die Rückgabe müsste wahrscheinlich auch schon das double besitzen.


----------

