开发者

Catching errors or exceptions

开发者 https://www.devze.com 2022-12-10 14:43 出处:网络
I built a mashup of google maps and weather.com and everytimeone of these server is not responding my application hangs up too.What do you think I can do to prevent or minimize hanging up of my web ap

I built a mashup of google maps and weather.com and everytime one of these server is not responding my application hangs up too.What do you think I can do to prevent or minimize hanging up of my web apps?Hanging up like you can't navigate away from that page.... I got this code on my app code to access the weather service;


Public Class WeatherIn
    Private _path As String
    Private _cachedFile As String
     Public Sub New(ByVal path As String)
        _path = path
        _cachedFile = String.Format("{0}\WeatherInCache.xml", _path)
    End Sub

Public Function GetWeather(ByVal arg As String) As String

    Return _getWebWeather(arg)

End Function

Private Function _getCachedWeather() As String
    Dim str As String = String.Empty


    Using reader As New StreamReader(_cachedFile)
        str = reader.ReadToEnd()
    End Using


    Return str
End Function

Private Function _getWebWeather(ByVal arg As String) As String

    Dim baseUrl As String = "http://xoap.weather.com/weather/local/{0}?cc=*&dayf=5&link=xoap&prod=xoap&par={1}&key={2}"
    Dim jane As String = arg
    Dim james As String = "api key"
    Dim john As String = "another api key"

    Dim url As String = String.Format(baseUrl, jane, james, john)


    Using client As New WebClient()

        Try

            Dim xml As New XmlTextReader(client.OpenRead(url))


            Dim xslt As New XslCompiledTransform()
            xslt.Load(_path + "/Pathto.xslt")


            Using writer As New StreamWriter(_cachedFile)
                xslt.Transform(xml, Nothing, writer)
            End Using


            Return _getCachedWeather()
        Catch exception As WebException

            Dim xmlStr As String = "<errorDoc>"
            xmlStr += "<alert>An Error Occurred!</alert>"
            xmlStr += [String].Format("<message>{0}</message>", exception.Message)
            xmlStr += "</errorDoc>"


            Dim doc As New XmlDocument()
            doc.LoadXml(xmlStr)


            Dim reader As New XmlNodeReader(doc)

            Dim xslt As New XslCompiledTransform()
            xslt.Load(_path + "/Pathto.xslt")


            Dim resultDocument As New XmlDocument()
            Using writer As XmlWriter = resultDocument.CreateNavigator().AppendChild()
                xslt.Transform(reader, DirectCast(Nothing, XsltArgumentList), writer)
            End Using

            Return resultDocument.OuterXml
        End Try
    End Using
End Function

Then I used the class above on my page where I display the weather like this:



'specific zip code or could be retrieved from querystring for dynamic retrieval

var jay="94576" 

Dim weather As New WeatherIn(Server.MapPath(String.Empty))
                Dim weatherData As String = weather.GetWeather(jay)


                Response.ContentType = "text/xml"
                Response.CacheControl = "no-cache"

                Response.Write(weatherData)

which I retrieve the data and write on the page through javascript.Most of the time its the weather.com that goes down.I got no problem with googl开发者_如何学JAVAe map its reliable....anybody got a solution why my page hangs up too if the remote server is not responding?The mashup is running smoothly if the remote server is responding...


When depending on external web services it is best to load asynchronously so that if one of them is slow you can show some kind of loading spinner to the page viewer. If it fails your page could simply report that reading from the web server failed and to try again later.

In this case I would load up the page with the Google map in place and then make an AJAX request for the Weather data.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号