这个问题已经在这里有了答案: > Best way to translate this java code into kotlin 3个
如何在Kotlin Android中使用while循环与responseInputStream.read
这里添加了while循环.kt时读取的responseInputStream
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())
解决方法:
Kotlin不喜欢Java,您不能在一行中组成多重表达式.您应该将单行表达式分成多行,例如:
while(true){
val i= responseInputStream.read(byteContainer);
if(i==-1) break;
responseStringBuffer.append(String(byteContainer, 0, i))
}