Scala调用url获取返回值

来源:互联网 发布:ios网络编程 编辑:程序博客网 时间:2024/06/05 17:39

在Scala编程中,常常会用到调用第三方接口,获取返回值(文件内容是字符类型,或者返回的是字符串),那么使用Scala自带的Scala.io.Source类,将非常方便。

举例如下:

  1、url="http://localhost:9008/services/user/getSampleValue"

  2、返回值:有两种

       (1)字符串:"{"status":"1","timestamp":"201612131742","sampleList":[{"domain":"www.baidu.cn","proportion":"0.8"},{"domain":"www.sina.com","proportion":"0.4"}]}"

       (2)文件流(内容是字符):sample.txt文件

            内容是:"{"status":"1","timestamp":"201612131742","sampleList":[{"domain":"www.baidu.cn","proportion":"0.8"},{"domain":"www.sina.com","proportion":"0.4"}]}"

实现的代码如下:


参考Scala 的API:

    http://www.scala-lang.org/api/current/scala/io/Source.html

0 0
原创粉丝点击