Sub Process_Globals
Dim MainUrl As String
MainUrl = "http://img222.imagevenue.com/gallery/loc683/336_1254_cars.php"
Dim HttpClient1 As HttpClient
Dim MainRequestId As Int
MainRequestId = 100
End Sub
Sub Globals
Dim btnConnect As Button
Dim ImageView1 As ImageView
Dim ImageView2 As ImageView
Dim ImageView3 As ImageView
Dim ImageView4 As ImageView
Dim ImageView5 As ImageView
Dim ImageView6 As ImageView
Dim ImageView7 As ImageView
Dim ImageView8 As ImageView
Dim ImageView9 As ImageView
Dim ivs() As ImageView
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
HttpClient1.Initialize("http")
End If
Activity.LoadLayout("1")
ivs = Array As ImageView(ImageView1, ImageView2, ImageView3, ImageView4, _
ImageView5, ImageView6, ImageView7, ImageView8, ImageView9)
ResetImagesBackground
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub ResetImagesBackground
For i = 0 To Activity.NumberOfViews - 1
If Activity.GetView(i) Is ImageView Then Activity.GetView(i).Color = Colors.Black
Next
'We could have used the image views array (ivs) instead of going over all the views...
End Sub
Sub btnConnect_Click
Dim request As HttpRequest
request.InitializeGet(MainUrl)
HttpClient1.Execute(request, MainRequestId)
ProgressDialogShow("Fetching data...")
End Sub
Sub Http_ResponseSuccess (Response As HttpResponse, TaskId As Int)
Log(TaskId & ": success")
If TaskId = MainRequestId Then 'Fetch the main page
Response.GetAsynchronously("MainResponse", _
File.OpenOutput(File.DirInternalCache, "page.html", False), True, MainRequestId)
Else 'One of the images requests has arrived, write the image to a file in the background
Response.GetAsynchronously("ImageResponse", _
File.OpenOutput(File.DirInternalCache, "image" & TaskId, False), True, TaskId)
End If
End Sub
Sub Http_ResponseError (Reason As String, StatusCode As Int, TaskId As Int)
ToastMessageShow("Error. TaskId: " & TaskId & ", Reason: " & Reason & ", StatusCode: " & StatusCode, True)
ProgressDialogHide
End Sub
'
Sub ImageResponse_StreamFinish (Success As Boolean, TaskId As Int)
If Success = False Then
Msgbox(LastException.Message, "Error")
Return
End If
Dim bd As BitmapDrawable 'load the image
bd.Initialize(LoadBitmap(File.DirInternalCache, "image" & TaskId))
ivs(TaskId).Background = bd 'set the image to an ImageView
End Sub
Sub MainResponse_StreamFinish (Success As Boolean, TaskId As Int)
ResetImagesBackground
HandleMainPage (File.OpenInput(File.DirInternalCache, "page.html"))
End Sub
'Parse the main page and find the 9 image links
Sub HandleMainPage (in As InputStream)
start = DateTime.Now
Dim TextReader1 As TextReader
TextReader1.Initialize(in)
Dim pattern, class As String
class = "morecars"
pattern = "img src=\q([^q]+)\q".Replace("q", QUOTE)
Dim links As List
links.Initialize
Dim line As String
line = TextReader1.ReadLine
Do While line <> Null
If line.IndexOf(class) > -1 Then
Dim link As String
Dim m As Matcher
m = Regex.Matcher(pattern, line)
If m.Find Then
links.Add(m.Group(1)) 'add the image link
End If
End If
line = TextReader1.ReadLine
Loop
TextReader1.Close
Log("done parsing main page: " & (DateTime.Now - start))
For i = 0 To links.Size - 1 'send request for each image
Dim request As HttpRequest
request.InitializeGet(links.Get(i))
HttpClient1.Execute(request, i)
Next
ProgressDialogHide
End Sub
'Show the second activity with the chosen image.
Sub img_Click
Dim iv As ImageView
iv = Sender
Dim bd As BitmapDrawable
bd = iv.Background
Activity2.Bitmap1 = bd.Bitmap
StartActivity(Activity2)
End Sub