Example Code

From Car and Driving
Jump to: navigation, search

There is a Visual Studio 2010 project which demonstrates how to access the webservice here:

http://www.caranddriving.com/ws/A041/helper-examplecode-june12.zip

Even if you don't use Visual Basic, the code should be simple enough to read and understand how to use and access the webservice.

Here's a useful extract (this assumes you've set up an external reference to the main webservice at http://www.caranddriving.com/ws/A041/ws.asmx, and called it cdws

   Public Sub DisplayReview(ByVal userid As String, ByVal id As String, videowidth As String, videoheight As String, what As String)

        Dim r As cdws.result = New cdws.result
        Try
            r = webs.GetReviewDetailsFromID(userid, id, "false", "")
        Catch ex As Exception
            WriteLine("<div id=errorbox>Webservice call failed: " & ex.Message & "</div>")
            Exit Sub
        End Try

        If r Is Nothing Then
            WriteLine("<div id=errorbox>Webservice error: nothing returned</div>")
            Exit Sub
        Else
            If r.code > 0 Then
                WriteLine("<div id=errorbox>Webservice error: " & r.message & "</div>")
                Exit Sub
            End If
        End If

        If r.reviewlist Is Nothing Then
            ' should never happen
        Else
            Dim dotitle As Boolean = True
            Dim dovideo As Boolean = True
            Dim dophoto As Boolean = True
            Dim dodata As Boolean = True
            Dim dotext As Boolean = True

            If what <> "" Then
                dotitle = InStr(what.ToLower, "title") > 0
                dovideo = InStr(what.ToLower, "video") > 0
                dophoto = InStr(what.ToLower, "photo") > 0
                dodata = InStr(what.ToLower, "data") > 0
                dotext = InStr(what.ToLower, "text") > 0
            End If

            With r.reviewlist(0)
                WriteLine("<div class=review>")

                If dotitle Then
                    Try
                        Select Case .reviewtype
                            Case "new", "new1", "new2"
                                WriteLine("<div class=title>" & .nameofcar & " - NEW CAR REVIEW</div>")
                                WriteLine("<div class=subtitle>" & .title & "</div>")
                            Case "used"
                                WriteLine("<div class=title>" & .nameofcar & " - USED CAR MODEL GUIDE</div>")
                                WriteLine("<div class=subtitle>" & .usedtitle & "</div>")
                            Case "womans"
                                WriteLine("<div class=title>" & .nameofcar & " - WOMAN'S VIEW</div>")
                                WriteLine("<div class=subtitle>" & .familytitle & "</div>")
                        End Select
                    Catch
                        ' Protect against null fields
                    End Try
                End If

                If dovideo Then
                    If Not .videolist Is Nothing Then ' not all reviews have videos
                        Dim v As cdws.video
                        For Each v In .videolist
                            If v.isdefault Then ' just show one video; some reviews have more than one
                                Dim vidurl As String = .videolist(0).video_urlforiframe
                                Dim _tmp As String = ""
                                If videowidth <> "" And videoheight <> "" Then
                                    _tmp = " width=""" & videowidth & """ height=""" & videoheight & """ "
                                Else
                                    videowidth = "510"
                                    _tmp = " width=""510"" height=""326""" ' sensible default
                                End If


                                vidurl = Replace(vidurl, "?w=450&", "?w=" & videowidth & "&") ' replace the default video width with something better...

                                ' HERE'S HOW WE RECOMMEND DOING VIDEOS. USE AN IFRAME TO DISPLAY OUR PLAYER
                                ' you can influence the width of the player itself by changing the w=... querystring on the vidurl to whatever you want. Don't forget to change the width and height of the calling iframe to match though! 
                                WriteLine("<div id=""video_player"">")
                                WriteLine("<iframe id=""videoIframe"" marginwidth=""0"" marginheight=""0"" src=""" & vidurl & """ align=""middle"" frameborder=""0"" scrolling=""no""" & _tmp & "></iframe>")
                                WriteLine("</div>")
                            End If

                        Next
                    Else
                        ' Just give me a photo.
                        WriteLine("<div id=""video_player"">")
                        If Not .photolist Is Nothing Then
                            Dim phs As cdws.photo = .photolist(0)
                            If Not phs.std Is Nothing And phs.std <> "" Then
                                WriteLine("<a href=""" & ToURL(phs.big) & """><img src=""" & ToURL(phs.std) & """></a>")
                            End If
                        End If
                        WriteLine("<p>Sorry, no video is available for this model.</p>")
                        WriteLine("</div>")
                    End If
                End If

                If dotext Then
                    Dim p As cdws.paragraph
                    For Each p In .paragraphlist
                        If p.headline <> "" Then
                            WriteLine("<div class=paragraphhead>")
                            WriteLine(p.headline)
                            WriteLine("</div>")
                        Else
                            WriteLine("<p>")
                        End If
                        WriteLine("<div class=paragraph>")
                        WriteLine(p.text)
                        WriteLine("</div>")
                    Next
                End If


                Dim ph As cdws.photo
                If dophoto And Not .photolist Is Nothing Then
                    For Each ph In .photolist
                        If Not ph.mini Is Nothing And ph.mini <> "" Then
                            WriteLine("<div class=photo>")
                            WriteLine("<a href=""" & ToURL(ph.big) & """><img src=""" & ToURL(ph.mini) & """></a>")
                            WriteLine("</div>")
                        End If
                    Next
                End If

                Dim rd As cdws.rangedata
                If dodata And Not .rangedatalist Is Nothing Then
                    WriteLine("<table class=data>")
                    For Each rd In .rangedatalist
                        If rd.field.ToUpper = "PRICE" Then
                            ' deliberately skip that
                        Else
                            WriteLine("<tr class=data>")
                            WriteLine("<td class=data>")
                            WriteLine(rd.field)
                            If rd.minvalue = rd.maxvalue Then
                                WriteLine("</td><td class=data colspan=2>")
                                WriteLine(rd.minvalue)
                            Else
                                WriteLine("</td><td class=data>")
                                WriteLine(rd.minvalue)
                                If rd.minversion <> "" Then
                                    WriteLine(" (" & rd.minversion & ")")
                                End If
                                WriteLine("</td><td class=data>")
                                WriteLine(rd.maxvalue)
                                If rd.maxversion <> "" Then
                                    WriteLine(" (" & rd.maxversion & ")")
                                End If
                            End If
                            WriteLine("</td>")
                            WriteLine("</tr>")
                        End If
                    Next
                    WriteLine("</table>")
                End If

                WriteLine("</div>")
            End With
        End If
    End Sub
Personal tools