Skip to content

Instantly share code, notes, and snippets.

@brantYan
Created August 24, 2011 03:22
Show Gist options
  • Save brantYan/1167243 to your computer and use it in GitHub Desktop.
Save brantYan/1167243 to your computer and use it in GitHub Desktop.
the abstract class can not be write in List[T]
I'm reading the IBM developer scala guide
http://www.ibm.com/developerworks/cn/java/j-scala05059.html
and write the weatherReport copy form example
WeatherReport.scala
import org.apache.http.impl.client.DefaultHttpClient
import org.apache.http.HttpResponse
import org.apache.http.util.EntityUtils
object WeatherTest {
def main(args: Array[String]) = {
new WeatherTest().callTwitterTest
}
}
class WeatherTest {
import org.apache.http.client._, methods._
def callTwitterTest = {
val testURL = "http://www.google.com/ig/api?weather=Beijing"
// HttpClient API 4
val client: HttpClient = new DefaultHttpClient()
val method: HttpUriRequest = new HttpGet(testURL)
val response: HttpResponse = client.execute(method)
val responseString: String = EntityUtils.toString(response.getEntity)
val c = xml.XML.loadString(responseString)
val forecastConditionsList = new List[forecastConditions]
for (n <- (c \\ "forecast_conditions").elements)
forecastConditionsList += (forecastConditions.fromXml(n))
forecastConditionsList.forall(x -> println(x.toString()))
}
}
WeatherType.scala
abstract class forecastConditions {
val dayOfWeek: String
val lowTemperature: String
val highTemperature: String
val iconUrl: String
val condition: String
}
object forecastConditions {
/*
<forecast_conditions>
<day_of_week data="Fri"/>
<low data="64"/>
<high data="82"/>
<icon data="/ig/images/weather/mostly_sunny.gif"/>
<condition data="Mostly Sunny"/>
</forecast_conditions>
*/
def formXml(node: scala.xml.Node): forecastConditions = {
new forecastConditions {
val dayOfWeek = (node \\ "day_of_week").text
val lowTemperature = (node \\ "low").text
val highTemperature = (node \\ "high").text
val iconUrl = (node \\ "icon").text
val condition = (node \\ "condition").text
override def toString():String = {
dayOfWeek+" "+ lowTemperature+" "+highTemperature+" "+iconUrl+" "+ condition
}
}
}
}
the compile show
error: class List is abstract; cannot be instantiated
val forecastConditionsList =new List[forecastConditions]
@brantYan
Copy link
Author

val a=<a>
<forecast_conditions>
    <day_of_week data="Wed"></day_of_week>
</forecast_conditions>
<forecast_conditions>
    <day_of_week data="Thu"></day_of_week>
</forecast_conditions>
<forecast_conditions>
    <day_of_week data="Fri"></day_of_week>
</forecast_conditions>
<forecast_conditions>
    <day_of_week data="Sat"></day_of_week>
</forecast_conditions>
<a>
val forecast= a\\ "forcast_conditions"
接下来怎么对forecast 中每一个取出来啊。。
我看forecast.elements方法被标注为过时了

@jackywyz
Copy link

我建议你看看program in scala先。

val forecastConditionsList:List[forecastConditions] = List.empty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment