如何用kotlin android中的responseInputStream.read写while循环 - - while((i = responseInputStream.read(byteContainer))

时间:2017-07-01 05:31:34

标签: android kotlin inputstream

如何在kotlin android

中使用带有responseInputStream.read的while循环

my code for while loop in kotlin android

another one

这里添加了responseInputStream read while循环.kt

                val responseInputStream = conn.inputStream
                val responseStringBuffer = StringBuffer()
                val byteContainer = ByteArray(1024)
                var i: Int
                while ((i = responseInputStream.read(byteContainer)) != -1) {
                    responseStringBuffer.append(String(byteContainer, 0, i))
                }
                Log.w("TAG", "res :" + responseStringBuffer.toString())

1 个答案:

答案 0 :(得分:4)

Kotlin不喜欢java,你不能在单行中编写多个表达式。你应该将单行表达式分解为多行,例如:

while(true){
  val i= responseInputStream.read(byteContainer);

  if(i==-1) break;

  responseStringBuffer.append(String(byteContainer, 0, i))
}