Powered By Blogger

Monday 12 March 2012

Networking Concepts


package com.networkingex;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class NetworkingEx extends Activity {
    /** Called when the activity is first created. */
ImageView img;
   
private InputStream openHttpConnection(String urlstring)
throws IOException
{
InputStream in=null;
int response=-1;
URL url=new URL(urlstring);
URLConnection con=url.openConnection();
if(!(con instanceof URLConnection))
throw new IOException("NOT AN HTTP CONNECTION");
try{
HttpURLConnection httpcon=(HttpURLConnection)con;
httpcon.setAllowUserInteraction(false);
httpcon.setInstanceFollowRedirects(true);
httpcon.setRequestMethod("GET");
httpcon.connect();
response=httpcon.getResponseCode();
if(response==HttpURLConnection.HTTP_OK)
{
in=httpcon.getInputStream();
}

}catch(Exception e)
{
e.printStackTrace();
}

return in;

}
private Bitmap downloadImage(String url)
{
Bitmap bitmap=null;
InputStream in=null;
try
{
//in=openHttpConnection(url);
bitmap=BitmapFactory.decodeStream(openHttpConnection(url));
//in.close();
}catch(IOException e)
{
Toast.makeText(this, e.getLocalizedMessage(),Toast.LENGTH_LONG).show();
e.printStackTrace();

}
return bitmap;

}
private String downloadfile(String url)
{
int buffersize=200;
InputStream in=null;
try
{
in=openHttpConnection(url);
}catch(IOException e)
{
Toast.makeText(this,e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
InputStreamReader inr=new InputStreamReader(in);
int charread =0;
String str="";
char[] inputbuffer=new char[buffersize];
try
{
while((charread==inr.read(inputbuffer)))
{
String readline=String.copyValueOf(inputbuffer, 0,charread);
str+=readline;
inputbuffer=new char[buffersize];
}
in.close();
}catch(IOException e)
{
Toast.makeText(this,e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
return url;

}
private String worddefinition(String word)
{
InputStream in=null;
String strDefinition="";
try
{
in=openHttpConnection("http://services.aonaware.com/DictService/DictService.asmx/Define?word="+word);
   Document doc=null;
   DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
   DocumentBuilder db;
   try
   {
    db=dbf.newDocumentBuilder();
    doc=db.parse(in);
   
   }catch(ParserConfigurationException e)
   {
    e.printStackTrace();
   }
   catch(Exception e)
   {
    e.printStackTrace();
   }
   doc.getDocumentElement().normalize();
   NodeList definitionElements=doc.getElementsByTagName("Definition");
 
 
  // NodeList definitionElements = null;
for(int i=0;i<definitionElements.getLength();i++)
   {

System.out.println("Lenght of Defintion:"+definitionElements.getLength());
Node item=definitionElements.item(1);
if(item.getNodeType()==Node.ELEMENT_NODE)
{
NodeList nodeList=item.getChildNodes();
strDefinition=nodeList.item(5).getTextContent();
/*//Element defElement=(Element)item;
NodeList wordDefinition=doc.getElementsByTagName("WordDefinition");
strDefinition="";
for(int j=0;j<wordDefinition.getLength();j++)
{
Element wordDefinitionElement =
(Element) wordDefinition.item(j);
NodeList textNodes =
((Node) wordDefinitionElement).getChildNodes();
strDefinition +=
((Node) textNodes.item(0)).getNodeValue() + ".";
}*/
/*NodeList nodeList=item.getChildNodes();
System.out.println("Lenght of Defintion:"+nodeList.getLength());

//System.out.println("Name::::::::::::::::2"+nodeList.item(2).getNodeName());
//System.out.println("Name:::::::::::::::3:"+nodeList.item(3).getNodeName());
//System.out.println("Name::::::::::::::::4"+nodeList.item(4).getNodeName());

Toast.makeText(getApplicationContext(),"Meaning is"+nodeList.item(5).getTextContent() , Toast.LENGTH_LONG).show();
//System.out.println("Name::::::::::::::::5"+nodeList.item(5).getTextContent());
*/
}
//return strDefinition;


   // NodeList itemnode= definitionElements.item(1);
   // System.out.println("Lenght of Defintion:"+itemnode.getLength());
    /*System.out.println("title:ln"+itemnode.getNodeName());
    System.out.println("title:nn"+itemnode.getNodeName());
    System.out.println("title:nv"+itemnode.getNodeValue());
    System.out.println("title:tc"+itemnode.getTextContent());*/
    /*if(itemnode.getNodeType()==Node.ELEMENT_NODE)
    {
    Log.d("mg","value is"+strDefinition);
    try
    {
    Element definitionelement=(Element)itemnode;
    }catch(Exception e)
    {
    Toast.makeText(getApplicationContext(), e.getLocalizedMessage(),Toast.LENGTH_LONG).show();
    }
   
    for(int j=0;j<wordDefinitionElements.getLength();j++)
    {
    Element wordelement=(Element)wordDefinitionElements.item(j);
    NodeList textnode=((Node)wordelement).getChildNodes();
    if(itemnode.getNodeName().equals("WordDefinition"))
    {
    strDefinition=itemnode.getNodeName();
    Log.d("msg is", "value is"+strDefinition);
    }
    //}
    Toast.makeText(getApplicationContext(),strDefinition, Toast.LENGTH_LONG).show();
    }*/



   
}
 



}catch(Exception e)
{
//Toast.makeText(getApplicationContext(),e.getLocalizedMessage(),Toast.LENGTH_LONG).show();
e.printStackTrace();
}
return strDefinition;
}
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Bitmap bitmap = null;
        try
        {
         bitmap=downloadImage("http://cars.88000.org/wallpapers/80/Porsche_911_Police_Car.jpg");
        }catch(Exception e)
        {
        e.printStackTrace();
        }
       
       
        img=(ImageView)findViewById(R.id.imageView1);
        img.setImageBitmap(bitmap);
        /*String str="";
        try
        {
         str=downloadfile("http://appleinsider.com.feedsportal.com/c/33975/f/616168/index.rss");
        }catch(Exception e)
        {
        Toast.makeText(getApplicationContext(),str, 10000).show();
        e.printStackTrace();
        }
        try
        {
        String s=worddefinition("dry");
        TextView tv=new TextView(this);
        tv.setText(s);
        }catch(Exception e)
        {
        Toast.makeText(getApplicationContext(), e.getLocalizedMessage(),Toast.LENGTH_LONG).show();
        e.printStackTrace();
        }*/
    }
}



main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/icon">
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<Button android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" android:background="@drawable/icon"></Button>
<ImageView android:id="@+id/imageView1"
android:layout_gravity="center"
android:layout_height="match_parent" android:layout_width="match_parent"></ImageView>
</LinearLayout>


No comments:

Post a Comment