2016-07-10 38 views
0

我正在使用Apache HttpClient創建一個Scala REST客戶端。 這是我的代碼。Scala中的REST客戶端問題

import java.io._ 
import org.apache.http.client.methods.HttpGet 
import org.apache.http.impl.client.DefaultHttpClient 

val output = getRestContent(myURL) 

    /** 
    * Returns the text content from a REST URL. Returns a blank String if there 
    * is a problem. 
    */ 
    def getRestContent(url:String): String = { 
    val httpClient = new DefaultHttpClient() 
    val httpResponse = httpClient.execute(new HttpGet(url)) 
    val entity = httpResponse.getEntity() 
    var content = "" 
    if (entity != null) { 
     val inputStream = entity.getContent() 
     content = io.Source.fromInputStream(inputStream).getLines.mkString 
     inputStream.close 
    } 
    httpClient.getConnectionManager().shutdown() 
    return content 
    } 

的問題是,Source標記爲紅色的io.Source。它說Cannot resolve symbol source。此外import java.io._被標記爲未使用。如何解決這個問題?

回答

2

您應該:

  • 取代import java.io._import scala.io.Source
  • Source.fromInputStream(...)
2

源類取代io.Source.fromInputStream(...)沒有在Java IO庫

正是在

scala.io.Source 

加入這行,也將努力

import scala.io.Source