Monthly Archives: August 2015

Android Cordova 4.0.2 Webview Intercept Requests

Recently I was working on Android cordova app where I was using cordova 4.0.2 version. In this project I have to intercept urls loaded in webview and do some stuff.

In earlier cordova version we can do following.

myWebView.setWebViewClient(new WebViewClient() {
         public boolean shouldOverrideUrlLoading(WebView view, String url)  {
         }
});

But in newer version of cordova this will not work. So what to do? Here is this blog I am going to explain it.

First of all lets get webView

myWebView = (WebView) this.appView.getView();
Then add WebViewClient to webView.
myWebView.setWebViewClient(new SystemWebViewClient((SystemWebViewEngine) this.appView.getEngine()){
          public boolean shouldOverrideUrlLoading(WebView view, String url) {
          }
          public void onPageFinished(WebView view, String url) {
                   super.onPageFinished(view, url);
          }
});
As you can see in cordova newer version they have added new class SystemWebViewClient in cordova lib that we have to use for accessing functions like shouldOverrideUrlLoading and onPageFinished.
Hope this helps you. 

PHP Parse SOAP XML Response

Recently in one of my project we were using SOAP service which was having XML response that we have to parse with PHP. For some reason PHP SOAP client was giving some because it XML response was using name namespaces and that was not parsed correctly with SOAP client. Here in this blog I am going to explain how to call XML web service without using SOAP client and parse response with PHP.

We will use PHP cURL. First lets create XML post data.

$postString = ‘<?xml version=”1.0″ encoding=”utf-8″?>
<soap:Envelope xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”                 xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/”>
  <soap:Body>
<GetProducts xmlns=”http://ws.sourcingcity.co.uk/”>
  <query>pen</query>
  <username>username</username>
  <password>password</password>
  <pageindex>1</pageindex>
  <pagesize>20</pagesize>
</GetProducts>
  </soap:Body>
</soap:Envelope>’;

So that’s our post data. Now lets create cURL request.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ‘https://ws.sourcingcity.co.uk/ws3dpromo.asmx?op=GetProducts’);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                                            ‘Content-Type: text/xml; charset=utf-8’,
                                            ‘Connection: Keep-Alive’,
                                            ‘SOAPAction: http://ws.sourcingcity.co.uk/GetProducts’
                                            ));    
$result = curl_exec($ch);
curl_close($ch);

Now you will get XML response in $result which is in string format. We will use SimpleXML class of PHP.

$soap = simplexml_load_string($result);

That will load SimpleXML object. Now we will register namespace.

$soap->registerXPathNamespace(‘ns1’, ‘http://ws.sourcingcity.co.uk/’);

Now you can use XPATH to go to specific nodes.

$productsResult = $soap->xpath(‘//ns1:GetProductsResponse/ns1:GetProductsResult’)[0]->Products;

for($i=0;$iProduct);$i++){
$product = $productsResult->Product[$i];
echo “Product “.$i;
echo “Name : “.$product->Name;
echo “SCRef : “.$product->SCRef;
echo “Description : “.$product->Description;
echo “PriceRange : “.$product->PriceRange;
echo “ProductType : “.$product->ProductType;
echo “ProductGroups : “.$product->ProductGroups;
echo “Colours : “.$product->Colours;
echo “ThumbImageURL : “.$product->ThumbImageURL;
echo “LargeImageURL : “.$product->LargeImageURL;
}

That’s it. In the same way you can parse any other XML response.