Diese Anwendung verwendet diese HTML-Seite: Flickr: Explore interesting photos from the last 7 days in FlickrLand... , analysiert sie und findet die 9 Bilder-Links.
Sie sendet dann eine Anfrage (request) für jedes Bild. Die heruntergeladenen Bilder werden dann in ImageViews angezeigt.
Jedes mal, wenn Sie auf den Connect Button klicken werden 9 neue Bilder angezeigt. Wenn man auf ein Bild klickt wird es vergrößert in einer neuen Activity angezeigt. (beachten Sie daß nicht das volle Bild heruntergeladen wird sondern nur der Thumbnail in seiner Originalgrösse).
Wenn Sie dieses Programm starten sehen Sie dass Anfragen (requests) im Hintergrund verarbeitet werden und dass die Bilder angezeigt werden sobald sie fertig sind.
Die TaskId spielt hier eine wichtige Rolle. Jede Bild Anfrage (request)wird mit einem TaskId zwischen 0 bis 8 gesandt. Dieser TaskId wird später verwendet, um die richtige ImageView vom ImageViews Array zu erhalten.
Der komplette Code befindet sich am Ende.
Das Englische Originaltutorial.
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
Das Programm:
Last edited: