This application gets this html page: Flickr: Explore interesting photos from the last 7 days in FlickrLand... , parses it and finds the 9 image links.
It then sends a request for each of the images. Downloaded images are then displayed in the ImageViews.
Each time you press on the Connect button 9 new images appear.
Pressing on an image shows it larger in a second activity (note that the full image is not downloaded so the second activity just shows the thumbnail with its original size).
If you run this program you can see that all requests are handled in the background and that images appear whenever they are ready.
The TaskId plays an important role here. Each image request is sent with a TaskId between 0 to 8. This TaskId is later used to get the correct ImageView from the ImageViews array.
B4X:
Sub Process_Globals
Dim MainUrl As String
MainUrl = "http://www.flickr.com/explore/interesting/7days/"
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 = "<td class=" & QUOTE & "Photo" & QUOTE & ">"
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
A new version of FlickrViewer is available based on HttpUtils: http://www.b4x.com/forum/basic4andr...ndroid-web-services-now-simple.html#post50919
The new code is simpler and more robust.