ToolsActivity.java (Main activity )
Package ....
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ToolsActivity extends Activity {
/** Called when the activity is first created. */
private List<String> RankData = new ArrayList<String>();
private List<String> AlexaData = new ArrayList<String>();
private List<String> CompeteData = new ArrayList<String>();
TextView tv_PageRnk, tv_PageAlx, tv_CompeteRnk;
EditText et_url;
Button check;
String UrlValue;
private AdView adView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
tv_PageAlx = (TextView) findViewById(R.id.tv_PageAlexa);
tv_PageRnk = (TextView) findViewById(R.id.tv_PageRank);
tv_CompeteRnk = (TextView) findViewById(R.id.tv_CompeteRank);
et_url = (EditText) findViewById(R.id.editText1);
check = (Button) findViewById(R.id.button1);
// UrlValue = et_url.getText().toString();
Log.d("URL value : ", String.valueOf(UrlValue));
check.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
ParseXml();
et_url.setText("");
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_options_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.about:
startActivity(new Intent(this, About.class));
return true;
case R.id.help:
startActivity(new Intent(this, Help.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void ParseXml() {
try {
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ToolsActivity extends Activity {
/** Called when the activity is first created. */
private List<String> RankData = new ArrayList<String>();
private List<String> AlexaData = new ArrayList<String>();
private List<String> CompeteData = new ArrayList<String>();
TextView tv_PageRnk, tv_PageAlx, tv_CompeteRnk;
EditText et_url;
Button check;
String UrlValue;
private AdView adView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
tv_PageAlx = (TextView) findViewById(R.id.tv_PageAlexa);
tv_PageRnk = (TextView) findViewById(R.id.tv_PageRank);
tv_CompeteRnk = (TextView) findViewById(R.id.tv_CompeteRank);
et_url = (EditText) findViewById(R.id.editText1);
check = (Button) findViewById(R.id.button1);
// UrlValue = et_url.getText().toString();
Log.d("URL value : ", String.valueOf(UrlValue));
check.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
ParseXml();
et_url.setText("");
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_options_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.about:
startActivity(new Intent(this, About.class));
return true;
case R.id.help:
startActivity(new Intent(this, Help.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void ParseXml() {
try {
// DONT USE THIS LINK IT WILL NO WORK USE YOUR OWN LINK
URL rssUrl = new URL("http://els.in/EMS/rank.php?url="
+ et_url.getText().toString());
SAXParserFactory mySAXParserFactory = SAXParserFactory
.newInstance();
SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
XMLReader myXMLReader = mySAXParser.getXMLReader();
RSSHandler myRSSHandler = new RSSHandler();
myXMLReader.setContentHandler(myRSSHandler);
InputSource myInputSource = new InputSource(rssUrl.openStream());
myXMLReader.parse(myInputSource);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
tv_PageRnk.setText(String.valueOf(RankData).replace("[", "")
.replace("]", ""));
Log.d("Parsing", "Page Rank : " + String.valueOf(RankData));
// tv_PageAlx.setText(String.valueOf(AlexaData));
tv_PageAlx.setText(String.valueOf(AlexaData).replace("[", "")
.replace("]", ""));
Log.d("Parsing", "Page Alexa : " + String.valueOf(AlexaData));
tv_CompeteRnk.setText(String.valueOf(CompeteData).replace("[", "")
.replace("]", ""));
Log.d("Parsing", "Compete Rank : " + String.valueOf(CompeteData));
RankData.clear();
AlexaData.clear();
CompeteData.clear();
}
private class RSSHandler extends DefaultHandler {
final int stateUnknown = 0;
final int statesRankview = 1;
final int statesAlexaview = 2;
final int statesCompeteview = 3;
int state = stateUnknown;
@Override
public void startDocument() throws SAXException {
}
@Override
public void endDocument() throws SAXException {
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (localName.equalsIgnoreCase("pagerank"))
state = statesRankview;
else if (localName.equalsIgnoreCase("alexa"))
state = statesAlexaview;
else if (localName.equalsIgnoreCase("compete"))
state = statesCompeteview;
else
state = stateUnknown;
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
state = stateUnknown;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
String strCharacters = new String(ch, start, length);
if (state == statesRankview)
RankData.add(strCharacters);
else if (state == statesAlexaview)
AlexaData.add(strCharacters);
else if (state == statesCompeteview)
CompeteData.add(strCharacters);
}
}
}
URL rssUrl = new URL("http://els.in/EMS/rank.php?url="
+ et_url.getText().toString());
SAXParserFactory mySAXParserFactory = SAXParserFactory
.newInstance();
SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
XMLReader myXMLReader = mySAXParser.getXMLReader();
RSSHandler myRSSHandler = new RSSHandler();
myXMLReader.setContentHandler(myRSSHandler);
InputSource myInputSource = new InputSource(rssUrl.openStream());
myXMLReader.parse(myInputSource);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
tv_PageRnk.setText(String.valueOf(RankData).replace("[", "")
.replace("]", ""));
Log.d("Parsing", "Page Rank : " + String.valueOf(RankData));
// tv_PageAlx.setText(String.valueOf(AlexaData));
tv_PageAlx.setText(String.valueOf(AlexaData).replace("[", "")
.replace("]", ""));
Log.d("Parsing", "Page Alexa : " + String.valueOf(AlexaData));
tv_CompeteRnk.setText(String.valueOf(CompeteData).replace("[", "")
.replace("]", ""));
Log.d("Parsing", "Compete Rank : " + String.valueOf(CompeteData));
RankData.clear();
AlexaData.clear();
CompeteData.clear();
}
private class RSSHandler extends DefaultHandler {
final int stateUnknown = 0;
final int statesRankview = 1;
final int statesAlexaview = 2;
final int statesCompeteview = 3;
int state = stateUnknown;
@Override
public void startDocument() throws SAXException {
}
@Override
public void endDocument() throws SAXException {
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (localName.equalsIgnoreCase("pagerank"))
state = statesRankview;
else if (localName.equalsIgnoreCase("alexa"))
state = statesAlexaview;
else if (localName.equalsIgnoreCase("compete"))
state = statesCompeteview;
else
state = stateUnknown;
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
state = stateUnknown;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
String strCharacters = new String(ch, start, length);
if (state == statesRankview)
RankData.add(strCharacters);
else if (state == statesAlexaview)
AlexaData.add(strCharacters);
else if (state == statesCompeteview)
CompeteData.add(strCharacters);
}
}
}
main.xml file
Put views according to requirement
No comments:
Post a Comment