Programming/Non programming

Friday, February 19, 2010

How to retrieve HTTP URL data programmatically on Android:

How to retrieve HTTP URL data programmatically on Android:

Here is the simple function:

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;


private String getURLRequestData ()
{
String contentData=null;

try {

URL url1 = new URL("http://www.xxxx.com/xxxx.html");
URLConnection con = url1.openConnection();

InputStream is = con.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);

ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ( (current = bis.read())!=-1 )
{
baf.append((byte)current);
}
contentData = new String(baf.toByteArray());

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

return contentData; // you will get output code get from the URL given..
}

After getting this, you can look up, how to parse the data to have with your requirements.

Thanks you.

Cheers !

M.P.Prabakar
Senior Systems Analyst.

No comments:

Post a Comment