2016-07-10 6 views
0

Ich erstelle einen Scala-REST-Client mit Apache HttpClient. Hier ist mein Code.Probleme mit dem REST-Client in Scala

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 
    } 

Das Problem ist, dass Source in io.Source in rot markiert ist. Es sagt Cannot resolve symbol source. Außerdem ist import java.io._ als unbenutzt markiert. Wie kann ich dieses Problem lösen?

Antwort

2

Sie sollten:

  • ersetzen import java.io._ mit import scala.io.Source
  • ersetzen io.Source.fromInputStream(...) mit Source.fromInputStream(...)
2

Quelle Klasse ist nicht in Java-IO-Bibliothek

Es ist in

scala.io.Source 

diese Zeile hinzufügen, und es wird funktionieren

import scala.io.Source