We help IT Professionals succeed at work.

Java Array string

JonMny
JonMny asked
on
931 Views
Last Modified: 2012-06-22
I am connecting to a WCF service and it's expecting me to send an

Array of Array of string

I am creating a multi dimensional array in java and assigning it to that value but the WCF sees it as null.

on the WCF the function it is expecting an mutidimentional array

String [10][10]

any suggestions

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
>>I am creating a multi dimensional array in java and assigning it to that value

How? Please post code

Author

Commented:
Java code
                 merge = new Merge();
                String[] fieldNames = new String[4];
                String[][] fieldValues = new String[1][4];
                String[] values = new String[4];
                fieldNames[0] = "Header1";
                fieldNames[1] = "Header2";
                fieldNames[2] = "Header3";
                fieldNames[3] = "Header4";
               
                values[0] = "Row1Value1";
                values[1]= "Row1Value2";
                values[2]= "Row1Value3";
                values[3]= "Row1Value4";
             
                fieldValues[0][0]=values[0];
                fieldValues[0][1]=values[1];
                fieldValues[0][2]=values[2];
                fieldValues[0][3]=values[3];
 
                mergeData.setFieldNames(fieldNames);
           mergeData.setFieldValues(fieldValues);
           merge.setMergeData(mergeData);
         po.setMerge(merge);
   
  //Create Job    
  Job job = new Job();

 
 
WSDL (Section)
 

<xs:element minOccurs="0" name="MergeData" nillable="true" type="tns:MergeData" />
<xs:element minOccurs="0" name="MergeFile" nillable="true" type="tns:MergeFile" />
</xs:sequence>
</xs:complexType>
<xs:element name="Merge" nillable="true" type="tns:Merge" />
+ <xs:complexType name="MergeData">
- <xs:sequence>
<xs:element minOccurs="0" name="FieldNames" nillable="true" type="q1:ArrayOfstring" xmlns:q1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" />
<xs:element minOccurs="0" name="FieldValues" nillable="true" type="q2:ArrayOfArrayOfstring" xmlns:q2="http://schemas.microsoft.com/2003/10/Serialization/Arrays" />  
 
 
When submitted the Field Names "go through" the Merge Data is empty
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>mergeData

is not shown in your code - where do you initialize it?
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>values[0] = "Row1Value1";

In the above, btw, 'values' is redundant. You can do


fieldValues[0][0] = "Row1Value1"; //etc

Open in new window

Author

Commented:
Merge merge = new Merge();
     MergeData mergeData = new MergeData();
 
Below is the auto generated Java code
 

/**
 * MergeData.java
 *
 * This file was auto-generated from WSDL
 * by the Apache Axis 1.4 Apr 22, 2006 (06:55:48 PDT) WSDL2Java emitter.
 */
package org.datacontract.schemas._2004._07.Rimage_Web_Service;
public class MergeData  implements java.io.Serializable {
    private java.lang.String[] fieldNames;
    private java.lang.String[][] fieldValues;
    public MergeData() {
    }
    public MergeData(
           java.lang.String[] fieldNames,
           java.lang.String[][] fieldValues) {
           this.fieldNames = fieldNames;
           this.fieldValues = fieldValues;
    }

    /**
     * Gets the fieldNames value for this MergeData.
     *
     * @return fieldNames
     */
    public java.lang.String[] getFieldNames() {
        return fieldNames;
    }

    /**
     * Sets the fieldNames value for this MergeData.
     *
     * @param fieldNames
     */
    public void setFieldNames(java.lang.String[] fieldNames) {
        this.fieldNames = fieldNames;
    }

    /**
     * Gets the fieldValues value for this MergeData.
     *
     * @return fieldValues
     */
    public java.lang.String[][] getFieldValues() {
        return fieldValues;
    }

