Here's an example of fetching data from a Web page.
In this example, only the data is obtained from the Web page, but no processing is done, only the data is saved to a TXT file.
This example is written in the Android project.
Package com.example.creepertest;
Import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.io.OutputStream;
Import Java.io.OutputStreamWriter;
Import java.net.HttpURLConnection;
Import java.net.MalformedURLException;
Import Java.net.URL;
public class Controller
{
public static final String Sd_lottery_url =
"http://www.sdticai.com/zjhfx/cpmain.asp?cptype=115";
public static final String Sd_lottery_file_path = "/data/data/com.example.creepertest/test.txt";
Private BufferedWriter mbufferedwriter = null;
Private BufferedReader mbufferedreader = null;
Public Controller () {
Runnable Runnable = new Runnable () {
@Override
public void Run () {
Capturehtml ();
}
};
Thread thread = new Thread (runnable);
Thread.Start ();
}
private void capturehtml () {
try{
URL sdlotterurl = new URL (sd_lottery_url);
HttpURLConnection Httpconn =
(httpurlconnection) sdlotterurl.openconnection ();
InputStreamReader InputStreamReader =
New InputStreamReader (Httpconn.getinputstream (), "utf-8");
Mbufferedreader = new BufferedReader (InputStreamReader);
OutputStream OutputStream =
New FileOutputStream (sd_lottery_file_path,true);
OutputStreamWriter outputstreamwriter = new OutputStreamWriter (outputstream);
Mbufferedwriter = new BufferedWriter (outputstreamwriter);
String linestr = null;
while (true) {
Linestr = Mbufferedreader.readline ();
if (linestr! = null) {
Mbufferedwriter.write (LINESTR);
Mbufferedwriter.newline ();
Mbufferedwriter.flush ();
}
Else
Break
}
}
catch (Malformedurlexception e) {
E.printstacktrace ();
}
catch (IOException e) {
E.printstacktrace ();
}
finally{
try {
if (mbufferedwriter! = null)
Mbufferedwriter.close ();
if (Mbufferedreader! = null)
Mbufferedreader.close ();
} catch (Exception Exception) {
Exception.printstacktrace ();
}
}
}
}