Search code examples
phpandroidxmljsonnusoap

WSDL/Soap web service response complex array parsing using KSOAP2 library


I have created PHP web service using nusoap

$namespace="http:/mynamespace.com/mynamespace"
$server = new soap_server();
$server->debug_flag = false;
$server->configureWSDL("test", $namespace);
$server->wsdl->schemaTargetNamespace = $namespace;


$server->wsdl->addComplexType(
    'Products',
    'complexType',
    'struct',
    'all',
    '',
    array('ID' => array('name' => 'ID','type' => 'xsd:int'),
        'ProductName' => array('name' => 'ProductName','type' => 'xsd:string'),
        'ImageUrl' => array('name' => 'ImageUrl','type' => 'xsd:string')
        )
);

$server->wsdl->addComplexType(
    'ProductsArray',
    'complexType',
    'array',
    '',
    'SOAP-ENC:Array',
    array(),
    array(
    array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:Products[]')
    ),
    'tns:Products'
);
$server->register('GetProductDetails',                    // method name
  array('AgentId' => 'xsd:string'),          // input parameters
  array('return' => 'tns:ProductsArray'),    // output parameters
  $namespace,                         // namespace
  $namespace . '#GetProductDetails',                   // soapaction
  'rpc',                                    // style
  'sequence',                                // use
  'Get Product Details'        // documentation
);

function GetProductDetails($AgentId)
{
    $productArray = array();

    $sqlQry="SELECT pr.products_id, pr.products_image, pd.products_name FROM `products` pr left join products_description pd on pr.products_id=pd.products_id";
    $result=mysql_query($sqlQry);

    while($row=mysql_fetch_array($result)){
        $product=array();
        $product["ID"]=$row['products_id'];
    $product["ProductName"]=$row['products_name'];
    $product["ImageUrl"]=$row['products_image'];
        $productArray[]=$product;
    }
    return $productArray;
}
$HTTP_RAW_POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA'])?$GLOBALS['HTTP_RAW_POST_DATA'] : '';
$server->service($HTTP_RAW_POST_DATA);

and I am getting response in android something like

[Products{ID=29; Name=product1; Url=product1.jpg; }, Products{ID=30; Name=product2; Url=product2.jpg; }]

this responce is in one element of response.getProperty(0) and if I paste this code in http://jsonviewer.stack.hu/ site then it tells me it is not valid json, I am new in nusoap so I don't know how this gives json/XML response Is there any problem with code? I have also tried Android Code:

 SoapObject response = (SoapObject) envelope.bodyIn;
 SoapObject nameResult = (SoapObject) response.getProperty(0);

In above nameResult I am getting all response in one single property.


Solution

  • I have solved it by my self, this is help here if any one needs how to solve this..

    I am getting response is parsed using Vector and after that I just simply make a loop that get every attribute and add to ArrayList<HashMap<String, String>>.

    My process is like, get response from webservice and parsing that response using vector and I just retrieve properties and add to ArrayList<HashMap<String, String>>.

    So If here anyone wants to parse complexType Array Response and finds problem like me, they can find solution like following code

    public static final ArrayList<HashMap<String, String>> productMapArray = new ArrayList<HashMap<String, String>>();
    
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            envelope.dotNet = true;
    
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            request.addProperty("user_id", "1");
       try {
                androidHttpTransport.call(SOAP_ACTION, envelope);
                Vector<SoapObject> response = (Vector<SoapObject>)envelope.getResponse();
                for (SoapObject soapObject : response) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(KEY_ID, soapObject.getProperty(KEY_ID).toString());
                    map.put(KEY_PRODUCT, soapObject.getProperty(KEY_PRODUCT).toString());
                    map.put(KEY_IMG, soapObject.getProperty(KEY_IMG).toString());
                    productMapArray.add(map);
                }  
                if (response.toString().equalsIgnoreCase("invalid")) {
                    result = false;
                } else {
                    result = true;
                }
    
            } catch (SocketException ex) {
                result = false;
                Log.e("Error : ", "Error on soapPrimitiveData() " + ex.getMessage());
                ex.printStackTrace();
            } catch (Exception e) {
                result = false;
                Log.e("Error : ", "Error on soapPrimitiveData() " + e.getMessage());
                e.printStackTrace();
            }
            return result;