square / wire

gRPC and protocol buffers for Android, Kotlin, Swift and Java.
https://square.github.io/wire/
Apache License 2.0
4.24k stars 570 forks source link

Android Client Base64 in Android 7 #3127

Open Xiao1209 opened 7 hours ago

Xiao1209 commented 7 hours ago

App in Android7 or Android7.1.1 happened this Exception

java.lang.NoClassDefFoundError: Failed resolution of: Ljava/util/Base64; (Ask Gemini)
    at com.squareup.wire.internal.GrpcKt.grpcResponseToException(grpc.kt:219)
    at com.squareup.wire.internal.RealGrpcCall.readExactlyOneAndClose(RealGrpcCall.kt:112)
    at com.squareup.wire.internal.RealGrpcCall.access$readExactlyOneAndClose(RealGrpcCall.kt:32)
    at com.squareup.wire.internal.RealGrpcCall$execute$2$2.onResponse(RealGrpcCall.kt:70)
    at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:519)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
    at java.lang.Thread.run(Thread.java:761)

Then I fond this code in grpc.kt

internal fun GrpcResponse.grpcResponseToException(suppressed: IOException? = null): IOException? {
  grpcStatus?.toIntOrNull()?.takeIf { it != 0 }?.let { grpcStatusInt ->
    (trailers["grpc-status-details-bin"] ?: header("grpc-status-details-bin"))?.let {
      try {
        grpcStatusDetailsBin = Base64.getDecoder().decode(it)
      } catch (e: IllegalArgumentException) {
        throw IOException(
          "gRPC transport failure, invalid grpc-status-details-bin" +
            " (HTTP status=$code, grpc-status=$grpcStatus, grpc-message=$grpcMessage)",
          e,
        )
      }
    }

    return GrpcException(GrpcStatus.get(grpcStatusInt), grpcMessage, grpcStatusDetailsBin)
  }
  return null // Success.
}

How Can I update this code?Please

Xiao1209 commented 6 hours ago

Sorry,The code layout is not easy to identify.I update at here

/** Returns an exception if the gRPC call didn't have a grpc-status of 0. */
internal fun GrpcResponse.grpcResponseToException(suppressed: IOException? = null): IOException? {
  var trailers = headersOf()
  var transportException = suppressed
  try {
    trailers = trailers()
  } catch (e: IOException) {
    if (transportException == null) transportException = e
  }

  val grpcStatus = trailers["grpc-status"] ?: header("grpc-status")
  val grpcMessage = trailers["grpc-message"] ?: header("grpc-message")
  var grpcStatusDetailsBin: ByteArray? = null

  grpcStatus?.toIntOrNull()?.takeIf { it != 0 }?.let { grpcStatusInt ->
    (trailers["grpc-status-details-bin"] ?: header("grpc-status-details-bin"))?.let {
      try {
        grpcStatusDetailsBin = Base64.getDecoder().decode(it)
        //TODO this code.How Can I update?
      } catch (e: IllegalArgumentException) {
        throw IOException(
          "gRPC transport failure, invalid grpc-status-details-bin" +
            " (HTTP status=$code, grpc-status=$grpcStatus, grpc-message=$grpcMessage)",
          e,
        )
      }
    }

    return GrpcException(GrpcStatus.get(grpcStatusInt), grpcMessage, grpcStatusDetailsBin)
  }

  if (transportException != null || grpcStatus?.toIntOrNull() == null) {
    return IOException(
      "gRPC transport failure" +
        " (HTTP status=$code, grpc-status=$grpcStatus, grpc-message=$grpcMessage)",
      transportException,
    )
  }

  return null // Success.
}