【问题标题】:How to write while loop with responseInputStream.read in kotlin android - - while ((i = responseInputStream.read(byteContainer)) [duplicate]如何在kotlin android中使用responseInputStream.read编写while循环--while((i = responseInputStream.read(byteContainer))[重复]
【发布时间】:2017-07-01 05:31:33
【问题描述】:

如何在 kotlin android 中使用带有 responseInputStream.read 的 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())

【问题讨论】:

标签: android kotlin inputstream


【解决方案1】:

Kotlin 不像 java,你不能在一行中编写多表达式。您应该将单行表达式分成多行,例如:

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

  if(i==-1) break;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-26
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    • 2014-11-28
    • 2013-07-09
    • 1970-01-01
    相关资源
    最近更新 更多