我正在尝试用使用HTTP POST URL获取的数据填充ListView,收到的数据是这样的XML文档:
<?xml version="1.0" encoding="utf-8" ?>
- <Arrives>
- <Arrive>
<IdStop>226</IdStop>
<idLine>70</idLine>
<IsHead>True</IsHead>
<Destination>ALSACIA</Destination>
<IdBus>0000</IdBus>
<TimeLeftBus>0</TimeLeftBus>
<DistanceBus>373</DistanceBus>
<PositionXBus>-1</PositionXBus>
<PositionYBus>-1</PositionYBus>
<PositionTypeBus>2</PositionTypeBus>
</Arrive>
- <Arrive>
<IdStop>226</IdStop>
<idLine>11</idLine>
<IsHead>True</IsHead>
<Destination>BARRIO BLANCO</Destination>
<IdBus>0000</IdBus>
<TimeLeftBus>161</TimeLeftBus>
<DistanceBus>1498</DistanceBus>
<PositionXBus>-1</PositionXBus>
<PositionYBus>-1</PositionYBus>
<PositionTypeBus>1</PositionTypeBus>
</Arrive>
- <Arrive>
<IdStop>226</IdStop>
<idLine>N3</idLine>
<IsHead>True</IsHead>
<Destination>CIBELES</Destination>
<IdBus>0000</IdBus>
<TimeLeftBus>422</TimeLeftBus>
<DistanceBus>1923</DistanceBus>
<PositionXBus>-1</PositionXBus>
<PositionYBus>-1</PositionYBus>
<PositionTypeBus>1</PositionTypeBus>
</Arrive>
</Arrives>
我需要在ListView中使用的唯一标签是idLine,Destination和TimeLeftBus。
我计划在HTTP请求中使用的方法:(我也可以使用GET METHOD执行此操作)
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("SITE URL");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("apiid", "API ID"));
nameValuePairs.add(new BasicNameValuePair("apikey", "API KEY"));
nameValuePairs.add(new BasicNameValuePair("stopid", "STOP ID"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Log exception
} catch (IOException e) {
// TODO Log exception
}
}
如何实现这个?谢谢你的帮助。
答案 0 :(得分:0)
我并不熟悉从HTTP POST URL获取数据,但我知道如何将xml数据提供给列表视图。我会做的是
请参阅以下代码了解每个步骤。
第1步:
public class ArrivalInfo {
private String idLine;
private String destination;
private String timeLeft;
public void setIdLine(String id) {
this.idLine = id;
}
public String getIdLine() {
return idLine;
}
...(getters and setters for other fields)
}
第2步:
public class XMLParser {
//the tags as found in your XML document
private static final String ARRIVE = "arrive";
private static final String ID_LINE = "idLine";
private static final String DEST = "Destination";
private static final String TIME_LEFT_BUS = "TimeLeftBus";
private ArrivalInfo curArrival = null;
List<ArrivalInfo> arrivals = new ArrayList<ArrivalInfo>();
public List<ArrivalInfo> getArrivals() {
return arrivals;
}
public void parseXml() {
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
InputStream stream = getResources().openRawResource(...) (this can be done in different
ways depending on where the file is)
int eventType = xpp.getEventType();
while(eventType != XmlPullParser.END_DOCUMENT) {
if(eventType == XmlPullParser.START_TAG) {
handleStartTag(xpp.getName());
}
else if (eventType == XmlPullParser.END_TAG) {
handleEndTag(xpp.getName());
}
else if (eventType == XmlPullParser.TEXT) {
handleText(xpp.getName(), xpp.getText());
}
eventType = xpp.next();
} catch(NotFoundException e) {
...handle exeptions
}
public void handleStartTag(String tag) {
if(tag.equalsIgnoreCase(ARRIVE)) {
curArrival = new ArrivalInfo();
}
}
public void handleEndTag(String tag) {
if(tag.equalsIgnoreCase(ARRIVE)) {
arrivals.add(curArrival);
}
}
public void handleText(String tag, String text) {
if(curArrival != null) {
if(tag.equalsIgnoreCase(ID_LINE)) {
curArrival.setIdLine(text);
}
else if (tag.equalsIgnoreCase(DEST)) {
curArrival.setDestination(text);
}
// and so on...
}
}
} //end of XmlParser
步骤3 :(这将在非UI线程中发生,即使用AsyncTask在不同的线程上执行此操作);
public class MyActivity extends Activity {
private ListView mListView;
private StableArrayAdapter adapter;
private List<ArrivalInfo> arrivals;
private class LoadDataTask extends AsyncTask<Void, Void, List<ArrivalInfo>>() {
@Override
public void doInBackground(Void...params) {
XmlParser parser = new XmlParser();
try {
parser.parseXml();
} finally {
return parser.getArrivals();
}
}
@Override
public void onPostExecute(List<ArrivalInfo> list) {
if(mListView != null && adapter != null) {
arrivals = list;
adapter.notifyDataSetChanged();
}
}
} // end of loading task.
@Override
public void onCreate(Bundle savedInstanceState) {
arrivals = new ArrayList<ArrivalInfo>();
setContentView(R.layout.my_layout);
mListView = findViewById(android.R.id.list);
StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_1, arrivals);
mListView.setAdapter(adapter);
new LoadDataTask().execute();
}
} // end of your activity
这就是它!希望能帮助你做你想做的事。