    /**
     * Sets the fieldValues value for this MergeData.
     *
     * @param fieldValues
     */
    public void setFieldValues(java.lang.String[][] fieldValues) {
        this.fieldValues = fieldValues;
    }
    private java.lang.Object __equalsCalc = null;
    public synchronized boolean equals(java.lang.Object obj) {
        if (!(obj instanceof MergeData)) return false;
        MergeData other = (MergeData) obj;
        if (obj == null) return false;
        if (this == obj) return true;
        if (__equalsCalc != null) {
            return (__equalsCalc == obj);
        }
        __equalsCalc = obj;
        boolean _equals;
        _equals = true && 
            ((this.fieldNames==null && other.getFieldNames()==null) ||
             (this.fieldNames!=null &&
              java.util.Arrays.equals(this.fieldNames, other.getFieldNames()))) &&
            ((this.fieldValues==null && other.getFieldValues()==null) ||
             (this.fieldValues!=null &&
              java.util.Arrays.equals(this.fieldValues, other.getFieldValues())));
        __equalsCalc = null;
        return _equals;
    }
    private boolean __hashCodeCalc = false;
    public synchronized int hashCode() {
        if (__hashCodeCalc) {
            return 0;
        }
        __hashCodeCalc = true;
        int _hashCode = 1;
        if (getFieldNames() != null) {
            for (int i=0;
                 i<java.lang.reflect.Array.getLength(getFieldNames());
                 i++) {
                java.lang.Object obj = java.lang.reflect.Array.get(getFieldNames(), i);
                if (obj != null &&
                    !obj.getClass().isArray()) {
                    _hashCode += obj.hashCode();
                }
            }
        }
        if (getFieldValues() != null) {
            for (int i=0;
                 i<java.lang.reflect.Array.getLength(getFieldValues());
                 i++) {
                java.lang.Object obj = java.lang.reflect.Array.get(getFieldValues(), i);
                if (obj != null &&
                    !obj.getClass().isArray()) {
                    _hashCode += obj.hashCode();
                }
            }
        }
        __hashCodeCalc = false;
        return _hashCode;
    }
    // Type metadata
    private static org.apache.axis.description.TypeDesc typeDesc =
        new org.apache.axis.description.TypeDesc(MergeData.class, true);
    static {
        typeDesc.setXmlType(new javax.xml.namespace.QName("http://schemas.datacontract.org/2004/07/Rimage.Web.Service", "MergeData"));
        org.apache.axis.description.ElementDesc elemField = new org.apache.axis.description.ElementDesc();
        elemField.setFieldName("fieldNames");
        elemField.setXmlName(new javax.xml.namespace.QName("http://schemas.datacontract.org/2004/07/Rimage.Web.Service", "FieldNames"));
        elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"));
        elemField.setMinOccurs(0);
        elemField.setNillable(true);
        elemField.setItemQName(new javax.xml.namespace.QName("http://schemas.microsoft.com/2003/10/Serialization/Arrays", "string"));
        typeDesc.addFieldDesc(elemField);
        elemField = new org.apache.axis.description.ElementDesc();
        elemField.setFieldName("fieldValues");
        elemField.setXmlName(new javax.xml.namespace.QName("http://schemas.datacontract.org/2004/07/Rimage.Web.Service", "FieldValues"));
        elemField.setXmlType(new javax.xml.namespace.QName("http://schemas.microsoft.com/2003/10/Serialization/Arrays", "ArrayOfstring"));
        elemField.setMinOccurs(0);
        elemField.setNillable(true);
        elemField.setItemQName(new javax.xml.namespace.QName("http://schemas.microsoft.com/2003/10/Serialization/Arrays", "ArrayOfstring"));
        typeDesc.addFieldDesc(elemField);
    }
    /**
     * Return type metadata object
     */
    public static org.apache.axis.description.TypeDesc getTypeDesc() {
        return typeDesc;
    }
    /**
     * Get Custom Serializer
     */
    public static org.apache.axis.encoding.Serializer getSerializer(
           java.lang.String mechType,
           java.lang.Class _javaType,  
           javax.xml.namespace.QName _xmlType) {
        return
          new  org.apache.axis.encoding.ser.BeanSerializer(
            _javaType, _xmlType, typeDesc);
    }
    /**
     * Get Custom Deserializer
     */
    public static org.apache.axis.encoding.Deserializer getDeserializer(
           java.lang.String mechType,
           java.lang.Class _javaType,  
           javax.xml.namespace.QName _xmlType) {
        return
          new  org.apache.axis.encoding.ser.BeanDeserializer(
            _javaType, _xmlType, typeDesc);
    }
}
 
 
 
