如何在kotlin android中用responseInputStream.read编写while循环–while((i = responseInputStream.read(byteContain

这个问题已经在这里有了答案:            >            Best way to translate this java code into kotlin                                    3个
如何在Kotlin Android中使用while循环与responseInputStream.read

如何在kotlin android中用responseInputStream.read编写while循环–while((i = responseInputStream.read(byteContain

如何在kotlin android中用responseInputStream.read编写while循环–while((i = responseInputStream.read(byteContain

这里添加了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))
}
上一篇:android-客户端如何保持等待可能的服务器消息?


下一篇:多次从具有大量数据的Java InputStream中读取