How to Check PageRank and AlexaRank in Android

If you have implemented a Java program on desktop, why not bring it to mobile platform? In this tutorial, I will show how to transplant the Java program for PageRank and AlexaRank to Android platform. Let’s glance at the screenshot in advance.

Android Rank Checker

Android Project

Create a new Android project in Eclipse. To access Internet, you have to open AndroidManifest.xml and add

<uses-permission android:name="android.permission.INTERNET"/>

Create an UI layout, which includes TextView, EditText and Button.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/linearlayout"
        android:layout_width="match_parent"
        android:layout_height="60dp" >

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/url" />

        <EditText
            android:id="@+id/url"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:ellipsize="end"
            android:hint="@string/hint" 
            android:inputType="textUri"/>

        <Button
            android:id="@+id/btSearch"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:text="@string/check" />
    </LinearLayout>

    <TextView
        android:id="@+id/results"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearlayout"
        android:scrollbars="vertical"
        android:text="" />

</RelativeLayout>

In main Activity, we need to create an event listener for button and set check results in text view. In case of blocking UI, we should create a thread for network connection.

public void onClick(View v) {
		// TODO Auto-generated method stub
		String url = mURL.getText().toString();
		String tmpURL = url.trim();
		if (url != null && !url.equals("")) {
			if (!url.startsWith("http")) {
				tmpURL = "http://" + url;
			}
			final String finalURL = tmpURL;
			final String logURL = url;
			mProgressDialog = ProgressDialog.show(this,
					getString(R.string.process_dialog_title),
					getString(R.string.process_dialog_content));
			new Thread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					final int pageRank = PageRank.get(finalURL);
					final int alexRank = AlexaRank.getAlexaRank(finalURL);
					runOnUiThread(new Runnable() {

						@Override
						public void run() {
							// TODO Auto-generated method stub
							mResults.append(logURL + ": PageRank = " + pageRank
									+ "; Alexa rank = " + alexRank + mNewline);
							mURL.setText("");
							mProgressDialog.dismiss();
						}

					});

				}

			}).start();
		}
	}

Run the application on your smartphone to have fun. Download the source code AndroidRankChecker.

Now, no matter where you are, you can instantly check PageRank and AlexaRank. Please feel free to contact me at {desmond at Dynamsoft dot com} if you have any questions.

How to Check Alexa Rank in Java

Since we have known how to check PageRank in Java, let’s strengthen our tool by adding the functionality of checking Alexa rank today.

