XML Resources in Android, using XmlResourceParser

Well i’m kinda new to android and i’m learning everything that i ca find about android app development so just yesterday i realised that i havent written any post in my blog lately so why not write about android??? well now i’m writing everything that i learn from no onwards as far as possible. well if i have a time i will do post it for sure.

So today lets start with XML parsing using XmlResourceParser. so i’ve divided it in steps and here it goes:

STEP 1. CREATE A LAYOUT WITH XML
Create an Xml file named sample.xml in a folder named xml(you can name your xml filename and folder name as you like for me its res/xml/sample.xml)

CODE:
=================================================================

<?xml version=”1.0″ encoding=”UTF-8″?>
<school>
    <class1>beginners</class1>
    <class2>beginners+</class2>
    <class9>Young</class9>
    <class12>Adult</class12>
</school>

=================================================================

well the getDepth() or the counting of XML is like this:

 <!– outside –>     0
 <root>                  1
   sometext                 1
     <foobar>         2
     </foobar>        2
 </root>              1
 <!– outside –>     0
=================================
Just dont get confused you will be clear soon.

STEP 2. NOW CREATE A LAYOUT
Now We need to create a Layout where we want to display our output. For this i have just created a header and a TextView.

CODE:
=================================================================

<TextView
        android:id=”@+id/first”
        android:layout_width=”fill_parent”
        android:layout_height=”wrap_content”
        android:text=”@string/hello_world” />

<TextView
android:id=”@+id/second”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:layout_below=”@+id/first” />

=================================================================

STEP 3: Write code in your MainActivity.java file
=================================================================

package com.ashim.xmlparseexample_1;

import java.io.IOException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView displayXmlHere =(TextView)findViewById(R.id.second); //make TextView Reference as displayXmlHere
String storeHere; //make a string reference as storeHere
try{
storeHere=fromAnXml(this); //call a method fromAnXml and store everything returned to storeHere
displayXmlHere.setText(storeHere); //display everything that is in storeHere
}catch(XmlPullParserException e){ //if an Xml pulls some Parser exception
e.printStackTrace();
}catch(IOException e){ //if an IO exception shows up
e.printStackTrace();
}
}
public String fromAnXml(Activity a) throws XmlPullParserException, IOException{
StringBuffer buffer = new StringBuffer(); //buffer as a stringBuffer
Resources res =a.getResources(); //Return a Resources instance for your application’s package.
XmlResourceParser xpp =res.getXml(R.xml.sample); //Return an XmlResourceParser for the given resource ID.
xpp.next(); //Get next parsing event

int EventType = xpp.getEventType(); //returns the type of the current Events
System.out.println(“TOTAL EventType:”+EventType); //This Prints the initial value of EventType. We can see it in the LogCat to know how it works

while(EventType !=XmlPullParser.END_DOCUMENT){//check if the value of EventType is equal or not with The END_DOCUMENT if not enter inside the loop
if(EventType==XmlPullParser.START_DOCUMENT)
{
buffer.append(“START OF AN XMLn”);
}
if(EventType == XmlPullParser.START_TAG){
buffer.append(“nSTART_TAG: “+xpp.getName());
}
if(EventType == XmlPullParser.END_TAG){
buffer.append(“nEND_TAG: “+xpp.getName());
}
if(EventType ==XmlPullParser.TEXT){
buffer.append(“nText: “+xpp.getText());
}
System.out.println(“VALUE OF EventType:”+EventType);
EventType=xpp.next();
}
buffer.append(“nnEND OF AN XML”);
return buffer.toString(); //returns the StringBuffer i.e “buffer” to the above calling method
}

}

=================================================================
So This is IT. just try it once if u face any problem just comment below or ask Google Uncle. 😛

Theory behind torrent downloads

