Сообщается, что в канале GTFS в реальном времени отсутствует заголовок - PullRequest
2 голосов
/ 14 февраля 2020

Я пытаюсь проанализировать транзитную ленту gtfs-realtime в Android.

Я создал приложение barebones Android, которое использует библиотеку привязок Java GtfsRealtime на основе приведенного здесь примера кода: https://github.com/MobilityData/gtfs-realtime-bindings/tree/final-google-version/java. Канал, который я пытаюсь проанализировать, - это канал с тройными обновлениями GRT: http://webapps.regionofwaterloo.ca/api/grt-routes/api/tripupdates. Тем не менее, после этого я получаю сообщение об ошибке:

W/System.err: com.google.protobuf.InvalidProtocolBufferException: Message missing required fields: header

Я экспериментировал с анализом фида с помощью кода nodejs, показанного внизу этого вопроса, и оказалось, что инфицирование фида трипдата GRT содержит заголовок !

Если я заменю ленту GRT в приложении Android на ленту Thunderbay (http://api.nextlift.ca/gtfs-realtime/tripupdates.pb), библиотека gtfs в Android сможет анализировать ленту только хорошо, и правильно печатает информацию, содержащуюся в заголовке. Итак, я знаю, что файл protobuf tripupdate анализируется с помощью библиотеки.

Кроме того, при взгляде на данные, содержащиеся в 2 разных источниках, с помощью внешнего средства просмотра в http://transitfeeds.com/p/thunder-bay-transit/431/source и http://transitfeeds.com/p/grand-river-transit/624/source показано, что оба канала иметь аналогичный структурированный заголовок с тем же gtfs_realtime_version.

Буду признателен за любую помощь при разборе канала GRT в Android. Я не уверен, почему protobuf считает, что в ленте GRT нет заголовка.

Android код:

package com.myapp.grttracker;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.net.URL;

import com.google.transit.realtime.GtfsRealtime.FeedEntity;
import com.google.transit.realtime.GtfsRealtime.FeedMessage;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new RetrieveFeedTask().execute("");
    }
}

class RetrieveFeedTask extends AsyncTask<String, Void, FeedMessage> {
    private static final String TAG_TASK = "---ASYNC_TASK---";
    private Exception exception;
    private FeedMessage feed;

    protected FeedMessage doInBackground(String... urls) {
        try {
            URL url = new URL("http://webapps.regionofwaterloo.ca/api/grt-routes/api/tripupdates");
            //http://api.nextlift.ca/gtfs-realtime/tripupdates.pb
            feed = FeedMessage.parseFrom(url.openStream());
            System.out.println(feed.getHeader().toString());
            return feed;
        } catch (Exception e) {
            this.exception = e;
            return null;
        }
    }

    protected void onPostExecute(FeedMessage feed) {
        if (this.exception != null) {
            Log.e(TAG_TASK, "Error getting feed");
            this.exception.printStackTrace();
        } else {
            for (FeedEntity entity : feed.getEntityList()) {
                // do stuff
            }
        }
    }
}

build.gradle приложения

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.myapp.grttracker"
        minSdkVersion 26 // 26 to support gtfs
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    //https://search.maven.org/search?q=a:gtfs-realtime-bindings
    implementation group: 'io.mobilitydata.transit', name: 'gtfs-realtime-bindings', version: '0.0.5'
}

index. js

var GtfsRealtimeBindings = require('gtfs-realtime-bindings');
var request = require('request');

var requestSettings = {
  method: 'GET',
 // url: 'http://api.nextlift.ca/gtfs-realtime/tripupdates.pb',  /// Thunderbay
  url: 'http://webapps.regionofwaterloo.ca/api/grt-routes/api/tripupdates', /// GRT
  
  encoding: null
};
request(requestSettings, function (error, response, body) {
  if (!error && response.statusCode == 200) {
    var feed = GtfsRealtimeBindings.transit_realtime.FeedMessage.decode(body);
    console.log(feed)
  }
});

Вывод подачи Thunderbay с использованием nodejs

FeedMessage {
  entity:
   [ FeedEntity { id: '0', tripUpdate: [TripUpdate] }, 
     FeedEntity { id: '1', tripUpdate: [TripUpdate] } ],
  header:
   FeedHeader {
     gtfsRealtimeVersion: '1.0',
     incrementality: 0,
     timestamp: Long { low: 1581568947, high: 0, unsigned: true } } }

Вывод подачи GRT в Nodejs

FeedMessage {
  entity:
   [ FeedEntity { id: '1985737', isDeleted: false, tripUpdate: [TripUpdate] },
     FeedEntity { id: '1985738', isDeleted: false, tripUpdate: [TripUpdate] },
     ... 31 more items ],
  header:
   FeedHeader {
     gtfsRealtimeVersion: '1.0',
     incrementality: 0,
     timestamp: Long { low: 1581568953, high: 0, unsigned: true } } }
...