CERTIFIED EXPERT
Top Expert 2016

Commented:
You may find, as is most the case where serialization is  involved, only the no-arg ctor gets called

Author

Commented:
I am calling the no arg constructors.
CERTIFIED EXPERT
Top Expert 2016

Commented:
Can you post the actual error message you're getting?

Author

Commented:
there is not an error generated, the problem is the resulting soap message has a "" instead of the values from the array

Author

Commented:
Ok,
I have more information attached is a trace dump one from .net that works and the java that does not.
 
 
Java:

<ns1:FieldNames>
<ns2:string xmlns:ns2="http://schemas.microsoft.com/2003/10/Serialization/Arrays">Header1</ns2:string>
<ns3:string xmlns:ns3="http://schemas.microsoft.com/2003/10/Serialization/Arrays">Header2</ns3:string>
<ns4:string xmlns:ns4="http://schemas.microsoft.com/2003/10/Serialization/Arrays">Header3</ns4:string>
<ns5:string xmlns:ns5="http://schemas.microsoft.com/2003/10/Serialization/Arrays">Header4</ns5:string>
</ns1:FieldNames>
<ns1:FieldValues>
<ns6:ArrayOfstring xmlns:ns6="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<ns6:ArrayOfstring>Row1Value1</ns6:ArrayOfstring>
<ns6:ArrayOfstring>Row1Value2</ns6:ArrayOfstring>
<ns6:ArrayOfstring>Row1Value3</ns6:ArrayOfstring>
<ns6:ArrayOfstring>Row1Value4</ns6:ArrayOfstring>
</ns6:ArrayOfstring>
 
 
.net

<a:MergeData>
<a:FieldNames xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<b:string>Header1</b:string>
<b:string>Header2</b:string>
<b:string>Header3</b:string>
<b:string>Header4</b:string>
</a:FieldNames>
<a:FieldValues xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<b:ArrayOfstring>
<b:string>Row1Value1</b:string>
<b:string>Row1Value2</b:string>
<b:string>Row1Value3</b:string>
<b:string>Row1Value4</b:string>
</b:ArrayOfstring>
</a:FieldValues>
</a:MergeData>
 
CERTIFIED EXPERT
Top Expert 2016

Commented:
MergeData has not been deserialized properly. Probably for the reasons i already gave you
CERTIFIED EXPERT
Top Expert 2016

Commented:
Sorry - ignore that last comment

Author

Commented:
Soap end point class

qName = new javax.xml.namespace.QName("http://schemas.microsoft.com/2003/10/Serialization/Arrays", "ArrayOfArrayOfstring");
cachedSerQNames.add(qName);
cls = java.lang.String[][].class;
cachedSerClasses.add(cls);
qName = new javax.xml.namespace.QName("http://schemas.microsoft.com/2003/10/Serialization/Arrays", "ArrayOfstring");
qName2 = new javax.xml.namespace.QName("http://schemas.microsoft.com/2003/10/Serialization/Arrays", "ArrayOfstring");
cachedSerFactories.add(new org.apache.axis.encoding.ser.ArraySerializerFactory(qName, qName2));
cachedDeserFactories.add(new org.apache.axis.encoding.ser.ArrayDeserializerFactory());
qName = new javax.xml.namespace.QName("http://schemas.microsoft.com/2003/10/Serialization/Arrays", "ArrayOfstring");
cachedSerQNames.add(qName);
cls = java.lang.String[].class;
cachedSerClasses.add(cls);
qName = new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string");
qName2 = new javax.xml.namespace.QName("http://schemas.microsoft.com/2003/10/Serialization/Arrays", "string");
cachedSerFactories.add(new org.apache.axis.encoding.ser.ArraySerializerFactory(qName, qName2));
cachedDeserFactories.add(new org.apache.axis.encoding.ser.ArrayDeserializerFactory());
 
