Tuesday, October 22, 2013

Web Scrapping in Android using Jsoup

Web Scrapping basically mean parsing some site and extracting the information from it.
There are many libraries that can be used for web scrapping in android we will be focusing on Jsoup which is one of famous library for this purpose.

First step is to create a simple android application.Then you need to download the library file which is basically .jar file and add that to your project.

This can be done by right clicking your project ->Build Path ->Configure Build Path

Click on Add External Jar and provide the path of the download Jar file



You can download Jsoup Library from this link: http://jsoup.org/download

Next step is to add the parsing code in your activity. There are multiple ways to do that best way is to make Async Task  to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

So we will make Async Task in our Activity class like

private class MyTask extends AsyncTask<Void, Void, String> {

  @Override
  protected String doInBackground(Void... params) {
    String title ="";
    Document doc;
    try {
      doc = Jsoup.connect("http://google.com/").get();
      title = doc.title();
      System.out.print(title);
    } catch (IOException e) {
      e.printStackTrace();
    }
    return title;   
  } 


  @Override
  protected void onPostExecute(String result) {        
    //if you had a ui element, you could display the title
    ((TextView)findViewById (R.id.myTextView)).setText (result);
  }
}

In order to get data from any site or extract data from any site we need to specify site name to connect function of Jsoup

doc = Jsoup.connect("http://google.com/").get();      

Then we will have all document in our doc variable and we can parse it using different techniques. As the purpose of this blog is to tell the basic usage of Jsoup so we will just extract the title of website from the data extracted from google site.

For that purpose we will do

doc.title();                                          

So now we need to show it in activity. In order to do that we have created a textview and we will set its value in onPostExecute function of Async Task .  This function get executed once the task has completed.

That was all will set the title which we extracted from google.com and set its value in TextView which was present in our Activity Layout.

Donot forget to add internet permission in you android manifest file.

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
        <uses-permission android:name="android.permission.INTERNET"></uses-permission>  
 </manifest>  

Sample Code can be downloaded from this link   Download


Friday, September 13, 2013

Resetting IIS remotely using Powershell script

While working on a project I come across a scenario where I wanted to reset the IIS remotely. I searched on internet looking for the best and easiest solution. After spending in some time I found out that powershell script would be best and easiest way to achieve my task.Steps explained below will explain how can we achieve this.

In order to remotely connect we need to make sure firewall is not blocking the port on which we are trying to connect so first thing to do is run the following command on server as well as your machine to get the port open for communication.


 To enable that follow below steps;

1- Open Windows PowerShell command window (Start -> Accessories -> Windows PowerShell)
2- Execute following command


winrm quickconfig
Press y when it asks for confirmation.


$server="IRVDIGWEB22"

invoke-command -computername $server -scriptblock {iisreset} -Credential (Get-Credential)

The code above simply connect to server first and then reset the IIS on it. But for connection we need to have to authenticate that we have rights to the server so for that we need to provide credentials of server.

invoke-command -computername $server -scriptblock {iisreset} -Credential (Get-Credential)

Credential (Get-Credential) here prompts for credentials 
$server is the name of server that has IIS you want to reset.
iisreset is the command you want to perform on remote machine.


I hope that would help you. Sample Script File can be downloaded here