使用PATTERN MATCHER解析NMEA数据

时间:2012-09-17 18:18:55

标签: java android parsing pattern-matching nmea

使用模式/匹配器方法让ANDROID的正则表达式变得疯狂。我好几天都没有成功。是的,在寻求直接帮助之前,我一直在寻求帮助。好的,这是交易:我的传入流看起来像这样

UTC时间(HhMmSs):154653
状态(A = OK,V = KO):V
纬度:4428.2011
方向(N / S):N
经度:00440.5161
方向(E / W):W
结节速度:000.5
度数方向:342.8
UTC的日期(DdMmAa):050407
磁变化:
变化(E / W):
模式:A

我现在想要的只是纬度和经度值。

我的代码如下所示:

public class NmeaParser {
/**
 * Tag used for log messages
 */
public String nmeaData;
public String lat; //latitude
public String lon; //longitude

private static final String LOG_TAG = "NPGPS";



public String longitude, latitude;
//find the text pattern
public static final Pattern NMEA_PATTERN = Pattern.
            compile(
                    "\\d{6}" +
                    ":\\s\\D" +
                    "\\w+\\S\\s\\d{4}.\\d+" +
                    ":\\s\\D" +
                    "\\w+\\S\\s\\d{5}.\\d+" +
                    ":\\s\\D" +
                    "\\d{3}.\\d{1}" +
                    "\\d{3}.\\d{1}" +
                    "\\d{6}" +
                    ":\\s\\D" +
                    ":\\s\\D"
                    //".*" //this works as a test
                    );

public String parseNmeaSentence(String gpsSentence) {
        Matcher rmcMatcher = NMEA_PATTERN.matcher(gpsSentence);
        //Log.i(LOG_TAG, "dataNP: "+ rmcMatcher);
        if (rmcMatcher.find()) {
                //Log.d("Matcher", "PATTERN MATCHES!");
            //else
                //Log.d("MATCHER", "PATTERN DOES NOT MATCH!");
            //String time = rmcMatcher.group(1);
          latitude = rmcMatcher.group(3);
          nmeaData = latitude;

        }
            return nmeaData;
  }
}

我不断收到这些错误:

09-17 11:37:36.936: E/AndroidRuntime(8756): FATAL EXCEPTION: main
09-17 11:37:36.936: E/AndroidRuntime(8756): java.lang.NullPointerException
09-17 11:37:36.936: E/AndroidRuntime(8756):     at                 android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
09-17 11:37:36.936: E/AndroidRuntime(8756):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
09-17 11:37:36.936: E/AndroidRuntime(8756):     at android.widget.AbsListView.obtainView(AbsListView.java:2058)
09-17 11:37:36.936: E/AndroidRuntime(8756):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
09-17 11:37:36.936: E/AndroidRuntime(8756):     at android.widget.ListView.onMeasure(ListView.java:1155)
09-17 11:37:36.936: E/AndroidRuntime(8756):     at android.view.View.measure(View.java:12723)
09-17 11:37:36.936: E/AndroidRuntime(8756):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
09-17 11:37:36.936: E/AndroidRuntime(8756):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
09-17 11:37:36.936: E/AndroidRuntime(8756):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
09-17 11:37:36.936: E/AndroidRuntime(8756):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
09-17 11:37:36.936: E/AndroidRuntime(8756):     at android.view.View.measure(View.java:12723)
09-17 11:37:36.936: E/AndroidRuntime(8756):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
09-17 11:37:36.936: E/AndroidRuntime(8756):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
09-17 11:37:36.936: E/AndroidRuntime(8756):     at android.view.View.measure(View.java:12723)
09-17 11:37:36.936: E/AndroidRuntime(8756):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
09-17 11:37:36.936: E/AndroidRuntime(8756):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
09-17 11:37:36.936: E/AndroidRuntime(8756):     at android.view.View.measure(View.java:12723)
09-17 11:37:36.936: E/AndroidRuntime(8756):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
09-17 11:37:36.936: E/AndroidRuntime(8756):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
09-17 11:37:36.936: E/AndroidRuntime(8756):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
09-17 11:37:36.936: E/AndroidRuntime(8756):     at android.view.View.measure(View.java:12723)
09-17 11:37:36.936: E/AndroidRuntime(8756):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
09-17 11:37:36.936: E/AndroidRuntime(8756):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
09-17 11:37:36.936: E/AndroidRuntime(8756):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-17 11:37:36.936: E/AndroidRuntime(8756):     at android.os.Looper.loop(Looper.java:137)
09-17 11:37:36.936: E/AndroidRuntime(8756):     at android.app.ActivityThread.main(ActivityThread.java:4575)
09-17 11:37:36.936: E/AndroidRuntime(8756):     at java.lang.reflect.Method.invokeNative(Native Method)
09-17 11:37:36.936: E/AndroidRuntime(8756):     at java.lang.reflect.Method.invoke(Method.java:511)
09-17 11:37:36.936: E/AndroidRuntime(8756):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-17 11:37:36.936: E/AndroidRuntime(8756):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-17 11:37:36.936: E/AndroidRuntime(8756):     at dalvik.system.NativeStart.main(Native Method)

显然,我在模式/匹配器方法中搞砸了我的正则表达式语法。 任何帮助都很清楚。 谢谢!

1 个答案:

答案 0 :(得分:1)

在您的跟踪中,android.widget.ArrayAdapter似乎缺少代码中的资源,因此这是您无法继续操作的真正问题。

重新使用正则表达式,正则表达式中没有任何capturing groups,并且您的模式与输入字符串不匹配。

使用多个表达式时的最佳方法是一次匹配1或2个表达式,以使这些表达式首先工作。

因为你只想要纬度和经度,这是让你(重新)启动的正则表达式代码:

Pattern NMEA_PATTERN = Pattern.compile(
                ".*Latitude: (\\d{4}.\\d+).*" + 
                "Longitude: (\\d{5}.\\d+).*");
Matcher m = NMEA_PATTERN.matcher(gpsSentence);

if (m.matches()) {
    String latitude = m.group(1);
    String longitude = m.group(2);
    ...
}