我遵循YouTube上的教程將解析添加到android應用程序。在他的教程中,他分析了位於http://demoprojectserver1234.appspot.com/xmlquery.cgi?appid=3fa19507-c746-4dfa-8ded-ec60ef4d30d9的從未關閉的記錄標籤內的數據。我想將其應用於我的項目並解析一些google map api。 http://maps.googleapis.com/maps/api/distancematrix/xml?origins=Denver&destinations=Miami&language=en-EN&sensor=false我想修改教程來捕獲持續時間文本標籤1天5小時的值以及距離文本標籤3,317公里。將解析教程轉換爲項目
這是我完成教程後的主要活動類。我知道我需要在processReceivedData方法中更改if(tagName.equals(「record」),但我不確定我是否使用持續時間或文本,並且我不確定是否會做第二個if( tagName.equals(「record」)來捕獲遙遠的/文本,或者如果我使用了文本,我可以讓它成爲一個循環來抓取它們。任何幫助指向我以正確的方向收集我後面的值將是讚賞。
public class MainActivity extends Activity implements OnClickListener {
private static final String TAG = "ProjectServerDemo";
Button destination_next_button;
public final static String START_LOCATION = "com.google.gascalculator.START_LOCATION";
public final static String END_LOCATION = "com.google.gascalculator.END_LOCATION";
public static final String QUERY_URL = "http://maps.googleapis.com/maps/api/distancematrix/xml?origins="
+ START_LOCATION + "&destinations=" + END_LOCATION
+ "&language=en-EN&sensor=false";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
destination_next_button = (Button) findViewById(R.id.destination_next_button);
destination_next_button.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void destination_next_buttonClick() {
Intent intent = new Intent(MainActivity.this, Vehicle.class);
EditText startText = (EditText) findViewById(R.id.startinglocation);
EditText endText = (EditText) findViewById(R.id.endinglocation);
String startDestination = startText.getText().toString();
String endDestination = endText.getText().toString();
intent.putExtra(START_LOCATION, startDestination);
intent.putExtra(END_LOCATION, endDestination);
startActivity(intent);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.destination_next_button:
destination_next_buttonClick();
AsyncDownloader downloader = new AsyncDownloader();
downloader.execute();
break;
}
}
private void handleNewRecord(String itemId, String data) {
TextView textView = (TextView) findViewById(R.id.informationTextView);
String message =
textView.getText().toString() + "\n" +
itemId + ": " + data;
textView.setText("");
}
private class AsyncDownloader extends AsyncTask<Object, String, Integer> {
@Override
protected Integer doInBackground(Object... arg0) {
XmlPullParser receivedData = tryDownloadingXmlData();
int recordsFound = tryParsingXMLData(receivedData);
return recordsFound;
}
private XmlPullParser tryDownloadingXmlData() {
try {
URL xmlUrl = new URL(QUERY_URL);
XmlPullParser receivedData = XmlPullParserFactory.newInstance().newPullParser();
receivedData.setInput(xmlUrl.openStream(), null);
return receivedData;
} catch (XmlPullParserException e) {
} catch (IOException e) {
}
return null;
}
private int tryParsingXMLData(XmlPullParser receivedData) {
if (receivedData != null) {
try {
return processReceivedData(receivedData);
} catch (XmlPullParserException e) {
} catch (IOException e) {
}
}
return 0;
}
private int processReceivedData(XmlPullParser xmlData) throws XmlPullParserException, IOException {
int recordsFound = 0;
//find values in the xml records
String appId = "";
String itemId = "";
String timeStamp = "";
String data = "";
int eventType = -1;
while (eventType != XmlResourceParser.END_DOCUMENT) {
String tagName = xmlData.getName();
switch (eventType) {
case XmlResourceParser.START_TAG:
//start of a record so pull values encoded as attributes
if (tagName.equals("record")) {
appId = xmlData.getAttributeValue(null, "appid");
itemId = xmlData.getAttributeValue(null, "itemid");
timeStamp = xmlData.getAttributeValue(null, "timestamp");
data = "";
}
break;
//Grab data text (simple processing)
//Note this could be full xml data to process
case XmlResourceParser.TEXT:
data += xmlData.getText();
break;
case XmlPullParser.END_TAG:
if (tagName.equals("record")) {
recordsFound++;
publishProgress(appId, itemId, data, timeStamp);
}
break;
}
eventType = xmlData.next();
}
//handle no data available publish an empty event.
if (recordsFound == 0) {
publishProgress();
}
return 0;
}
@Override
protected void onProgressUpdate(String... values) {
if (values.length == 4) {
String appId = values[0];
String itemId = values[1];
String data = values[2];
String timestamp = values[3];
handleNewRecord(itemId, data);
}
super.onProgressUpdate(values);
}
}
}