To get the Alexa rank, you can use following code or read the excellent tutorial.

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class AlexaRank {

	public static int getAlexaRank(String domain) {

		int result = 0;

		String url = "http://data.alexa.com/data?cli=10&url=" + domain;

		try {

			URLConnection conn = new URL(url).openConnection();
			InputStream is = conn.getInputStream();

			DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			Document doc = dBuilder.parse(is);

			Element element = doc.getDocumentElement();

			NodeList nodeList = element.getElementsByTagName("POPULARITY");
			if (nodeList.getLength() > 0) {
				Element elementAttribute = (Element) nodeList.item(0);
				String ranking = elementAttribute.getAttribute("TEXT");
				if(!"".equals(ranking)){
					result = Integer.valueOf(ranking);
				}
			}

		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		return result;
	}
}

Now, let’s update our Java code to write Alexa rank to Excel files.

public void getPageRankAndAlexaRank() {
		// TODO Auto-generated method stub		
		try {
			InputStream excelFile = new FileInputStream(mFileName);
			XSSFWorkbook wb = new XSSFWorkbook(excelFile);
			XSSFSheet sheet = wb.getSheetAt(0);
			XSSFRow row;
			XSSFCell cellPR, cellAR;

			Iterator<Row> rows = sheet.rowIterator();

			int col = 0, colPR = 1, colAR = 2;
			int pageRank = 0, alexaRank = 0;
			String url = null;
			String log = null;

			while (rows.hasNext()) {
				row = (XSSFRow) rows.next();
				url = row.getCell(col).getStringCellValue();
				if (url.matches(Utils.REGEX)) { // check whether URL is valid
					System.out.println(url);
					pageRank = PageRank.get(url); // check page rank
					alexaRank = AlexaRank.getAlexaRank(url); // get alexa rank

					// write PageRank to excel
					cellPR = row.createCell(colPR);
					cellPR.setCellValue(pageRank);

					cellAR = row.createCell(colAR);
					cellAR.setCellValue(alexaRank);

					log = "PR = " + pageRank + ", AR = " + alexaRank;
					if (mEventListener != null) {
						mEventListener.log(log);
					}
					System.out.println(log);
				}
				else {
					System.out.println("URL not valid");
				}

				System.out.println("--------------------------");
			}

			FileOutputStream out = new FileOutputStream(mFileName);
	        wb.write(out);
	        out.flush();
	        out.close();
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

Download the updated source package here. Please feel free to contact me at {desmond at dynamsoft dot com} if you have any question.

How to Read and Write Excel Files in Java

Previously, I talked about how to check PageRank in Java. In this article, I will combine PageRank with Excel files. To operate Excel files in Java, I used Apache POI – the Java API for Microsoft Documents. Apparently, you can download the API package, and follow the relevant tutorials to learn how to use POI. When I first downloaded the Java library, I was astonished by how many jar files there were. I had no idea which ones would be useful for my project. Fortunately, I managed to filter out what I want.

Read more

How to Write a Simple Java Program to Check PageRank

In this article, I am not going to go in-depth about Java and Swing. The content I will share in this article is about how to combine slices of knowledge to make a simple java tool for checking PageRank. In my previous article, I talked about how to extract domain names from the urls that downloaded from Google Webmaster. Today, I will not only introduce a way to check the PR but also how to load an Excel file in Java to automatically test the batch of page PRs.

Read more

How to Extract Domain Names from URLs in VBA

Backlink is one of the most important factors for search engine ranking. Thus, the analysis of backlinks is significant for website owners. It is known that Google Webmaster tools can record search traffic allowing users to check the links to their sites. We can download the relevant data as figure 1.

webmaster download

Figure 1

However, it is imperfect that the data only consists of links and dates (figure 2), which are not categorized or visualized. In my point of view, the worst part is that there is no data of domain names, which are of a higher value than the backlinks. Therefore, if we want to see the domain names, we have to process the data ourselves. Today, I would like to share my knowledge about how to extract domain names from URLs in VBA.

webmaster links

Figure 2

Why Programmatically Processing the Excel Data?

Some may think that it is not necessary to deal with the data in VBA. That to filter the domain names, the basic built-in functions are enough. It is true that to get the domain names, we just need to type in “=LEFT(text, FIND(“/”,text,[start_num]))”. However, this approach is not perfect. It cannot process sub domain names. For example, after executing excel functions, we can get both “a.test.com” and “b.test.com” whereas the target domain name should be “test.com”. How can we solve this complicated question? The answer is to write a program in VBA.

How to Extract the Domain Names?

To solve the problem, we can consider the following steps:

  • Create a data source of domains. E.g. “.com, .net, .org, .gov …”
    dict = CreateObject("Scripting.Dictionary") ' use dictionary to store key & value
    ' initialize the data source of domains
    dict.Add("com", "com")
    dict.Add("cn", "cn")
    dict.Add("biz", "biz")
    dict.Add("org", "org")
    dict.Add("net", "net")
    dict.Add("edu", "edu")
    dict.Add("gov", "gov")
    dict.Add("co", "co")
    dict.Add("us", "us")
    dict.Add("ca", "ca")
    dict.Add("info", "info")
    dict.Add("eu", "eu")
    dict.Add("de", "de")
  • Split “://” and “/” to get the domain names. E.g. “http://social.msdn.microsoft.com/a/b/c/d.html” -> “social.msdn.microsoft.com”
Dim urlArray() As String
Dim domainArrayOri() As String
Dim sourceRow, sourceCol, destinationRow, destinationCol As Integer
sourceRow = 2 ' index of source row
sourceCol = 1 ' index of source column
destinationRow = 2 ' index of destination row
destinationCol = 7 ' index of destination column

'------------------------------------------------------------------
urlArray = Split(Cells(sourceRow, sourceCol).Value, "://")
domainArrayOri = Split(urlArray(1), "/")
  • Split “.” to store strings in array. E.g. “social.msdn.microsoft.com” -> “social”, “msdn”, “microsoft”, “com”
domainArray = Split(domainArrayOri(0), ".")
  • Traverse the array data to find out the domain that matches the data source of domains. E.g. the domain is “com”
' function for checking domain
Function isDomain(tmp As String, domains() As String) As Boolean
    isDomain = False
    For Each domain In domains
        If tmp = domain Then
            isDomain = True
            Exit For
        End If
    Next domain

End Function
        lastIndex = UBound(domainArray, 1)
        firstIndex = LBound(domainArray, 1)
        count = lastIndex - firstIndex + 1
        If count > 2 Then
            Dim j As Integer
            Dim bIsDomain As Boolean
            bIsDomain = False
            If lastIndex > 5 Then
                lastIndex = 3
            End If

            For j = 2 To lastIndex
                If dict.Exists(domainArray(j)) Then
                    ' TODO:
                Else
                    ' TODO:
                End If
            Next j
        Else
            Cells(destinationRow, destinationCol).Value = domainArrayOri(0)
        End If
  • Compose the domain names with the adjacent string “microsoft”. E.g. the final domain name is “Microsoft.com”
                If dict.Exists(domainArray(j)) Then
                    bIsDomain = True
                    Cells(destinationRow, destinationCol).Value = domainArray(j - 1) & "." & domainArray(j)
                Else
                    Cells(destinationRow, destinationCol).Value = domainArrayOri(0)
                End If

You can feel free to download the sample code.