【发布时间】:2020-02-14 05:52:18
【问题描述】:
我正在尝试在 Android 中解析 gtfs 实时交通提要。
我制作了一个准系统 Android 应用程序,它使用基于此处显示的示例代码的 Java GtfsRealtime 绑定库:https://github.com/MobilityData/gtfs-realtime-bindings/tree/final-google-version/java。我要解析的提要是 GRT tripupdates 提要:http://webapps.regionofwaterloo.ca/api/grt-routes/api/tripupdates。但是,这样做时我得到一个错误:
W/System.err: com.google.protobuf.InvalidProtocolBufferException: Message missing required fields: header
我已尝试使用此问题底部显示的 nodejs 代码解析提要,似乎 GRT tripupdate 提要实际上包含标题!
如果我将 Android 应用程序中的 GRT 提要替换为 Thunderbay 提要 (http://api.nextlift.ca/gtfs-realtime/tripupdates.pb),Android 中的 gtfs 库能够很好地解析提要,并正确打印标题中包含的信息。所以,我知道tripupdate protobuf 文件可以使用该库进行解析。
此外,通过使用http://transitfeeds.com/p/thunder-bay-transit/431/source 和http://transitfeeds.com/p/grand-river-transit/624/source 的外部查看器查看两个不同来源中包含的数据,可以发现两个提要具有相似的结构化标题,具有相同的 gtfs_realtime_version。
如果能在 Android 中解析 GRT 提要,我将不胜感激。我不确定为什么 protobuf 认为 GRT 提要中没有标头。
安卓代码:
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 feed 的输出,使用 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 } } }
Nodejs 中 GRT 提要的输出
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 } } }
【问题讨论】:
标签: android node.js protocol-buffers gtfs