BitTorrent well they are known as  peer to peer file sharing protocol which are used for distributing large amounts of data,these are one of the most common protocols used for transferring large files,and these are estimated that it accounts for roughly 25-60% of all Internet traffic these values varies depending on geographical location.It was designed by a programmer from UNITED STATES name Bram Cohen in April 2001 and released a first implementation on 2 JULY 2001.

This protocol  initially works when a file provider make his file or groups of files available to the network which are known as SEED and the other as PEERS, the work of SEED and PEERS  are such that SEED allows to connect to the file and PEERS helps in downloading the file.Each PEERS who download a part of data makes it available to other PEERS to download. After the file is successfully downloaded by a peer,may continue to make the data available,becoming additional SEED.This distributed nature leads to a viral spreading of file throughout peers.

IF YOU WANT TO CREATE AND PUBLISH TORRENT THEN THIS IS HOW YOU HAVE TO START:

 The peer distributing a data file treats the file as a number of identically sized pieces, usually with byte which sizes a power of 2,and it lies typically between 32 KB  and 4 MB each. These peer creates a hash(A hash function is any well defined procedure or mathematical function that converts a large, possibly variable-sized amount of data into a small datum, usually a single integer that may serve as an index to an array.) for each single piece by using SHA-1(cryptographic hash function) hash function which helps to record it in torrent file.The pieces which are greater thn 512 KB will help in reducing the size of a torrent file for a very lagre payload,but is claimed to reduce the efficiency of the protocol. When another peer later receives a particular piece, the hash of the piece is compared to the recorded hash to test that the piece is error-free.

The correct and exact information contained in the torrent file depends on the version used in the BIT TORRENT protocol.The torrent files has the extension as .torrent. Torrent files have an “announce” section, which specifies the URL of the tracker, and an “info” section, containing (suggested) names for the files, their lengths, the piece length used, and a SHA-1 hash code for each piece, all of which are used by clients to verify the integrity of the data they receive.

For publishing a torrent files we need to have a web site and we must have to register it with atleast one tracker.The tracker keeps the record of the client participating in torrent.

Downloading torrents and sharing files:

Users browse the web to find a torrent of interest, download it, and open it with a BitTorrent client. The client connects to the tracker(s) specified in the torrent file, from which it receives a list of peers currently transferring pieces of the file(s) specified in the torrent. The client connects to those peers to obtain the various pieces. If the swarm contains only the initial seeder, the client connects directly to it and begins to request pieces.

Clients incorporate mechanisms to optimize their download and upload rates; for example they download pieces in a random order to increase the opportunity to exchange data, which is only possible if two peers have different pieces of the file. The effectiveness of this data exchange depends largely on the policies that clients use to determine to whom to send data. Clients may prefer to send data to peers that send data back to them  which encourages fair trading. But strict policies often result in suboptimal situations, such as when newly joined peers are unable to receive any data because they don’t have any pieces yet to trade themselves or when two peers with a good connection between them do not exchange data simply because neither of them takes the initiative. To counter these effects, the official BitTorrent client program uses a mechanism called “optimistic unchoking”, whereby the client reserves a portion of its available bandwidth for sending pieces to random peers (not necessarily known good partners, so called preferred peers) in hopes of discovering even better partners and to ensure that newcomers get a chance to join the swarm.

The community of BitTorrent users frowns upon the practice of disconnecting from the network immediately upon success of a file download, and encourages remaining as another seed for as long as practical, which may be days – especially when there are a lot of downloading peers and when the ratio of seeders to downloading peers is low.

Increase your Internet Speed

Here’s how you increase your Internet Speed by limiting the reserved bandwidth.

1.) log on as Administrator.

2.) start – run – type gpedit.msc

3.) expand “local computer policy”

4.) then expand “administrative templates”

5.) then expand “network branch”

6.) Highlight the “QoS Packet Scheduler”

7.) on right window double click “limit reservable bandwidth”

8.) on setting tab check the “enabled”

9.) change “Bandwidth limit %” to 0

You’re done.It would be a good idea to reboot after this.