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

A M*_*aja 6 android inputstream kotlin

如何在 kotlin android 中使用带有 responseInputStream.read 的 while 循环

我在 kotlin android 中的 while 循环代码

另一个

这里添加了 responseInputStream read while loop .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())
Run Code Online (Sandbox Code Playgroud)

hol*_*ava 7

Kotlin 不喜欢 java,你不能在一行中组成多表达式。您应该将单行表达式分解为多行,例如:

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

  if(i==-1) break;

  responseStringBuffer.append(String(byteContainer, 0, i))
}
Run Code Online (Sandbox Code Playgroud)