Mergedata class

typeDesc.setXmlType(new javax.xml.namespace.QName("http://schemas.datacontract.org/2004/07/test.Web.Service", "MergeData"));
org.apache.axis.description.ElementDesc elemField = new org.apache.axis.description.ElementDesc();
elemField.setFieldName("fieldNames");
elemField.setXmlName(new javax.xml.namespace.QName("http://schemas.datacontract.org/2004/07/test.Web.Service", "FieldNames"));
elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"));
elemField.setMinOccurs(0);
elemField.setNillable(true);
elemField.setItemQName(new javax.xml.namespace.QName("http://schemas.microsoft.com/2003/10/Serialization/Arrays", "string"));
typeDesc.addFieldDesc(elemField);
elemField = new org.apache.axis.description.ElementDesc();
elemField.setFieldName("fieldValues");
elemField.setXmlName(new javax.xml.namespace.QName("http://schemas.datacontract.org/2004/07/test.Web.Service", "FieldValues"));
elemField.setXmlType(new javax.xml.namespace.QName("http://schemas.microsoft.com/2003/10/Serialization/Arrays", "string")); --Chaged this
elemField.setMinOccurs(0);
elemField.setNillable(true);
elemField.setItemQName(new javax.xml.namespace.QName("http://schemas.microsoft.com/2003/10/Serialization/Arrays", "ArrayOfstring"));
typeDesc.addFieldDesc(elemField);
}
 

I modified the serilizer above and got the following

<ns1:MergeData>
<ns1:FieldNames>
<ns2:string xmlns:ns2="http://schemas.microsoft.com/2003/10/Serialization/Arrays">Header1</ns2:string>
<ns3:string xmlns:ns3="http://schemas.microsoft.com/2003/10/Serialization/Arrays">Header2</ns3:string>
<ns4:string xmlns:ns4="http://schemas.microsoft.com/2003/10/Serialization/Arrays">Header3</ns4:string>
<ns5:string xmlns:ns5="http://schemas.microsoft.com/2003/10/Serialization/Arrays">Header4</ns5:string>
</ns1:FieldNames>
<ns1:FieldValues xsi:type="ns6:ArrayOfArrayOfstring" xmlns:ns6="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<ns6:ArrayOfstring xsi:type="ns6:ArrayOfstring">
<ns6:ArrayOfstring xsi:type="xsd:string">Row1Value1</ns6:ArrayOfstring>
<ns6:ArrayOfstring xsi:type="xsd:string">Row1Value2</ns6:ArrayOfstring>
<ns6:ArrayOfstring xsi:type="xsd:string">Row1Value3</ns6:ArrayOfstring>
<ns6:ArrayOfstring xsi:type="xsd:string">Row1Value4</ns6:ArrayOfstring>
</ns6:ArrayOfstring>
</ns1:FieldValues>
</ns1:MergeData>
</ns1:Merge>
</ns1:ProductionOptions>
<ns1:Type>ImageAndRecord</ns1:Type>
</ns1:Job>
</request>
</SubmitJob>
</soapenv:Body>
 
 

Author

Commented:
Ok I have done some more research, the problem is apache axis does not support multi-dimensional arrays which explains why it is not serializing properly. Does anyone know of a sample custom serializer for multidimesional arrays.
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.