Java 谷歌原BoFF格式错误:输入在一个字段中间意外终止

Java 谷歌原BoFF格式错误:输入在一个字段中间意外终止,java,protocol-buffers,Java,Protocol Buffers,我正在使用解析提要。下面是我遇到的确切错误: com .GoGoL.TyfBuff.ValuePrimuloBuffelExtExc:当解析协议消息时,输入在一个字段的中间意外终止。这可能意味着输入被截断,或者嵌入的消息误报了自己的长度。 首先想到的是,文件的完整性在下载时被破坏了,但我在Android emulator中检查了文件与我在桌面上下载的文件,结果是一样的。以前有人遇到过这个问题吗?我使用的是gfts实时解析器的“版本”。饲料是 编辑---- 以下是我用来下载该文件的代码: ove

我正在使用解析提要。下面是我遇到的确切错误: <代码> com .GoGoL.TyfBuff.ValuePrimuloBuffelExtExc:当解析协议消息时,输入在一个字段的中间意外终止。这可能意味着输入被截断,或者嵌入的消息误报了自己的长度。 首先想到的是,文件的完整性在下载时被破坏了,但我在Android emulator中检查了文件与我在桌面上下载的文件,结果是一样的。以前有人遇到过这个问题吗?我使用的是gfts实时解析器的“版本”。饲料是

编辑----

以下是我用来下载该文件的代码:

override fun doInBackground(vararg urls: URL): Boolean? {
        val httpClient = OkHttpClient()
        val call = httpClient.newCall(Request.Builder().url(urls[0]).get().build())
        val urlStr = urls[0].toString()
        val fileName = urlStr.substring(urlStr.lastIndexOf('/') + 1, urlStr.length)
        val file = File(context.filesDir, fileName)
        try {
            val response = call.execute()
            if (response.code() === 200) {
                var inputStream: InputStream? = null
                val outputStream = FileOutputStream(file)
                try {
                    inputStream = response.body().byteStream()
                    val buff = ByteArray(1024 * 4)
                    var downloaded: Long = 0
                    val target = response.body().contentLength()
                    publishProgress(0L, target)
                    while (true) {
                        val readed = inputStream!!.read(buff)
                        if (readed == -1) {
                            break
                        }
                        outputStream.write(buff, 0, readed)
                        //write buff
                        downloaded += readed.toLong()
                        publishProgress(downloaded, target)
                        if (isCancelled()) {
                            fileListener(null)
                            return false
                        }
                    }
                    fileListener(file)
                    return downloaded == target
                } catch (ignore: IOException) {
                    fileListener(null)
                    return false
                } finally {
                    if (inputStream != null) {
                        inputStream!!.close()
                    }
                    outputStream.flush()
                    outputStream.close()
                }
            } else {
                fileListener(null)
                return false
            }
        } catch (e: Exception) {
            e.printStackTrace()
            fileListener(null)
            return false
        }
    }
编辑2-- 下面是完整的代码路径

override fun onHandleIntent(intent: Intent?) {
        Realm.init(applicationContext)
        val config = RealmConfiguration.Builder().deleteRealmIfMigrationNeeded().build() // TODO remove before deploy
        RealmInstace = Realm.getInstance(config)

        Timber.i("Realm file path: ", RealmInstace?.path)
        val action = intent?.action
        when (action) {
            ACTION_UPDATE_GFTS -> updateGfts()
            ACTION_UPDATE_GFTS_REALTIME -> updateGftsReal() // this is called
            REALM_GET_ALL_TRAIN_MAP -> getTrainMapData(intent)
        }
        exportDatabase()
//        RealmInstace?.close()
        RealmInstace = null
    }

private fun updateGftsReal() {
        var file:File?=null
        AsyncDownloader(this, null, { f ->
            file = f
            if(file!=null && file!!.exists())
                GftsRealRealmParser(applicationContext, file!!, RealmInstace!!)
            else
                Timber.e("Failed to download trip updates", GFTS_REALTIME_TRIP_URL)
        }).execute(URL(GFTS_REALTIME_TRIP_URL)).get(2, TimeUnit.MINUTES)
    }

class GftsRealRealmParser(val context: Context, val file: File, val realm: Realm) {
    init {
        var feedr: FeedMessageR? = null
        var inputStream = BufferedInputStream(FileInputStream(file))
        try {
            val url = URL("http://developer.mbta.com/lib/GTRTFS/Alerts/TripUpdates.pb")
            val feed = GtfsRealtime.FeedMessage.parseDelimitedFrom(url.openStream())
            feedr = FeedMessageR(feed)
            realm.executeTransaction {
                Timber.i("Committing ${feedr!!.entityList.size} to the realtime db.")
                realm?.copyToRealm(feedr)
//                val train = realm.where(TripDescriptorR::class.java).contains("route_id", "31760754").findFirst()
            }
        } catch (e: IOException) {
            Timber.e(e.message, e)
            e.printStackTrace()
        } catch(e: Exception) {
            Timber.e(e.message, e)
            e.printStackTrace()
        }
        if (feedr == null)
            throw ParseException("Failed to parse file", 0)
    }
}
编辑4----


见下面的答案

我找到了解决方案。我打错电话了。我正在调用
parsedelimited from
我应该在那里调用
parseFrom

该错误表示数据已损坏。然而,该文件(我下载为227332字节)似乎是一个有效的protobuf--
protoc
工具对其进行了很好的解码。你能展示你的代码吗?也许它有一个bug。你也能展示你的protobuf反序列化代码吗?下载代码看起来没问题。如果您找到了解决方案,请不要编辑您的问题,将其添加为答案。