如何使用JAVA来解析此数据
*HQ20015067394874,BA&A1300553047441412045763260019050912&E12345678&B0180000000#
我这里只有MFC来做的,如何转换成JAVA的呢?
CString strsave,strleft,strright,strright1,strreply,strtelnum,strcode,strhour,strminute,strsecond,strdate;
CString strlat,strlat1,strlng,strlng1,stryear,strmonth,strday,strspeed,strheading,strmileage;
GetDlgItem(IDC_EXTRACTDATA)->GetWindowText(strsave);
MessageBox(strsave);
//将接受到的数据进行处理,截取*与#之间的数据进行存储。
int num = 0;
int strsave_length = strsave.GetLength();
//strda = strda.Right(strda_length-6);
if(strsave_length >0)
{
int num = strsave.Find(_T(','));//查询*的unicode码
if(num > 0)
{
strleft = strsave.Left(num);//取逗号以前的数据。
MessageBox(strleft,"strleft = %s");
strreply = strleft.GetAt(5);//获取回复属性。
strtelnum = strleft.Right(11);//获取终端号码。
MessageBox(strreply,"strreply = %s");
MessageBox(strtelnum,"strtelnum = %s");
strright = strsave.Right(strsave_length-num-1);//取逗号以后的数据
MessageBox(strright,"strright = %s");
intstrright_length = strright.GetLength();
strcode = strright.Left(2);//获取功能类型编码
strhour = strright.Mid(4,2);
strminute = strright.Mid(6,2);
strsecond = strright.Mid(8,2);
strlat = strright.Mid(10,2);
strlat1 = strright.Mid(12,6);
double lati = atof(strlat)+atof(strlat1)/(60*10000);//获取纬度数据
strlng = strright.Mid(18,3);
strlng1 = strright.Mid(21,7);
double lngi = atof(strlng)+atof(strlng1)/(60*10000);//获取经度数据
stryear = strright.Mid(strright_length-23-2,2);
strmonth = strright.Mid(strright_length-23-4,2);
strday = strright.Mid(strright_length-23-6,2);
strheading = strright.Mid(strright_length-23-8,2);
strspeed = strright.Mid(strright_length-23-10,2);
int heading = atoi(strheading);
int speed = atoi(strspeed);
int num1 = strright.Find(_T("&E"));
strright1 = strright.Right(strright_length-num1);//取&E及它以后的数据
strmileage = strright1.Mid(2,8);
int mileage = atoi(strmileage);
//MessageBox(strright1,"strright1 = %s");
//MessageBox(strmileage,"strmileage = %s");
//MessageBox(strlat1,"strlat1 = %s");
//MessageBox(strlng1,"strlng1 = %s");
//MessageBox(strhour,"strhour = %s");
//MessageBox(strminute,"strminute = %s");
//MessageBox(strsecond,"strsecond = %s");
//MessageBox(strright1,"strright1 = %s");
CString strsqlin,strlongitude,strlatitude,strtime;
strlongitude.Format("%f", lngi);
strlatitude.Format("%f", lati);
strtime.Format("20%s-%s-%s %s:%s:%s", stryear,strmonth,strday,strhour,strminute,strsecond);
MessageBox(strtime,"time = %s");
//strsqlin.Format("INSERT INTO SmallCar(TelNumber) VALUES('%s')",strtelnum);
//strsqlin.Format("INSERT INTO SmallCar(TelNumber) VALUES ('"+strtelnum+"')");
strsqlin.Format("INSERT INTO SmallCar(TelNumber,PositioningTime,Longitude,Latitude, Speed,Heading,Mileage,Reply,FunctionTypeCode) VALUES('"+strtelnum+"','"+strtime+"','"+strlongitude+"','"+strlatitude+"','"+strspeed+"','"+strheading+"','"+strmileage+"','"+strreply+"','"+strcode+"')");
try{
m_pRecordset1= m_pConnection1->Execute(_bstr_t(strsqlin), NULL, adCmdText); // 获得记录集
}
catch (_com_error d)
{
AfxMessageBox("执行SQL失败!");
return;
}
}
else
{
MessageBox("解析数据失败,请确定收到的数据格式!!");
}
}
else
{
MessageBox("没有数据可供分解存入数据库!!");
}
[解决办法]
String text = "HQ20015067394874,BA&A1300553047441412045763260019050912&E12345678&B0180000000#"; String strreply; String strtelnum; String strcode; String strhour; String strminute; String strsecond; String strlat; String strlat1; String strlng; String strlng1; String stryear; String strmonth; String strday; String strheading; String strspeed; String strmileage; if(text.length()>0){ int num = text.indexOf(","); if(num>0){ String strleft = text.substring(0, num);// System.out.println("strleft = " + strleft); strreply = String.valueOf(strleft.charAt(5)); //获取回复属性 System.out.println("strreply = " + strreply); strtelnum = strleft.substring(strleft.length()-11, strleft.length()); //获取终端手机 System.out.println("strtelnum = " + strtelnum); String strRight = text.substring(num+1); //获取逗号以后的数据// System.out.println("strright = " + strRight); int strright_length = strRight.length(); strcode = strRight.substring(0, 2); //获取功能类型编码 System.out.println("strcode = "+strcode); strhour = strRight.substring(4, 6); strminute = strRight.substring(6, 8); strsecond = strRight.substring(8, 10); strlat = strRight.substring(10, 12); strlat1 = strRight.substring(12, 18); double lati = Double.parseDouble(strlat) + Double.parseDouble(strlat1)/(60*10000); //获取纬度数据 System.out.println("lati = " + lati); strlng = strRight.substring(18, 21); strlng1 = strRight.substring(21, 28); double lngi = Double.parseDouble(strlng) + Double.parseDouble(strlng1)/(60*10000); //获取经度数据 System.out.println("lngi = " + lngi); stryear = strRight.substring(strright_length-23-2, strright_length-23-2+2); strmonth = strRight.substring(strright_length-23-4, strright_length-23-4+2); strday = strRight.substring(strright_length-23-6, strright_length-23-6+2); strheading = strRight.substring(strright_length-23-8, strright_length-23-8+2); strspeed = strRight.substring(strright_length-23-10, strright_length-23-10+2); int heading = Integer.parseInt(strheading); System.out.println("heading = "+heading); int speed = Integer.parseInt(strspeed); System.out.println("speed = " + speed); strRight = strRight.substring(strRight.indexOf("&E")+2); //截取&E后的数据 strmileage = strRight.substring(0, 8); int mileage = Integer.parseInt(strmileage); System.out.println("mileage = " + mileage); String strtime=String.format("20%s-%s-%s %s:%s:%s", stryear,strmonth,strday,strhour,strminute,strsecond); System.out.println(strtime); } }